I have been following an example of a loop that is in the following page:
https://www.tutorialspoint.com/assembly_programming/assembly_loops.htm
and the code is:
section .text
global _start ;must be declared for using gcc
_start: ;tell linker entry point
mov ecx,10
mov eax, '1'
l1:
mov [num], eax ;move eax to *num
mov eax, 4 ;invoke to sys_write
mov ebx, 1 ;data written to stdout
push ecx
mov ecx, num
mov edx, 1 ;number of bytes to show
int 0x80
mov eax, [num] ;move the value of *[num] into eax
sub eax, '0'
inc eax
add eax, '0'
pop ecx
loop l1
mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .bss
num resb 1
I tried to follow the program, but I cannot understand the use of an stack in this program. I have added some comments on the code, so feel free to correct me if I am wrong in some parts. For what I see the value of eax increments from one to one and then print this value. However, I do not know if the use of an stack here is needed, from the trace I made I see that the values of 1,2,.. and so on are pushed, but then are popped up almost immediately. So, why is needed an stack here?
An additional question is about the instruction sub and add that are given in the following instructions:
sub eax, '0'
add eax, '0'
Are these instructions to convert the number into a char or string?
Update
I have get rid of some instructions and pass the entire 32 registers to the 64 registers, so my program is as follows:
section .text
global _start ;must be declared for using gcc
_start: ;tell linker entry point
mov ecx,10
mov eax, '1'
l1:
mov [num], eax ;move eax value to [num] address
mov eax, 4 ;invoke to sys_write
mov ebx, 1 ;data is written to stdout
push rcx ;push whole cx register in 64 mode
mov ecx, num
mov edx, 1 ;number of bytes to show
int 80h
mov eax, [num] ;move the value of the address of [num] into eax
inc eax
pop rcx
loop l1
mov eax,1 ;system call number (sys_exit)
int 80h ;call kernel
section .bss
num resb 1
However, when I comment the stack section the following output is printed: