samedi 25 septembre 2021

x86 Using .IF , .ELSE , and .ELSEIF to compare two numbers

aI am given two DWORD values and will compare both of them using .IF, .ELSE, ELSEIF to see whether which number is larger or if they are both equal. For example, two prompts are called to screen, which are Enter Number 1 and Enter Number 2. Number 1 and Number 2 are stored into seperate registers using eax for num1 and ecx for num2. IF eax and ecx are equal, the equal prompt is called. If they aren't, then here's where .ELSEIF comes in. Eax is compared to ecx and vice versa.

The only problem is that number two is always larger than the first value, if they are not equal.

include asmlib.inc
.data

Prompt  BYTE    " Enter a number ", 0     ;Type number 1
Prompt2 BYTE    " Enter another number ", 0 ;Type number 2
Large   BYTE    " Is larger ", 0 ;Larger number output
Equal   BYTE    " Is equal", 0  ;Numbers are equal output
num1    DWORD ? ;Number 1 is num1
num2    DWORD ? ;Number 2 is num2

.code
main PROC

mov edx, OFFSET Prompt ;Enter first number
call writeLine
call readInt
mov num1, eax
endl

mov edx, OFFSET Prompt2 ;Enter second num
call writeLine
call readInt
mov num2, ecx
endl

.IF  eax == ecx
mov edx, OFFSET Equal   ;display Equal output
call writeString    ;display line
.ENDIF 

.IF ecx > eax && eax < ecx
mov ecx, num2
call    writeInt
mov edx, OFFSET Large
call    writeString

.ELSEIF ecx < eax && eax > ecx
mov eax, num1
call    writeInt
mov edx, OFFSET Large
call    writeString
.ENDIF

exit
main ENDP
end main

Aucun commentaire:

Enregistrer un commentaire