NASM Forum > Programming with NASM
How to sum 2 numbers (in Gnu/linux assembly x80_64 & Nasm)?
Ferran:
Hello everybody !
Suddenly a poor programmer forgot how to sum 2 numbers. He told me he was happy with i386 machines in 32 bits, but now in 64 bits he don't. He gave me this code if its possible we can help him.
--- Code: ---;-----------------------------------------------------------------
; sum.asm
; Learning to sum 2 numbers with 64 bit assembly
; (for GNU/Linux Assembly x80_64 Intel syntax & NASM compiler)
;
; Compile: nasm -f elf64 sum.asm
; Link: ld -s -o sum sum.o
; Run: ./sum
;
; Author: anonymous ashamed...
;------------------------------------------------------------------
segment .data
num1: dd 12
num2: dd 55
lf: db " ", 10, 0
res: dd " ", 10, 0
len_res equ $ - res
segment .text
global _start
_start:
.sum:
mov rax, [num1]
add rax, [num2]
mov [res], rax
.show_result:
mov rax, 1
mov rdi, 1
mov rsi, res
mov rdx, len_res
syscall
.showLF:
mov rax, 1
mov rdi, 1
mov rsi, lf
mov rdx, 2
syscall
.exit:
xor edi, edi
mov rax, 60
syscall
--- End code ---
I appeal to the charity of the good people of this forum to help to this poor programmer.
Why don't works the code?
fredericopissarra:
Because you need to transform the binary value in a string...
Ferran:
I saw in a book in Assembly64 pdf http://www.egr.unlv.edu/~ed/assembly64.pdf one script that convert integer to ascii (itoa function), i mixed it with our script but it not prints nothing :(
this is the mix i did
--- Code: ---;-----------------------------------------------------------------
; sum.asm
; Learning to sum 2 numbers with 64 bit assembly
; (for GNU/Linux Assembly x80_64 Intel syntax & NASM compiler)
;
; Compile: nasm -f elf64 sum.asm
; Link: ld -s -o sum sum.o
; Run: ./sum
;
; Author: anonymous ashamed...
;------------------------------------------------------------------
segment .data
num1 dd 1111111111
num2 dd 2222222222
res dd 0 ; The number to print
newline db 10
; Syscall information
sys_exit equ 60
sys_write equ 1
; Streams
stdout equ 1
; Constants
BUFFER_SIZE equ 10
section .bss
numbuf resb BUFFER_SIZE ; A buffer to store our string of numbers in
segment .text
global _start
_start:
.sum:
mov rax, [num1]
add rax, [num2]
mov [res], rax
mov rdi,[res]
call itoa
; Write the string returned in rax out to stdout
mov rdi,rax ; The string pointer is returned in rax - move it to rdi for the function call
mov rsi,rcx
call print
; Write the newline character to stdout
mov rdi,newline
mov rsi,1
call print
; Exit
mov rax,sys_exit
mov rbx,0
int 0x80
; Args: (rdi: char*, rsi: int)
print:
mov rax,sys_write
mov rbx,stdout
mov rcx,rdi
mov rdx,rsi
int 0x80
ret
itoa:
push rbp
mov rbp,rsp
sub rsp,4 ; allocate 4 bytes for our local string length counter
mov rax,rdi ; Move the passed in argument to rax
lea rdi,[numbuf+10] ; load the end address of the buffer (past the very end)
mov rcx,10 ; divisor
mov [rbp-4],dword 0 ; rbp-4 will contain 4 bytes representing the length of the string - start at zero
.divloop:
xor rdx,rdx ; Zero out rdx (where our remainder goes after idiv)
idiv rcx ; divide rax (the number) by 10 (the remainder is placed in rdx)
add rdx,0x30 ; add 0x30 to the remainder so we get the correct ASCII value
dec rdi ; move the pointer backwards in the buffer
mov byte [rdi],dl ; move the character into the buffer
inc dword [rbp-4] ; increase the length
cmp rax,0 ; was the result zero?
jnz .divloop ; no it wasn't, keep looping
mov rax,rdi ; rdi now points to the beginning of the string - move it into rax
mov rcx,[rbp-4] ; rbp-4 contains the length - move it into rcx
leave ; clean up our stack
ret
--- End code ---
I will follow searching the solution, but it seems difficult (for me). See you.
fredericopissarra:
it is wrong. But I already shown how to do it, in another post...
debs3759:
For starters, you are trying to print a number without converting it to an ASCII string first, as fredericopissarra already pointed out.
Navigation
[0] Message Index
[#] Next page
Go to full version