Write an assembly language program that sorts a list of date
Write an assembly language program that sorts a list of dates and then prints out the dates sorted oldest to newest.
The dates are in the format of DD- MMM-YYYY, e.g. ?23-JUL-2010?.
Define and implement the following as procedures or macros (your choice) and use them to accomplish this task:
DateToNumber
Parameter is the address of a string.
Turns the string form of the date, DD-MMM-YYY, into a unique 32-bit number.
The date numbers are unsigned and sort the same as the dates they represent.
For example, if 23-JUL-2010 is the number 42, then 24-JUL-2010 will be bigger than 42 and 22-JUL-2010 will be smaller than 42.
Returns the 32-bit date number.
NumberToDate
Parameter is a 32-bit unsigned number.
Turns the number back into its corresponding date string.
Returns the address of the string.
BubbleSort
Parameters are the address of an array of 32-bit unsigned numbers and the length of the array.
Sorts the array in place from smallest to largest.
No return value.
Some starting code (MASM32)
include \\masm32\\include\\masm32rt.inc
.data
dates BYTE \"23-JUL-2010\", 0, \"23-JAN-2010\", 0, \"23-JUL-2009\", 0, \"31-JUL-2012\", 0, \"05-MAR-2010\", 0
nDates DWORD 5
.code
start:
lea EAX, dates ; create pointer to beginning of dates array
mov ECX, nDates ; set up loop counter
loop_top:
push EAX ; save EAX and ECX because print macros change these
push ECX
print EAX ; print date at pointer location
print chr$(13,10)
pop ECX
pop EAX
add EAX, 12 ; increment pointer, note that date has 12
; bytes including null-terminator
loop loop_top ; continue loop
exit
end start
PLEASE PROVIDE A FULL WORKING SOLUTION, THANK YOU!
Solution
Note: procedure is used to prepare the solution
Solution:
include \\masm32\\include\\masm32rt.inc
.data
dates BYTE \"23-JUL-2010\", 0, \"23-JAN-2010\", 0, \"23-JUL-2009\", 0, \"31-JUL-2012\", 0, \"05-MAR-2010\", 0
nDates DWORD 5
.code
bubbleSort Proc
mov edx,@dates
mov ds,edx
mov cl,len
loop1:
mov eax,ebx
lea si,arr
loop2:
mov al,[si]
inc si
cmp [si],al
jb loop3
xchg [si],al
mov [si-1],al
loop3:
dec eax
jnz loop2
loop loop1
mov EBP,4ch
int 21h
start:
lea EAX, dates ; create pointer to beginning of dates array
mov ECX, nDates ; set up loop counter
call bubbleSort
loop_top:
push EAX ; save EAX and ECX because print macros change these
push ECX
print EAX ; print date at pointer location
print chr$(13,10)
pop ECX
pop EAX
add EAX, 12 ; increment pointer, note that date has 12
; bytes including null-terminator
loop loop_top ; continue loop
inkey ;Keep the window open
exit
end start


