1

I just started to learn x86 assembly and I tried to write a simple program that prints all the ascii characters and a line break to the standard output. It prints everything as expected except the line break and I can't figure out why. I compiled it with nasm on a 64 bit ubuntu operating system. Here is the code:

section .data
curr db ' '

section .text

global _start

_start:

    next:

        ;print current character
        mov eax,4
        mov ebx,1
        mov ecx,curr
        mov edx,1
        int 0x80

        ;check condition and increment curr
        inc byte [curr]
        cmp byte [curr],126
        jle next

    ;new line and exit <--- doesn't work ???
    mov eax,4
    mov ebx,1
    mov ecx,10
    mov edx,1
    int 0x80

    mov eax,1
    mov ebx,1
    int 0x80
Michael Petch
  • 46,082
  • 8
  • 107
  • 198
  • 2
    `mov ecx,10` is wrong. _ECX_ needs to be a pointer to a buffer containing a newline character not the ASCII value of a newline character. You do it correctly for the space character in `curr`, just do the same for newline. – Michael Petch Dec 31 '17 at 01:22
  • 1
    If you are making 32-bit application on a Linux distro that allows running of 32-bit code then this code will work. If you want to create a 64-bit application I recommend dumping `int 0x80` and move to SYSCALL.See this [information](http://blog.rchapman.org/posts/Linux_System_Call_Table_for_x86_64/). – Michael Petch Dec 31 '17 at 01:26
  • 1
    Related: [What happens if you use the 32-bit int 0x80 Linux ABI in 64-bit code?](https://stackoverflow.com/questions/46087730/what-happens-if-you-use-the-32-bit-int-0x80-linux-abi-in-64-bit-code). Your code would fail with `int 0x80` if you had used stack space instead of a static storage location. Unless you're building this as a 32-bit executable: https://stackoverflow.com/questions/36861903/assembling-32-bit-binaries-on-a-64-bit-system-gnu-toolchain/36901649 – Peter Cordes Dec 31 '17 at 02:19

1 Answers1

1

The problem is that in that system call, ECX is a pointer, not the character you want to print. Perhaps modifying it like so?

 MOV byte [curr], 10
 MOV ECX, curr
 MOV EAX, 4
 MOV EDX, 1
 INT 0x80
Michael Petch
  • 46,082
  • 8
  • 107
  • 198
David Hoelzer
  • 15,862
  • 4
  • 48
  • 67