Write Assembly language program to sort the following numbers,using bubble sort,
ID: 3610581 • Letter: W
Question
Write Assembly language program to sort the following numbers,using bubble sort, in signed descending order:
-10, -30, -25, 50, 15, 20, 35, 40, 45, 0
That is, at the end of your program, the numbers must be in thefollowing order:
50, 45, 40, 35, 20, 15, 0, -10, -25, -30
What to Submit:
Microsoft Word document containing:
Important:
Update the last column of the following entries of ConditionalJumps table given in
Topic 3.2.CONDITIONAL JUMPS in yourhandouts
JC
JB
JNAE
Jump if carry
Jump if below
Jump if not above or equal
CF = 1
The jump is taken if the last arithmetic operation generated acarry or required a borrow. After a CMP it is taken if the unsigneddestination is smaller than the unsignedsource.
JNC
JNB
JAE
Jump if not carry
Jump if not below
Jump if above or equal
CF = 0
This jump is taken if the last arithmetic operation did notgenerated a carry or required a borrow. After a CMP it is taken ifthe unsigned destination is larger or equalto the unsigned source.
JA
JNBE
Jump if above
Jump if not below or equal
ZF =0 AND CF = 0
This jump is taken after a CMP if the unsigneddestination is larger than the unsignedsource.
JNA
JBE
Jump if not above
Jump if below or equal
ZF = 1 OR CF = 1
This jump is taken after a CMP if the unsigneddestination is smaller than or equal to theunsigned source.
JL
JNGE
Jump if less
Jump if not greater or equal
SF OF
This jump is taken after a CMP if the signeddestination is smaller than the signedsource.
JNL
JGE
Jump if not less
Jump if greater or equal
SF = OF
This jump is taken after a CMP if the signeddestination is larger than or equal to thesigned source.
JG
JNLE
Jump if greater
Jump if not less or equal
ZF = 0 AND SF =OF
This jump is taken after a CMP if the signeddestination is larger than the signedsource.
JNG
JLE
Jump if not greater
Jump if less or equal
ZF = 1 OR SF OF
This jump is taken after a CMP if the signeddestination is smaller than or equal to thesigned source.
Additional Information:
As the program starts execution, the unsorted data is loaded inmemory (M1 window) as shown in the red boundary below:
As negative numbers are stored in two’s complimentform:
To take two’s compliment of a number, following are thesteps:
Step1: Take 1 compliment of the number (withoutsign)
Step2: Add 1 to the one’s compliment(calculated in step 1)
So:
First element is -10 (decimal):
As we have defined it of size word (with the help of dw), thevalue 10 can be represented in binary as:
0000000000001010(binary)
Taking one’s compliment (simply change 0 to 1, and 1 to0):
1111111111110101
Adding 1 to the one’s compliment:
1111111111110110(binary)
FFF6(hexadecimal)
[Note that this number is also equal to 65526(in decimal) whentreated as unsigned number]
In little-Endian notation, it will be represented as:
F6FF(as shown in the screen-shot above)
JC
JB
JNAE
Jump if carry
Jump if below
Jump if not above or equal
CF = 1
The jump is taken if the last arithmetic operation generated acarry or required a borrow. After a CMP it is taken if the unsigneddestination is smaller than the unsignedsource.
JNC
JNB
JAE
Jump if not carry
Jump if not below
Jump if above or equal
CF = 0
This jump is taken if the last arithmetic operation did notgenerated a carry or required a borrow. After a CMP it is taken ifthe unsigned destination is larger or equalto the unsigned source.
JA
JNBE
Jump if above
Jump if not below or equal
ZF =0 AND CF = 0
This jump is taken after a CMP if the unsigneddestination is larger than the unsignedsource.
JNA
JBE
Jump if not above
Jump if below or equal
ZF = 1 OR CF = 1
This jump is taken after a CMP if the unsigneddestination is smaller than or equal to theunsigned source.
JL
JNGE
Jump if less
Jump if not greater or equal
SF OF
This jump is taken after a CMP if the signeddestination is smaller than the signedsource.
JNL
JGE
Jump if not less
Jump if greater or equal
SF = OF
This jump is taken after a CMP if the signeddestination is larger than or equal to thesigned source.
JG
JNLE
Jump if greater
Jump if not less or equal
ZF = 0 AND SF =OF
This jump is taken after a CMP if the signeddestination is larger than the signedsource.
JNG
JLE
Jump if not greater
Jump if less or equal
ZF = 1 OR SF OF
This jump is taken after a CMP if the signeddestination is smaller than or equal to thesigned source.
Explanation / Answer
; sorting a list of tennumbers using bubble sort
[org 0x0100]
jmp start
data: dw -10, -30, -25, 50, 15, 20, 35, 40, 45, 0
swap: db 0
start: mov bx,0 ; initialize array index to zero
mov byte [swap],0 ; rest swap flagto no swaps
loop1: mov ax,[data+bx] ; load number in ax
cmp [data+bx+2], ax ; comparewith next number
jle noswap ; no swap if already in order
mov dx, [data+bx+2] ; load secondelement in dx
mov [data+bx+2], ax ; store firstnumber in second
mov [data+bx],dx ; store secondnumber in first
mov byte [swap], 1 ; flag that a swap has been done
noswap: add bx,2 ; advance bx to next index
cmp bx,18 ; are we at last index
jne loop1 ; if not compare next two
cmp byte [swap], 1 ; check if a swap has been done
je start ; if yes make another pass
mov ax,0x4c00 ; terminate program
int 0x21
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.