I have written a small c++ program and then output the instructions as assembly...but I have no clue what it means.

The book I am using does not explain this very well.

Can someone comment the code to tell me what is going on?

I'd greatly appreciate it.

Thanks

.file    "CSCILab03-1.cpp"
    .text
    .align 2
.globl main
    .type    main, @function
main:
.LFB2:
    leal    4(%esp), %ecx
.LCFI0:
    andl    $-16, %esp
    pushl    -4(%ecx)
.LCFI1:
    pushl    %ebp
.LCFI2:
    movl    %esp, %ebp
.LCFI3:
    pushl    %ecx
.LCFI4:
    subl    $16, %esp
.LCFI5:
    movw    $1, -10(%ebp)
    movw    $1, -8(%ebp)
    movzwl    -10(%ebp), %edx
    movzwl    -8(%ebp), %eax
    leal    (%edx,%eax), %eax
    movw    %ax, -6(%ebp)
    movl    $0, %eax
    addl    $16, %esp
    popl    %ecx
    popl    %ebp
    leal    -4(%ecx), %esp
    ret
.LFE2:
    .size    main, .-main
.globl __gxx_personality_v0
    .ident    "GCC: (GNU) 4.1.1 20070105 (Red Hat 4.1.1-52)"
    .section    .note.GNU-stack,"",@progbits

Can you post the corresponding C++ program as well.

Here is better code.

C++ Code

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

	z = x+y;
        return 0;
}

Assembly Code

.file	"CSCILab03-1.cpp"
	.text
	.align 2
.globl main
	.type	main, @function
main:
.LFB2:
	leal	4(%esp), %ecx
.LCFI0:
	andl	$-16, %esp
	pushl	-4(%ecx)
.LCFI1:
	pushl	%ebp
.LCFI2:
	movl	%esp, %ebp
.LCFI3:
	pushl	%ecx
.LCFI4:
	subl	$16, %esp
.LCFI5:
	movw	$6, -10(%ebp)
	movw	$9, -8(%ebp)
	movzwl	-10(%ebp), %edx
	movzwl	-8(%ebp), %eax
	leal	(%edx,%eax), %eax
	movw	%ax, -6(%ebp)
	movl	$0, %eax
	addl	$16, %esp
	popl	%ecx
	popl	%ebp
	leal	-4(%ecx), %esp
	ret
.LFE2:
	.size	main, .-main
.globl __gxx_personality_v0
	.ident	"GCC: (GNU) 4.1.1 20070105 (Red Hat 4.1.1-52)"
	.section	.note.GNU-stack,"",@progbits

Looks to me as if you are the unfortunate victim of a compiler which uses the AT&T syntax.

Here goes...

.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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.