0

Can someone help me find what is wrong with the following program? I'm reading 'Programming from the Ground Up' and attempting to translate the examples into x86-64 assembly. The following program finds the largest number in a set of data. But when I assemble, link, and run it I get 0. Which is obviously not the largest number. It runs fine with 32-bit registers/instructions, but not 64-bit.

# PURPOSE: This program finds the largest value in a set of data. # # # VARIABLES: %rax holds the current value. %rdi holds the largest # value. %rbx holds the current index. data_items is the # actual set of data. The data is terminated with a 0. # .section .data data_items: .long 76, 38, 10, 93, 156, 19, 73, 84, 109, 12, 21, 0 .section .text .globl _start _start: movq $0, %rbx movq data_items(, %rbx, 4), %rax movq %rax, %rdi loop_start: cmpq $0, %rax # Have we reached the end? je loop_end incq %rbx # Increment the index. movq data_items(, %rbx, 4), %rax # Load the next value. cmpq %rdi, %rax # Is new value larger? jle loop_start movq %rax, %rdi # New val is larger, store # it. jmp loop_start loop_end: # The largest value is already in %rdi and will be returned as # exit status code. movq $60, %rax syscall 

1 Answer 1

1

You are using movq which moves 64-bit values from your list that contains 32-bit values. That will give you wrong results. Define your list to hold 64-bit values instead: replace .long with .quad and replace 4 with 8 in the movs.

Sign up to request clarification or add additional context in comments.

1 Comment

Okay, thanks. I thought that might be the problem, but I didn't know what the directive for 64-bit values was. Is there any advantage to using movq instead of movl with .long values in this situation?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.