Fabio Paolini
Fabio Paolini

Reputation: 35

Find Maximum in Assembly code does not work for 64 bits register

I am following a book about Assembly for x86 called "Programming Ground UP" from Jonathan Bartlett. The examples are written to x86 architecture and I am trying to adapt to x64. Many examples fails to compile if written as x86 mainly because commands like push %eax are not allowed. However the original code to find a maximum in a list works since it has not push or pop commands. The original example is as below

.section .data
data_items:
#These are the data items
    .long 3,67,34,222,45,233,54,34,44,33,22,11,66,0
    .section .text
.globl _start
_start:
movl $0, %edi
# move 0 into the index register
movl data_items(,%edi,4), %eax # load the first byte of data
movl %eax, %ebx
# since this is the first item, %eax is
# the biggest
start_loop:
# start loop
cmpl $0, %eax
# check to see if we’ve hit the end
je loop_exit
incl %edi
# load next value
movl data_items(,%edi,4), %eax
cmpl %ebx, %eax
# compare values
jle start_loop
# jump to loop beginning if the new
# one isn’t bigger
movl %eax, %ebx
# move the value as the largest
    jmp start_loop

    loop_exit:
# %ebx is the return value, and it already has the number
movl $1, %eax
#1 is the exit() syscall
int $0x80

It works. Then I tried to adapt this code using x64 register as

    #Get the maximum from a list

    .section .data
data_items:
    .long 1,2,23,5,7,0

    .section .text
    .globl _start
_start:
    mov $0, %rdi
    mov data_items(,%rdi,4), %rax
    mov %rax, %rbx

start_loop:
    cmp $0, %rax
    je loop_exit
    inc %rdi
    mov data_items(,%rdi,4), %rax
    cmp %rbx, %rax
    jle start_loop
    mov %rax, %rbx
    jmp start_loop
loop_exit:
    #mov %rdi,%rbx
    mov $1, %rax
    int $0x80

but it does not work

I tried to change the mov command as mov data_items(,%rdi,8), %rax as I thought that the .long type could have 8 bytes long, but it did not work. I also tried to put %rdi as the return value to test if the total number of reading from the list was correct and actually it is, I mean, in this case the return value correspond to the size of the list.

I really do not know what can be wrong

Upvotes: 1

Views: 87

Answers (0)

Related Questions