arithmetic calculations-assembly langugage - monetdb-assembly-language

assembly language
**main.asm**
%include "lib.asm"
section .text
global _start
_start:
;
; YOUR CODE HERE!
;
; SAMPLE
mov rdi, 65526 ; going to print 65536
call print_num ; print the number
call print_lf ; move to next line
exit:
mov rax, 60 ; exit
mov rdi, 0 ; return code
syscall

Related

How to run an ASM file in VS Code

App: VS Code version 1.65.2
OS: Linux Mint 20.2 Cinnamon
I want to run the following code in Assembly:
; The input is a decimal number,
; Result is a hexadecimal number
section .text
; to make the printf out work the main 'method' is needed
global main
; for printing numbers out
extern printf
main:
; Find factorial for the initial value of ecx,
mov ecx, 5
; Copy the initial value of ecx to eax,
; eax is our factorial result
mov eax, ecx
loop:
; If the counter is less or equal to 1, finish
cmp ecx, 1
jle end
sub ecx, 1
mul ecx
jmp loop
end:
; Display the message and exit
push eax
push message
call printf
add esp, 8
ret
section .data
message: db "The result is = %08X", 10, 0
I have installed:
Cutting edge x86 and x86_64 assembly syntax highlighting.
Arm assembly syntax support for Visual Studio Code.
However, running the file in both extensions returns me the following:
Code language not supported or defined.

MASM x86-64 scanf not reading spaces

I have simple 64 bit assembly program that we are doing for class. It is supposed to take user input (string) and return that string with lowercase letters into uppercase and uppercase into lowercase.
With what I have, it will read anything until it finds a space and this will not read anymore after that. So if I input "test", it will output "TEST". If I input "test Test" it will output "TEST". However, if I add spaces before the first word, it would output the first word but removes the spaces. For example: input " TesT", output: "tESt".
Anyone know how I can go about fixing this?
Here is my whole program:
;Author: Keenan Kaufman
;Date: 10/20/2017
INCLUDELIB msvcrt.lib
printf PROTO
scanf PROTO
exit PROTO
.DATA
CRLF BYTE 0Dh, 0Ah, 0 ;carriage return
msgHeader BYTE "Enter a mixed case string: ", 0
message BYTE 20 DUP(0), 0
target BYTE SIZEOF message DUP(?), 0Dh, 0Ah, 0
msgformat BYTE "%20s", 0
.CODE
main PROC
;Display request for user input
lea rcx, msgHeader
call printf
;obtain user input
lea rcx, msgformat
lea rdx, message
call scanf
lea rsi, message
lea rdi, target
jmp GETNEXT
GETNEXT:
mov al, [rsi]
cmp al, 0
je ENDCASE
cmp al, 'z'
ja NOCHANGE
cmp al, 'A'
jb NOCHANGE
cmp al, 'a'
jae TOUPPER
cmp al, 'Z'
jbe TOLOWER
TOUPPER:
sub al, 32
mov [rdi], al
inc rdi
inc rsi
jmp GETNEXT
TOLOWER:
add al, 32
mov [rdi], al
inc rdi
inc rsi
jmp GETNEXT
NOCHANGE:
mov [rdi], al
inc rdi
inc rsi
jmp GETNEXT
ENDCASE:
jmp FINISH
FINISH:
;Display target
lea rcx, target
call printf
lea rcx, CRLF
call printf
mov rax, 0
call exit
main ENDP
END
Yes, that is a feature of scanf. Here is the Linux man page for scanf which, for the %s format, says:
s
Matches a sequence of non-white-space characters; the next
pointer must be a pointer to the initial element of a character
array that is long enough to hold the input sequence and the
terminating null byte ('\0'), which is added automatically. The
input string stops at white space or at the maximum field width,
whichever occurs first.
To do what you want, read the characters yourself directly from stdin using read(), getc(), or fgets().

FASM IRC Bot Prefix

I've been trying to learn how to create an IRC bot in assembler from some old sources. Everything is going fine with my learning except for a prefix problem.
The prefix for the bot is:
CommandPrefix equ "^^"
And the length of the prefix is added with:
add eax, 2d
I want to change the prefix to just "^", but I am having trouble with figuring out what "add eax" should be changed too for it to work. Or even if that is the best way to do it. Any help with this would be appreciated.
Here is what the original code looks like to get some idea:
include "win32ax.inc"
entry Bot
CommandPrefix equ "^^"
section '.code' code readable executable
Bot:
invoke WSAStartup,0101h,WSAData
cmp eax, 0
jne Exit
invoke socket,AF_INET,SOCK_STREAM,0
cmp eax, -1
je Exit
mov dword [SocketDesc], eax
invoke inet_addr,IRCServer
mov dword [SockAddr_IP], eax
invoke htons,IRCPort
mov word [SockAddr_Port], ax
invoke connect,dword [SocketDesc],SockAddr,16d
cmp eax, 0
jne Exit
call GenerateNickname
invoke lstrcpy,SendBuffer,"NICK "
invoke lstrcat,SendBuffer,Nickname
call SendLine
invoke lstrcpy,SendBuffer,"USER "
invoke lstrcat,SendBuffer,Nickname
invoke lstrcat,SendBuffer," 8 * :"
invoke lstrcat,SendBuffer,Nickname
call SendLine
GetMotd:
call RecvLine
call HandlePing
mov ecx, 0
IsMotd:
cmp dword [ReturnBuffer + ecx], "MOTD"
je HaveMotd
cmp byte [ReturnBuffer + ecx], 0d
je GetMotd
inc ecx
jmp IsMotd
HaveMotd:
invoke lstrcpy,SendBuffer,"JOIN "
invoke lstrcat,SendBuffer,Channel
invoke lstrcat,SendBuffer," "
call SendLine
RecvCommand:
call RecvLine
call HandlePing
mov ecx, 0
IsCommand:
cmp word [ReturnBuffer + ecx], CommandPrefix
je HaveCommand
cmp byte [ReturnBuffer + ecx], 0
je RecvCommand
inc ecx
jmp IsCommand
HaveCommand:
mov ebx, ReturnBuffer
add ebx, ecx
add ebx, 2d ;add length of command prefix
invoke lstrcpy,CommandBuffer,ebx
call ExecuteCommand
jmp RecvCommand

Emacs weirdness when trying to comment in Assembly

Suppose I have a block of code like so:
;; outut
mov eax, 4
mov ebx, 1 ; stdout
mov ecx, [ans] ; move biggest element to accumulator
add ecx, 30h ; convert to ascii representation
mov [buff], ecx ; move to memory
mov ecx, buff ; put pointer in ecx for printing
mov edx, 4 ; size, 4 bytes
int 80h ; system call.
When I try to put a comment in the front to comment out a line:
;; outut
;mov eax, 4
mov ebx, 1 ; stdout
mov ecx, [ans] ; move biggest element to accumulator
add ecx, 30h ; convert to ascii representation
mov [buff], ecx ; move to memory
mov ecx, buff ; put pointer in ecx for printing
mov edx, 4 ; size, 4 bytes
int 80h ; system call.
Instead of appearing there where I want it to go, it jumps to here:
;; outut
mov eax, 4 ;
mov ebx, 1 ; stdout
mov ecx, [ans] ; move biggest element to accumulator
add ecx, 30h ; convert to ascii representation
mov [buff], ecx ; move to memory
mov ecx, buff ; put pointer in ecx for printing
mov edx, 4 ; size, 4 bytes
int 80h ; system call.
And no matter what I do, I physically cannot comment out anything.
How can I fix this? It don't remember it always doing this, so i feel like I must have hit some combination of keys and it just happens.
; is bound to asm-comment in assembly mode. You can either do a quoted insert with C-q ; on a case-by-case basis, or remove the binding and just use M-; (comment-dwim) for fancier commenting. If you want to do the latter, set ";" locally to do a self-insert command:
(defun my-hook ()
(local-set-key ";" 'self-insert-command))
(add-hook 'asm-mode-hook 'my-hook)

Using NASM and TCP Sockets

I am trying to teach myself some NASM x86 assembly using some Unix system calls. I am trying to create a simple TCP server and I have the code working up until the send() command. I can connect via telnet but I get a segfault once my code reaches the point where it tries to send a response to the client.
This is the segment of code that is producing a segfault:
; push on to stack for send
push dword 0
push dword [start_len]
push dword [start]
push dword [socket]
; send something back
; THIS IS WHERE THE SEGFAULT OCCURS
mov eax,102
mov ebx,9 ; send is 9
mov ecx,esp
int 80h
Here is the full source code if anyone wants to look at it. Any help would be greatly appreciated!
; constants go here
section .data
start: db 'Starting Socket...',0
start_len: equ $-start
; variables go here
section .bss
socket: resd 1 ; store the fd for the socket
socket_address: resd 2 ; socket address
; starttttt
section .text
global _start
open_socket:
; print we are starting
mov eax,4
mov ebx,1
mov ecx,start
mov edx,start_len
int 80h
; push values to stack to make call
; values go in in opposite order, so when they are popped
; it is the correct order
; below is for a tcp socket
push dword 6
push dword 1
push dword 2
; make call to open socket
mov eax,102 ; 102 is the call to open socket
mov ebx,1 ; sub call, socket()
mov ecx,esp
int 80h
; store the file descriptor for the socket
mov dword[socket],eax
; this is the socket address to bind to
push dword 0x00000000 ; localhost (127.0.0.1)
push dword 0x2823 ; port 9000
push word 2 ; AF_INET (IPv4)
mov [socket_address],esp ; move to our socket address variable
; setup parameters for bind call bind(socket, socket_address, 16)
push dword 16
push dword [socket_address]
push dword [socket]
; call subcall for socket to bind
mov eax,102; sys_socket
mov ebx,2 ; subcall 2 = bind()
mov ecx,esp ; push vars from stack to params
int 80h
; setup parameters for listen()
push byte 20
push dword [socket]
; call listen()
mov eax,102 ; socket call
mov ebx,4 ; subcall listen()
mov ecx,esp ; move stack as variables
int 80h
; now we have to accept incoming connections...
; setup the call
push 0
push 0
push dword [socket]
; call accept()
mov eax,102
mov ebx,5
mov ecx,esp
int 80h
; push on to stack for send
push dword 0
push dword [start_len]
push dword [start]
push dword [socket]
; send something back
; THIS IS WHERE THE SEGFAULT OCCURS
mov eax,102
mov ebx,9 ; send is 9
mov ecx,esp
int 80h
; function to exit the program
exit:
mov eax,1
mov ebx,0
int 80h
; main function to be called
_start:
; open it
call open_socket