0

I'm a new to assembly and have no idea why It does not push %EAX after end_while: instruction. I have to provide the implementation of the function with an example of a call to set the value of the largest 32-bit integer values in the array. A copy of the array (the values of all elements) and the result (the largest value) are passed through the stack.

.globl _start
.data
tab: .long 2, 100, 14, 3, 4, 2, 7 
end_tab:
    .equ tab_byte, end_tab - tab
    .equ tab_size, tab_byte/4
.text
prog:
    mov 4(%ESP), %ECX
    mov 8(%ESP), %EBX
    dec %ECX
    push (%EBX, %ECX, 4)
    mov (%EBX, %ECX, 4), %EAX
while:
    dec %ECX
    cmp $0, %ECX 
    jl end_while
    push (%EBX, %ECX, 4)
    cmp (%EBX, %ECX, 4), %EAX
    jl change
    jmp while 
change:
    mov (%EBX,%ECX,4), %EAX
    jmp while
end_while:
    push %EAX
    ret 
_start:
    push $tab
    push $tab_size  
    call prog
    nop

As a result I don't get what I expect from this stack meaning that I want to get the largest value following the last element on the stack.

0xffffd0e8:     2       100     14      3
0xffffd0f8:     4       2       7       134516786
0xffffd108:     7       134520832       1       -11565
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • 1
    `ret` after `push` seems to be wrong. Note that `ret` is like `pop eip`. – ecm Jan 14 '22 at 14:24
  • 1
    Related: [What impact could extra push do to assembly program?](https://stackoverflow.com/q/67368320). Also Note 1 in this answer on [Tips for golfing in x86/x64 machine code](https://codegolf.stackexchange.com/a/165020) explains a possible calling convention that returns a size after pushing an array, by popping the return address to start with and jumping to it (or push/ret) *after* all the pushes. – Peter Cordes Jan 14 '22 at 15:05
  • Ok, but how to solve it? When I get rid of ret at all it seems to be working but then there are some addresses on stack: 0xff7fe000: 134520832 100 2 100 0xff7fe010: 14 3 4 2 0xff7fe020: 7 134516785 – Born Ok Jan 16 '22 at 07:21

0 Answers0