Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In NASM: I am trying to sort the array in ascending order, but I have in the pro

ID: 3807721 • Letter: I

Question

In NASM:

I am trying to sort the array in ascending order, but I have in the program using bubble sort.

I am getting this :

instead of this:

Here is my code below:

;;;;;;;;;;;;;;;;;;;;   MACRO DEFINITIONS   ;;;;;;;;;;;;;;;;;;;;

; A macro with two parameters

; Implements the write system call

%macro writestring 2

mov eax, 4 ;sys_write system call number

mov ebx, 1 ;file descriptor std_out

mov ecx, %1 ;message to write from parameter 1

mov edx, %2 ;message length from parameter 2

int 0x80

%endmacro

;;;;;;;;;;;;;;;;;;;;   DATA SEGMENT   ;;;;;;;;;;;;;;;;;;;;

section   .data

msg1 db 'Before sorting: '

lenmsg1 equ $-msg1

msg2 db 'After sorting: '

lenmsg2 equ $-msg2

asciinums db '7','3','2','1','0','5','6','4','8','9'

lenasciinums equ $-asciinums

crlf db 0x0d, 0x0a

lencrlf   equ   $ - crlf

swap: db 0

section   .text

global _start

_start:

writestring msg1, lenmsg1

writestring asciinums, lenasciinums

writestring crlf, lencrlf

outer:

mov eax, 10

mov ebx, 0

mov esi, asciinums

inner:

mov al, [esi+1]

mov ah, [esi]

inc esi

cmp ah, [esi]

jl skip

mov [esi], ah

mov [esi-1], al

skip:

dec eax

cmp eax, ebx

jl inner

loop outer

writestring msg2, lenmsg2

writestring asciinums, lenasciinums

writestring crlf, lencrlf

mov   eax, 1           ;terminate program

int   0x80

Before sorting: 7321056489 After sorting 3721056489

Explanation / Answer

section .data
msg1 db 'Before sorting: '
lenmsg1 equ $-msg1
msg2 db 'After sorting: '
lenmsg2 equ $-msg2
asciinums db '7','3','2','1','0','5','6','4','8','9'
lenasciinums equ $-asciinums
crlf db 0x0d, 0x0a
lencrlf equ $ - crlf
swap: db 0

section .text
global _start
_start:
writestring msg1, lenmsg1
writestring asciinums, lenasciinums
writestring crlf, lencrlf
Begin:
mov ax, data
mov ds, ax
mov es, ax
mov bx, OFFSET arr1
mov cx, 5
mov dx, cx
L1:
mov si, 0
mov ax, si
inc ax
mov di, ax
mov dx, cx
L2:
mov al, [bx][si]
cmp al, [bx][di]
jg L4
L3:
inc si
inc di
dec dx
cmp dx, 00
je L1
jg L2
L4:
mov al, [bx][si]
mov ah, [bx][di]
mov [bx][si], ah
mov [bx][di], al
inc si
inc di   
dec dx
cmp dx, 00
je L1
jg L2
Exit:
mov ax, 4c00h
int 21h
Code Ends
End Begin

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote