Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

what does this code do? please help.

Could someone please explain what this code does and where it does what?
having some problems here.. thank you.

#################
function:
pushl %ebp
movl %esp, %ebp
subl $56, %esp
subl $12, %esp
leal -40(%ebp), %eax
pushl %eax
call gets
addl $16, %esp
movl $0, -12(%ebp)
.L2:
cmpl $5, -12(%ebp)
jle .L5
jmp .L3
.L5:
leal -40(%ebp), %eax
addl -12(%ebp), %eax
cmpb $82, (%eax)
jne .L4
movl $82, -44(%ebp)
jmp .L1
.L4:
leal -12(%ebp), %eax
incl (%eax)
jmp .L2
.L3:
movsbl -40(%ebp),%eax
movl %eax, -44(%ebp)
.L1:
movl -44(%ebp), %eax
leave
ret

main:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
andl $-16, %esp
movl $0, %eax
subl %eax, %esp
subl $12, %esp
pushl $.LC0
call printf
addl $16, %esp
subl $8, %esp
subl $4, %esp
pushl $0
call function
addl $8, %esp
movsbl %al,%eax
pushl %eax
pushl $.LC1
call printf
addl $16, %esp
leave
ret


############

Comments

  • blipblip Member Posts: 756
    My guess is that this is the assembly output of a C(++) compiler. I'm not too great at reading AT&T assembly, so I translated it into Intel's "version" (without the "ptr" after the data types though). Perhaps this translation is what you needed? One can just trace the code to find out how it operates, but it will take some time.

    Note that LEA is used to do basic arithmetic. An example would be LEA EAX,[EBP-44] which would perform EAX = EBP - 44. I think LEAVE performs the equivalent of MOV ESP, EBP / POP EBP.

    Functions IIRC give their return value in the accumulator register, probably the whole of EAX in this case. It appears as if the results from one function are passed into another, as evidenced by a large ADD then subsequent SUBs that add up to something being left on the stack for the next one. I'm stuck on the parts where it says PUSH DWORD .LC0 and PUSH DWORD .LC1, my guess being that those are constants containing the number of parameters that printf takes for those calls. Is that enough information, or do you think you need further help?

    [code]
    function:
    push ebp
    mov ebp,esp
    sub esp,56
    sub esp,12
    lea eax,[ebp-40]
    push eax
    call gets
    add esp,16
    mov dword [ebp-12],0

    .L2:
    cmp dword [ebp-12],5
    jle .L5
    jmp .L3

    .L5:
    lea eax,[ebp-40]
    add eax,[ebp-12]
    cmp byte [eax],82
    jne .L4
    mov [ebp-44],82
    jmp .L1

    .L4:
    lea eax,[ebp-12]
    inc eax
    jmp .L2

    .L3:
    movsx eax,byte [ebp-40]
    mov [ebp-44],eax

    .L1:
    mov eax,[ebp-44]
    leave
    ret

    main:
    push ebp
    mov ebp,esp
    sub esp,8
    and esp,-16
    mov eax,0
    sub esp,eax
    sub esp,12
    push dword .LC0
    call printf
    add esp,16
    sub esp,8
    sub esp,4
    push dword 0
    call function
    add esp,8
    movsx eax,al
    push eax
    push .LC1
    call printf
    add esp,16
    leave
    ret
    [/code]
Sign In or Register to comment.