This nasm x86-32 code doesn't give any output.
It's a simple program to sum integers in an array, then convert the result to string for printing to the console.
The subroutine int_to_string
is from a thread in SO, but I can't find it anymore.
I believe there's an issue when calling to or returning from the subroutine.
If someone can point to the issue(s). Thank you.
section .text
global _start
_start:
mov eax, 0
mov ebx, array
mov ecx, 5
loop:
cmp ecx, 0
je print
add eax, [ebx]
add ebx, 4
sub ecx, 1
jmp loop
print:
; convert integer to string
mov esi, buffer
call int_to_string
; eax now holds the address to pass to sys_write
; call write syscall
mov edx, ecx ; length of the string
mov ecx, eax ; address of the string
mov ebx, 1 ; file descriptor, in this case stdout
mov eax, 4 ; Syscall number: write
int 0x80
; call exit syscall
mov eax, 1
int 0x80
; Input:
; eax = integer value to convert
; esi = pointer to buffer to store the string in (must have room for at least 10 bytes)
; Output:
; eax = pointer to the first character of the generated string
; ecx = length of the generated string
int_to_string:
add esi, 9
mov byte [esi], 0 ; String terminator
mov ebx, 10
.next_digit:
xor edx, edx ; Clear edx prior to dividing edx:eax by ebx
div ebx ; eax /= 10
add dl, '0' ; Convert the remainder to ASCII
dec esi ; store characters in reverse order
mov [esi], dl
test eax, eax
jnz .next_digit ; Repeat until eax==0
; return a pointer to the first digit (not necessarily the start of the provided buffer)
mov eax, esi
ret
section .data
array dd 10, 20, 30, 40, 50
section .bss
buffer resb 10
CodePudding user response:
Your code works perfectly except for one thing.
When you call write syscall, you pass the ecx register to the edx register as a string length, but ecx is 0 because the code subtracted ecx just before in the for loop.
So add mov ecx, 3
just before mov edx, ecx
and it will be fine.