Comment in the code segment ";"
The Assembly Code is as follows:
Dp @ dp :~ % Cat test1.s
. Section. rodata
Output:
. Asciz "the number is % d \ n"
. Section. data
Value:
. Int 0, 0, 0, 0, 0, 0, 0, 0
. Section. text
. Globl main
Main:
Movl $0, % ecx
Loopset:
Base Address (the offset address in the register exists, the data element index in the register exists, and the data element size in the register exists)
Movl % ecx, value (, % ecx, 4)
Inc % ecx
Cmpl $10, % ecx
Jne loopset
Movl $0, % ecx
Loopprint:
Movl value (, % ecx, 4), % eax
Pushl % ecx
Pushl % eax
Pushl $ output
Call printf
Add $8, % esp
Popl % ecx
Inc % ecx
Cmpl $10, % ecx
Jne loopprint
Compile and run
Dp @ dp :~ % Gcc-o test1 test1.s
Dp @ dp :~ %./Test1
The number is 0
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
The number is 6
The number is 7
The number is 8
The number is 9
All contents of the good AI Park blog is original, if reproduced please indicate the source http://blog.csdn.net/myhaspl/
The above code first goes to. in the data read/write area, place 10 int values whose initial values are 0. Then, use the loopset mark in the program and the jne statement to complete a loop and assign values to 0 to 9 in sequence, finally, the loopprint mark and jne statement are used to complete the loop, and the print Statement of library c is called to output these numbers.
Inc indicates auto-increment 1, while dec indicates auto-increment 1.
The stack increases from the high address to the low address. When the stack increases content, the top pointer of the stack decreases. Otherwise, the top pointer of the stack increases.
After the print statement is returned, clear the garbage generated by the print execution in the stack, add $8, % esp, discard the parameters prepared for calling the printf function, and locate the stack with the ESP register at the top, the stack pushing operation reduces the stack top address, and the pop-up operation increases the stack top address, so it is equivalent to directly popping up two % eax (pointing to the data to be output) and $ output parameters, which are called before:
Pushl % eax
Pushl $ output
Before the above two pressure stacks
Pushl % ecx
The function is to save the value of % ecx, because the printf function of library c will destroy the value of % ecx register. Generally, the stack is used to save the value that may be damaged, after printf is executed, restore: popl % ecx