I’ve been recently working towards learning a bit of assembly and I’m currently stumped on an exercise which requires me to find the maximum number of a list of long values.
The code is as follows:
.section .data
data_items: .long 200, 201, 101, 10, 0
min_val: .long 0x8000000000000000 # MIN_VALUE in long
.section .text
.global _start
_start:
movl $0, %edi # init counter to 0
movl min_val, %ebx
start_loop:
cmpl $0, %eax
je loop_exit # go to end if 0 encountered
incl %edi
movl data_items(,%edi,4), %eax
cmpl %ebx, %eax
jle start_loop # if new value < max value in ebx, read next element
movl %eax, %ebx
jmp start_loop
loop_exit:
movl $1, %eax
int $0x80
Two problems with this code:
- When trying to assemble the code, I get the message: Warning: value 0x8000000000000000 truncated to 0x0
- If I rewrite my code in an alternative logic (one which doesn’t require min_value variable), any value greater than 255 in the list of data_items is truncated or returned as value % 256 even though the range of
.longshould be much larger?
Can anyone help me understand what I’m doing wrong?
EDIT: After changes, the code looks like below. Note how maximum in this case turns out to be 145 instead of 401.
.section .data
data_items: .long 401, 201, 101, 10, 0
max_val: .long 0x80000000
.section .text
.global _start
_start:
movl $0, %edi # init counter to 0
movl max_val, %ebx
start_loop:
movl data_items(,%edi,4), %eax
cmpl $0, %eax
je loop_exit # go to end if 0 encountered
incl %edi
cmpl %ebx, %eax
jle start_loop # if new value < max value in ebx, read next element
movl %eax, %ebx
jmp start_loop
loop_exit:
movl $1, %eax
int $0x80
First of all, 0x8000000000000000 doesn’t even fit in a long, it’s a long long. A long -1 is 0xffffffff.
As for the other point, I can’t comment on code you haven’t posted.