1
I am trying to make a code in Assembly with Arq. x86 using NASM.
In this code I must return the difference between the highest value and the lowest value of an array, as below.
However, when running it is returning 14 to the example below and the correct one would be 18 (20-2). Could you help me find my error? Most grateful at once.
section .data
array: DB 2,4,6,8,10,12,14,16,18,20
size EQU $-array
section .text
global _start
_start:
mov eax,[array]
mov ebx,[array]
xor ecx,ecx
mov edx,size
jmp compara
maior:
mov eax,[array+ecx]
jmp volta_maior
menor:
mov ebx,[array+ecx]
jmp volta_menor
compara:
cmp ecx,edx
jnle fim
cmp [array+ecx],eax
jge maior
volta_maior:
cmp [array+ecx],ebx
jle menor
volta_menor:
inc ecx
jmp compara
fim:
sub eax,ebx
mov ebx,eax
mov eax,1
int 0x80
I was doing online for not having the nasm or other Assembler, and no getting, I think the problem is the online that has problem with IO.
– Maniero
Yes, the program does not have IO and the online compiler needs to display the result through the return code for the S.O. No Jdoodle it is possible to compile and view this return code :)
– Gomiero
I didn’t know that one, cool...
– Maniero
P.S.: I use https://tio.run/#Assembly-nasm to compile. It is very interesting and also allows to see the return!
– Léo Eduardo Silva
@Gomiero in your code in the "compare" function he does "Cmp [array+ecx],al" and if it is greater or equal he goes to "greater" and otherwise he goes on and does "Cmp [array+ecx],Bl", is that it? Thanks again!
– Léo Eduardo Silva
@Léoeduardosilva Yes, it’s the same question code. I just switched the registers
eax
andebx
foral
andbl
.– Gomiero
Got it. Thank you, @Gomiero. Thank you also for the explanation!
– Léo Eduardo Silva