-1

So I'm trying to implement the strlength function in x86 assembly. I've got most of it down, but I can't figure out how to print the integer value to the console. I'm using the sys_write call and all that I'm getting is a weird symbol for output or nothing at all. Can anyone tell me how I can accurately print out my counter in ECX to the console?

section .text
    global _start

_start:

    getInput:
    mov eax, 3
    mov ebx, 0
    mov ecx, msg
    mov edx, 50
    int 0x80

    mov eax, msg
    xor ecx, ecx
    jmp compare
    counter:
    add eax, 1
    add ecx, 1
    compare:
    cmp [eax], byte 0
    jnz counter
    dec ecx
    mov [len], ecx

    printInput:
    mov eax, 4
    mov ebx, 1
    mov ecx, len
    mov edx, 50
    int 0x80

    mov eax, 1
    mov ebx, 0
    int 0x80

section .bss
    msg resb 50
    len resb 5
Asa Hunt
  • 129
  • 4
  • 10
  • 1
    possible duplicate of [How do I print an integer in Assembly Level Programming without printf from the c library?](http://stackoverflow.com/questions/13166064/how-do-i-print-an-integer-in-assembly-level-programming-without-printf-from-the) – Michael Petch Sep 08 '15 at 20:32
  • It's pretty close. I guess I just don't understand how to implement that solution on a short number like 11 or something. – Asa Hunt Sep 08 '15 at 21:15
  • The problem is that `sys_write` requires a pointer to a buffer containing a string of characters. Your buffer contains the length as a value. That can't be printed properly by `sys_write`. You need to convert each digit in the number stored at variable `len` into a printable string and pass that buffer to `sys_write`. This applies to small and bigger numbers. – Michael Petch Sep 08 '15 at 22:18

1 Answers1

1

As far as I can tell, you don't do any arithmetic to convert the integer value (which might be something like 1756) to a string of characters.

Of course, this can't be printed. You will need to form a proper string.

Marcus Müller
  • 34,677
  • 4
  • 53
  • 94