Sie sind auf Seite 1von 3

www.VUSR.

net
CS401- Computer Architecture and Assembly Language
Programming
Assignment # 2
Spring 2009
Total Marks: 20
Solution:
Program code:

; program to sort the numbers, using bubble sort, in signed descending order

[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 flag to no swaps
SR
loop1: mov ax, [data+bx] ; load number in ax
cmp ax, [data+bx+2] ; compare with next number
jge noswap ; no swap if already in order
mov dx, [data+bx+2] ; load second element in dx
U

mov [data+bx+2], ax ; store first number in second


mov [data+bx], dx ; store second number in first
; flag that a swap has been done
V

mov byte [swap], 1

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

Page 1 of 3

http://lms.vusr.net
www.VUSR.net
Screen-shots:

As the program starts execution, the unsorted data is loaded in memory (M1
window) as shown in the red boundary below:

SR
As negative numbers are stored in two’s compliment form:
To take two’s compliment of a number, following are the steps:
U

Step1: Take 1 compliment of the number (without sign)


Step2: Add 1 to the one’s compliment (calculated in step 1)
So:
V

First element is -10 (decimal):


As we have defined it of size word (with the help of dw), the value 10 can be
represented in binary as:
0000000000001010 (binary)
Taking one’s compliment (simply change 0 to 1, and 1 to 0):
1111111111110101
Adding 1 to the one’s compliment:
1111111111110110 (binary)
FFF6 (hexadecimal)
[Note that this number is also equal to 65526(in decimal) when treated as unsigned
number]

In little-Endian notation, it will be represented as:


F6FF (as shown in the screen-shot above)

Page 2 of 3

http://lms.vusr.net
www.VUSR.net

After Executing the program, the data is now sorted in signed descending order:

SR
So, now the data is in signed descending order:

50(0032), 45(002D), 40(0028), 35(0023), 20(0014), 15(000F), 0(0000), -10(FFF6), -25(FFE7),


-30(FFE2)
U
V

Page 3 of 3

http://lms.vusr.net

Das könnte Ihnen auch gefallen