I'm a beginner trying to create a program that sorts 3 integers in ascending order in assembly 32-bit. I try to cmp
the value stored in the variable inputs directly but it doesn't work so I try to mov
the input value into the registers and then cmp
the registers instead but that still doesn't work. No matter what the values in input variables are, the flags were never set.
Can someone please explain how do I compare these values or any other way to sort those 3 inputs?
%include 'functions.asm'
SECTION .data ; To store message
msg1 db 'Please enter your first integer: ', ;0h
lenmsg1 equ $-msg1
msg2 db 'Please enter your second integer: ', ;0h
lenmsg2 equ $-msg2
msg3 db 'Please enter your third integer: ', ;0h
lenmsg3 equ $-msg3
msg4 db 'The sorted input in ascending order is:', ;0h
lenmsg4 equ $-msg4
SECTION .bss ; To store variable
; Storing variable sinput and reserve space for 5 bytes (numeric, 1 for sign)
input1 resb 5
input2 resb 5
input3 resb 5
SECTION .text
global _start
_start:
mov eax, 4
mov ebx, 1
mov ecx, msg1
mov edx, lenmsg1
int 80h
mov edx, 5 ; number of bytes to read
mov ecx, input1 ; reserved space to store our input (known as a buffer)
mov ebx, 2 ;
mov eax, 3 ; invoke SYS_READ (kernel opcode 3)
int 80h
mov eax, 4
mov ebx, 1
mov ecx, msg2
mov edx, lenmsg2
int 80h
mov edx, 5 ; number of bytes to read
mov ecx, input2 ; reserved space to store our input (known as a buffer)
mov ebx, 2 ;
mov eax, 3 ; invoke SYS_READ (kernel opcode 3)
int 80h
mov eax, 4
mov ebx, 1
mov ecx, msg3
mov edx, lenmsg3
int 80h
mov edx, 5 ; number of bytes to read
mov ecx, input3 ; reserved space to store our input (known as a buffer)
mov ebx, 2 ;
mov eax, 3 ; invoke SYS_READ (kernel opcode 3)
int 80h
jmp ascend_sort
continue:
; result printing part which I haven't implement yet
call quit
ascend_sort:
mov eax, input1
mov ebx, input2
mov ecx, input3
cmp eax, ecx
jg swap1_3
cmp eax, ebx
jg swap1_2
cmp ebx, ecx
jg swap2_3
jmp continue
swap1_3:
push eax
mov eax, ecx
pop ecx
jmp ascend_sort
swap1_2:
push eax
mov eax, ebx
pop ebx
jmp ascend_sort
swap2_3:
push ebx
mov ecx, ebx
pop ecx
jmp ascend_sort