c语言汇编

int main() {
    
short x=6;
    
short y=9;
    
short z;

    z 
= x+y;
        
return 0;
}

    .file    "CSCILab03-1.cpp"
; This 
is the input source file.  This will probably make it into the
; assembler output 
as some kind of debug record for later debugging.

    .text
    .align 
2
; .text 
is a section command (.data and .bss are others).  All program
; and constant data typically goes into .text.  Global initialised data
; should be 
in .data, and uninitialised globals should be in .bss

.globl main
    .type    main, @function
; Declare a 
global symbol, and set it's type to be a function.

main:
; main starts here :)

    leal    
4(%esp), %ecx       ; Save the original stack pointer
    andl    $
-16%esp          ; -16 is 0xFFFFFFF0, which clears the bottom
                                ; 
4 bits of the stack pointer (esp).  The effect
                                ; of 
this is to ensure the stack remains 16-byte
                                ; aligned 
for the most efficient access to any
                                ; data type;
    pushl   
-4(%ecx)            ; push the original stack pointer
    ; These first 
3 instructions are only something you will see in main()
    ; Put the same code into another function, and it will just begin with
    ; the saving and setting up of ebp.

    pushl   
%ebp                ; Save original base pointer (ebp)
    movl    
%esp, %ebp          ; Establish a new base pointer where the stack is now.
    pushl   
%ecx                ; Save it
    subl    $
16%esp           ; Allocate some space for local variables.

    movw    $
6-10(%ebp)       ; short x=6;
    movw    $
9-8(%ebp)        ; short y=9;
    movzwl  
-10(%ebp), %edx     ; Move (short)x into edx, and clear the MSW
    movzwl  
-8(%ebp), %eax      ; Move (short)y into eax, and clear the MSW
    leal    (
%edx,%eax), %eax   ; one of many ways of performing an addition.
    movw    
%ax, -6(%ebp)       ; Move (short)ax into z

    movl    $
0%eax            ; return 0; (well, putting 0 into the return register)

    addl    $
16%esp           ; remove the local variables
    popl    
%ecx                ; restore a register
    popl    
%ebp                ; restore another register

    leal    
-4(%ecx), %esp      ; restore the original stack pointer
                                ; 
this is another 'main only' step, see the start

    ret                         ; Adiós amigo

.LFE2:
    .size    main, .
-main
; Some 
internal symbol which indicates how many bytes the main function
; occupies.

.globl __gxx_personality_v0
; gxx_personality 
is something which g++ emits, for what, I don't know.

    .ident    
"GCC: (GNU) 4.1.1 20070105 (Red Hat 4.1.1-52)"
; More identification of what generated 
this assembly code.

    .section    .note.GNU
-stack,"",@progbits
; Dunno what 
this is for.

.file 
"CSCILab03-1.cpp" ; This is the input source file. This will probably make it into the ; assembler output as some kind of debug record for later debugging. .text .align 2 ; .text is a section command (.data and .bss are others). All program ; and constant data typically goes into .text. Global initialised data ; should be in .data, and uninitialised globals should be in .bss .globl main .type main, @function ; Declare a global symbol, and set it's type to be a function. main: ; main starts here :) leal 4(%esp), %ecx ; Save the original stack pointer andl $-16, %esp ; -16 is 0xFFFFFFF0, which clears the bottom ; 4 bits of the stack pointer (esp). The effect ; of this is to ensure the stack remains 16-byte ; aligned for the most efficient access to any ; data type; pushl -4(%ecx) ; push the original stack pointer ; These first 3 instructions are only something you will see in main() ; Put the same code into another function, and it will just begin with ; the saving and setting up of ebp. pushl %ebp ; Save original base pointer (ebp) movl %esp, %ebp ; Establish a new base pointer where the stack is now. pushl %ecx ; Save it subl $16, %esp ; Allocate some space for local variables. movw $6, -10(%ebp) ; short x=6; movw $9, -8(%ebp) ; short y=9; movzwl -10(%ebp), %edx ; Move (short)x into edx, and clear the MSW movzwl -8(%ebp), %eax ; Move (short)y into eax, and clear the MSW leal (%edx,%eax), %eax ; one of many ways of performing an addition. movw %ax, -6(%ebp) ; Move (short)ax into z movl $0, %eax ; return 0; (well, putting 0 into the return register) addl $16, %esp ; remove the local variables popl %ecx ; restore a register popl %ebp ; restore another register leal -4(%ecx), %esp ; restore the original stack pointer ; this is another 'main only' step, see the start ret ; Adiós amigo .LFE2: .size main, .-main ; Some internal symbol which indicates how many bytes the main function ; occupies. .globl __gxx_personality_v0 ; gxx_personality is something which g++ emits, for what, I don't know. .ident "GCC: (GNU) 4.1.1 20070105 (Red Hat 4.1.1-52)" ; More identification of what generated this assembly code. .section .note.GNU-stack,"",@progbits ; Dunno what this is for.

The 
%ebp is the base pointer, also known as the stack frame pointer. It is a fixed register within the scope of a single function.
All local variables (a negative offset) and parameters (a positive offset) are accessed relative to the 
%ebp established at the start of the function.

Since 
4(%ebp) is the previous frame pointer, you can use this (as debuggers do) to walk up the stack to examine the state of any function in the current call hierarchy. The 'bt' command in GDB will use this chain for example.
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值