0

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

    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

Michel
  • 1
  • 1
  • 1
  • If you are working in assembly language, you need to learn to read and think in hexadecimal. You can of course code a conversion to decimal and would do so for user-destined output, but when monitoring your code it's a poor shorthand for binary, while hexadecimal is a near ideal one. – Chris Stratton Feb 04 '11 at 17:55

1 Answers1

3

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.

BlackBear
  • 22,411
  • 10
  • 48
  • 86