0

I'm trying to get this assembly code program to compare three integers and return the largest variable, I believe this code should work however my print is giving me integers 6 and 9. The three variables are set to be 3, 6, and 9 in ASCII

Here's my code:

section .data

str: db 'Hello world!', 0Ah
var1: dw 51 
var2: dw 54
var3: dw 57
result: dw 00h

section .text

global _start

_start:

       mov eax, dword [var1]
       mov ebx, dword [var2]

       cmp eax, ebx

       jge nextstate

       mov dword [result], ebx

       jmp print


nextstate:

       mov eax, dword [var1]
       mov ebx, dword [var3]

       cmp eax, ebx

       jge finalstate

       mov dword [result], ebx

finalstate:

       mov dword [result], eax

print:

       mov eax, 4
       mov ebx, 1
       mov ecx, result
       mov edx, 13
       int 80h

       mov eax, 1
       mov ebx, 0
       int 80h
zx485
  • 28,498
  • 28
  • 50
  • 59
  • 1
    One critical problem of your code is that you confused `dw` with DWORD. `dw` does mean _**d**ata **w**ord_ (2 bytes) (or so), but DWORD means _**d**ata **d**ouble word_ (4 bytes). So you are reading the wrong values (overlapping), and also write the wrong `result` value. So either change your variables to DWORD size by using `variable dd xxx` instead of `dw`, or change your references to `movzx eax, word [var1]` (this reads the word at location var1 and zero extends it to 32bits). – zx485 Mar 24 '20 at 20:42
  • @zx485 I'm a little confused this code worked when our professor used it for 2 variable i tried to implement it for three. The two variable code our professor used is : – Mike Guarino Mar 24 '20 at 20:56
  • section .data str: db 'Hello world!', 0Ah var1: dw 48 var2: dw 50 result: dw 00h section .text global _start _start: mov eax, dword [var1] mov ebx, dword [var2] cmp eax, ebx jge nextstate mov dword [result], ebx jmp print nextstate: mov dword [result], eax print: mov eax, 4 mov ebx, 1 mov ecx, result mov edx, 13 int 80h mov eax, 1 mov ebx, 0 int 80h – Mike Guarino Mar 24 '20 at 20:56
  • 1
    Then your professor is probably wrong. See [here](https://stackoverflow.com/q/10168743/1305969). – zx485 Mar 24 '20 at 21:00
  • My specific error is that when i print result after moving eax to it it is till holding two values instead of one – Mike Guarino Mar 24 '20 at 21:05
  • You have to convert the value `result` to an ASCII string before outptting it with SYS_WRITE. But this is a different topic, see [here](https://stackoverflow.com/search?q=%5Bx86%5D+convert+integer+to+ascii). – zx485 Mar 24 '20 at 21:10
  • If you want to load from a `dw` into EAX, use `movzx eax, word [var1]`. IDK why you'd use 2 bytes of static storage to hold a char value, though; it's bigger than needed for a char, and still not as big as a register so you can't use it as a memory source operand for dword operations. In C it's like `unsigned short var1 = '3';` And BTW, you should just use `db '3'` in your asm source. – Peter Cordes Mar 25 '20 at 00:30

0 Answers0