1

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

Sep Roland
  • 33,889
  • 7
  • 43
  • 76
Noxz
  • 11
  • 2
  • 1
    First of all you're only comparing the addresses, not the 4 bytes of ASCII characters. Use your debugger to look at register values. But if you were, x86 is little-endian so the ASCII digit at the lowest address (the first digit in printing / typing order, the most significant) is in the least-significant position. You'd want to BSWAP them before sorting their 4 ASCII digits as a binary integer. (Except you'd also have to normalize the position of the LSD so `9` doesn't compare greater than `8000`, if they're not all 4 digits long) – Peter Cordes Sep 09 '21 at 05:21
  • Of course the standard way to do things is to convert input ASCII strings of digits into binary integers that represent the value, and then convert back for printing, like scanf and printf would. – Peter Cordes Sep 09 '21 at 05:22
  • Thanks a lot, I'm new to assembly and this is extremely helpful – Noxz Sep 09 '21 at 05:31
  • 1
    Swapping is simpler with `xchg eax, ecx`... And this is an error: After any of the swaps, you should not reload the registers from the *input_*'s. Those didn't get swapped! – Sep Roland Sep 10 '21 at 00:46

0 Answers0