You should be using the same system call to print the newline that you're using to print the numbers.
Also, newline in Linux is just LF (char 10,) not CR (char 13) followed by LF like Windows/DOS uses.
How to print to stdout on Linux in x86 assembly
This answer describes what each of the arguments does for the Linux print system call, which is what you're calling by raising int 0x80
.
System calls use registers to pass in their arguments. From the linked answer, eax
is the system call number (4 = print), ebx
is the destination stream (1 = stdout), ecx
is a pointer to the data to print, and edx is the length of the data to print.
So, the code that's actually doing the print in your loop is:
mov [num], eax ; Moves the character in eax to the buffer pointed to by num.
mov eax, 4 ; Moves 4 into eax, i.e. selects the print system call.
mov ebx, 1 ; Moves 1 into ebx, i.e. selects stdout as the destination.
mov ecx, num ; Moves the address where your text is stored into ecx.
mov edx, 1 ; Moves the number of bytes to characters (1) into edx.
int 0x80 ; Executes a Linux system call using the above parameters.
To print a newline, you'd just need to have the line feed character (character 10 decimal) in eax
before this code rather the character for a number. So, for example, adding mov eax, 10
before this code would print a line feed instead of a number.
How to make this work with your existing loop
num
in your "my code" section is the buffer where you're storing the data to print. However, this code is also using this memory in order to keep track of the last number it printed. So, there are two ways around losing that information between loops:
Option 1: Simply increase the size of your buffer from 1 byte to 2 and put the line feed in the second byte. You can then just move 2
into edx
instead of 1
to tell Linux that you'd like to print 2 characters, thus printing both the number and the line feed on each loop iteration.
Option 2: Allocate another single-byte buffer to store the line feed. Move the line feed character there, then make a second system call after the system call that prints the number in the loop to print the line feed. If your new buffer were called "lfbuffer", for example, then you would add this code after the int 0x80
line in your existing loop:
mov byte [lfbuffer], 10 ; Moves the a line feed to the buffer pointed to by lfbuffer.
mov eax, 4 ; Moves 4 into eax, i.e. selects the print system call.
mov ebx, 1 ; Moves 1 into ebx, i.e. selects stdout as the destination.
mov ecx, lfbuffer ; Moves the address where your line feed is stored into ecx.
mov edx, 1 ; Moves the number of bytes to characters (1) into edx.
int 0x80 ; Executes a Linux system call using the above parameters.