Hi,
I am attempting to create a counter program in assembler. I would like to do something like this little C program:
/* Counter program */
#include <stdio.h>
main()
{
long long int counter;
counter = 1;
for ( ; ; )
{
counter = counter + 1;
printf("%015lld\n", counter);
}
}
Here is my first attempt. It, unfortunately, only works up to 9, then prints ascii characters instead of numbers:
; Count up and print each time until end
section .text
global _start
_start:
mov eax,0 ; Initialize eax to 0
loop:
inc eax ; Increment edx
cmp eax,50 ; Stop counting when we reach 50
jz exit ; Exit the program
push eax ; Push the non converted eax to the stack
add eax,'0' ; Convert number to a string
push eax ; Push the converted eax to the stack
mov ecx,esp ; Get address of the current stack pointer into ecx for sys_write
mov eax,4 ; setup sys_write
mov ebx,1 ; stdout
mov edx,1 ; one character
int 80h ; call sys_write
pop eax ; Release the converted item in the stack
call newline ; Call newline function to print a newline
pop eax ; Get the non converted eax back
jmp loop
newline:
mov edx, 10 ; Move 'newline' character into edx
push edx ; Put it into the stack
mov ecx, esp ; Put the current stack pointer into ecx for sys_write
mov eax,4 ; sys_write
mov ebx,1 ; stdout
mov edx,1 ; Only on character to print out
int 80h ; call sys_write
pop edx ; Don't need newline anymore, get rid of it so stack pointer points to what it was before
ret
exit:
mov eax,1 ; Function exit
mov ebx,0 ; Return code 0
int 80h ; Execute function
I know that sys_write is using only one character and that it should find the length properly.
qiet72