How to print integers in assembly loop [duplicate]
I'm expirimenting with assembly. Now I have a simple while loop starting with eax = 1 and then loop until eax = 10. In the loop i use a print macro to print te progress (should print 1 - 10) but it doesnt work..
section .bss
buffer resd 1
section .text
global _start
%macro write 3
mov eax, 4
mov ebx, %1
mov ecx, %2
mov edx, %3
int 80H
%endmacro
_start:
xor eax, eax
inc eax
while:
cmp eax, 10
jg end
开发者_如何学JAVA mov [buffer], eax
write 1, [buffer], 1
mov eax, [buffer]
inc eax
jmp while
end:
mov eax, 1
xor ebx, ebx
int 0x80
The c equivalent is:
#include <stdio.h>
int main(void)
{
unsigned int i = 1;
while(i <= 10)
{
printf("%d", i);
i++;
}
return 0;
}
So my question is.. how can i use the write macro to print the values? What do i have to change in it?
Thanks
Ok, there are a couple of things:
1) The syscall you are calling writes an ascii string, so you have to convert from integer to ascii. From 0 to 9 you have to add 30h to the integer (if I remember well) to get the corresponding ascii code, but for larger number it gets more and more complicated.
2) The size you are passing to sys_write (1) is wrong, you should pass 3 (2 bytes to hold the number + one terminating null byte).
And since you have to change the value of all registers in order to call sys_write I would suggest you using esi or edi to hold the counter or push, then pop it when writing.
精彩评论