Assembler: Add
Small assembler program, that reads two numbers from stdin and prints the sum (+ a newline) to stdout.
Uses 64 bit registers and amd's SYSCALL. (Which is now also implemented on intel processors).
Uses "syscall_64_syscall.inc" from here
; assemble using nasm -f elf64 -g numbers.asm && ld -o numbers numbers.o && ./numbers
BITS 64
section .text
global _start
_start:
%include "syscall_64_syscall.inc"
%define _stdin 0
%define _stdout 1
%define _stderr 2
call readnumber
mov r9,r8
call readnumber
add r8,r9
call printnumber
mov bl,0x0A
mov [sngbuf],bl
mov rax,_sys_write
mov rdi,_stdout
mov rsi,sngbuf
mov rdx,1
syscall
mov rax,_sys_exit
mov rdi,0
syscall
readnumber:
push r9
push r10
xor r8,r8
xor r9,r9
readnumber_loop:
mov rax,_sys_read
mov rdi,_stdin
mov rsi,sngbuf
mov rdx,1
syscall
xor r10,r10
mov r10b,[sngbuf]
cmp r10,0x09
je readnumber_whitespace
cmp r10,0x0A
je readnumber_whitespace
cmp r10,0x0B
je readnumber_whitespace
cmp r10,0x0C
je readnumber_whitespace
cmp r10,0x0D
je readnumber_whitespace
cmp r10,0x20
je readnumber_whitespace
cmp r10,0x30
jl readnumber_loop
cmp r10,0x39
jg readnumber_loop
sub r10,0x30
mov rax,r8
mov rbx,10
mul rbx
mov r8,rax
add r8,r10
mov r9,1
jmp readnumber_loop
readnumber_whitespace:
cmp r9,0
je readnumber_loop
pop r10
pop r9
ret
printnumber:
mov rcx,1000000000000000000
mov rbx,9
mov rdx,0
printnumber_loop:
mov rax, rcx
mul rbx
cmp rax,r8
jg printnumber_zugross
cmp rbx,0
jg printnumber_write
cmp rdx,0
je printnumber_nowrite
printnumber_write:
sub r8,rax
add rbx,0x30
mov [sngbuf],bl
sub rbx,0x30
push rax
push rdx
push rcx
mov rax,_sys_write
mov rdi,_stdout
mov rsi,sngbuf
mov rdx,1
syscall
pop rcx
pop rdx
pop rax
mov rdx,1
printnumber_nowrite:
jmp printnumber_blab
printnumber_zugross:
cmp rbx,0
jg printnumber_weiter
printnumber_blab:
mov rbx,10
cmp rcx,1
jg printnumber_div
ret
printnumber_div:
mov rax,rcx
mov rcx,10
push rdx
xor rdx,rdx
div rcx
pop rdx
mov rcx,rax
printnumber_weiter:
dec rbx
jmp printnumber_loop
section .data
figbuf db 'Puffergrossgenugfuergrossezahlen',0x0A
sngbuf equ $ - 1