You are on page 1of 5

.

ORIG x3000
LEA R0, Intro
PUTS
LD R5, Base ;x3500 x3501 ... hold the input character
LD R4, Base1 ;R4 count the number of characters in each
string
;--------------------------------MAIN PROGRAM----------------------------------;
INPUT GETC
OUT
ADD R1, R0, #-9
BRz NEXT ;end input if press 'TAB' "-9"
ADD R1, R0, #-16
ADD R1, R1, #-16
BRz INPUT ;Ignore 'SPACE' "-32"
ADD R1, R1, #-12
BRz INPUT ;Ignore ',' "-44"
ADD R1, R0, #-10
BRz NEXT ;If 'ENTER' "-10" => end string
STR R0, R5, #0 ;store valid character in x3500 ...
ADD R5, R5, #1
ADD R2, R2, #1 ;count the number of character
BR INPUT

NEXT STR R2, R4, #0


AND R2, R2, #0
ADD R4, R4, #1 ;store number of character then jump to next
mem location (next string)
ADD R3, R3, #1 ;count the number of strings
ADD R1, R0, #-9
BRz DONE
BR INPUT

DONE LEA R0, Intro1


PUTS
ST R3, SaveString ;Save the number of strings
GETC
ADD R1, R0, #-10
BRz AS
LEA R0, IntroD
PUTS
JSR DESCEND
HALT
AS LEA R0, IntroA
PUTS
JSR ASCEND
HALT
;----------------------------------------------------------------------;
Intro .STRINGZ "Input strings of characters. 'ENTER' next string. 'TAB' to
stop.\n"
Intro1 .STRINGZ "\nENTER for ascend, others for descend\n"
IntroD .STRINGZ "Descending order:\n"
IntroA .STRINGZ "Ascending order:\n"
Base .FILL x3500
Base1 .FILL x4500
SaveString .FILL 0
SaveR7 .BLKW 1
HOME .FILL #127
;----------------------------------ASCEND
SUBROUTINE------------------------------------;
ASCEND ST R7, SaveR7
A_BEGIN LD R6, HOME
LD R5, Base
LD R4, Base1
A_LOOP LDR R0, R5, #0
BRz A_DONE1 ;if there is nothing in mem location =>
start to output strings
ADD R1, R0, #-5 ;if 5 means that this is the already displayed
character
BRz A_NEW_S
NOT R1, R0
ADD R1, R1, #1 ;R6 + R1(-R0)
ADD R3, R1, R6 ;R6 is the character of current highest string
BRz A_EQUAL ;if equal then check the next character
in 2 strings
BRp A_NEW_MIN ;R6 - R1 > 0
BRn A_NEW_S
;----------------------------------------------------------;
A_EQUAL ST R4, SaveR4_BACKUP ;store R4, R5 in back-up location to save
data
ST R5, SaveR5_BACKUP
LD R3, SaveR4 ;R3 contains the number of character in old
address
LD R2, SaveR5 ;R3 contains the character in old address
LDR R1, R5, #0 ;R1 is CURRENT VALUE
LDR R0, R2, #0 ;R0 is OLD VALUE
LDR R2, R2, #0
LDR R3, R3, #0
ADD R5, R5, #1
ADD R4, R4, #1
NOT R1, R1
ADD R1, R1, #1
ADD R1, R1, R0 ;R0 - R1 (OLD - CURRENT)
BRn A_CASE3
BRz A_CASE2
BRp A_CASE1

A_CASE3 LD R4, SaveR4 ;dont change


LD R5, SaveR5
BR A_NEW_S

A_CASE2 LDR R1, R5,#0 ;R1 new value


LDR R0, R2,#0 ;R0 stored value
ADD R5, R5, #1 ;point to the next character in CURRENT STRING
ADD R2, R2, #1 ;point to the next character in CURRENT STRING
ADD R3, R3, #-1 ;check whehter there is any character left in
the old address
BRz A_CASE3
ADD R4, R4, #-1 ;check whehter there is any character left in
the current address
BRz A_CASE1
NOT R1, R1
ADD R1, R1, #1
ADD R6, R1, R0 ;R0 - R1 (OLD - CURRENT)
BRn A_CASE3
BRz A_CASE2
BRp A_CASE1

A_CASE1 LD R4, SaveR4_BACKUP


LD R5, SaveR5_BACKUP
ST R4, SaveR4
ST R4, SaveR5
BR A_NEW_S
;----------------------------------------------------------;
A_NEW_S LDR R3, R4, #0
ADD R4, R4, #1
ADD R5, R5, R3
BR A_LOOP

A_NEW_MIN ADD R6, R0, #0 ;MIN <- current character of current string
ST R5, SaveR5 ;SaveR5 save the current address of lowest
string
ST R4, SaveR4 ;SaveR4 save the current address of the number
of cha
BR A_NEW_S
;----------------------------------------------------------;
A_DONE1 LD R2, SaveString ;Load the the number of remaining
string
AND R1, R1, #0
LD R5, SaveR5
LD R4, SaveR4
LDR R3, R4, #0
A_OUTPUT LDR R0, R5, #0
OUT
AND R0,R0,#0 ;when we regconize 5 in 'B_LOOP' Label means
that this is already displayed chracter
ADD R0,R0,#5
STR R0,R5,#0
ADD R5, R5, #1 ;next character of string
ADD R3, R3, #-1 ;check whether there is any characters in the
string left
BRp A_OUTPUT
ADD R0, R1, #10 ;after finish each string. ENTER to the new
line
OUT
ADD R2, R2, #-1 ;check whether there is any strings left
BRz A_END
ST R2, SaveString
BR A_BEGIN

A_END LD R7, SaveR7


RET
;-------------------------------DESCEND
SUBROUTINE---------------------------------------;
DESCEND ST R7, SaveR7
B_BEGIN LD R5, Base
LD R4, Base1
B_LOOP LDR R0, R5, #0
BRz B_DONE1 ;if there is nothing in mem location =>
start to output strings
ADD R1, R0, #-5 ;if 5 means that this is the already displayed
character
BRz B_NEW_S
NOT R1, R0
ADD R1, R1, #1 ;R6 + R1(-R0)
ADD R3, R1, R6 ;R6 is the character of current lowest string
BRz B_EQUAL ;if equal then check the next character
in 2 strings
BRn B_NEW_MIN
BRp B_NEW_S
;----------------------------------------------------------;
B_EQUAL ST R4, SaveR4_BACKUP
ST R5, SaveR5_BACKUP
LD R3, SaveR4 ;R3 contains the number of character in old
address
LD R2, SaveR5
LDR R1, R5, #0 ;R1 is CURRENT VALUE
LDR R0, R2, #0 ;R0 is OLD VALUE
ADD R5, R5, #1 ;point to the next character in CURRENT STRING
ADD R2, R2, #1 ;point to the next character in CURRENT STRING
NOT R1, R1
ADD R1, R1, #1
ADD R1, R1, R0 ;R0 - R1 (OLD - CURRENT)
BRp B_CASE3
BRz B_CASE2
BRn B_CASE1

B_CASE3 AND R6, R6, #0


LD R4, SaveR4 ;dont change
LD R5, SaveR5
BR B_NEW_S

B_CASE2 LDR R1, R5, #0 ;R1 new value


LDR R0, R2, #0 ;R0 stored value
ADD R5, R5, #1 ;point to the next character in CURRENT STRING
ADD R2, R2, #1 ;point to the next character in CURRENT STRING
ADD R3, R3, #-1 ;check whehter there is any character left in
the old address
BRz B_CASE1
ADD R4, R4, #-1 ;check whehter there is any character left in
the current address
BRz B_CASE3
NOT R1, R1
ADD R1, R1, #1
ADD R1, R1, R0 ;R0 - R1 (OLD - CURRENT)
BRp B_CASE3
BRz B_CASE2
BRn B_CASE1

B_CASE1 AND R6, R6, #0 ;change the values


LD R4, SaveR4_BACKUP
LD R5, SaveR5_BACKUP
ST R4, SaveR4
ST R5, SaveR5
BR B_NEW_S
;----------------------------------------------------------;
B_NEW_S AND R1, R1, #0
LDR R3, R4, #0
ADD R4, R4, #1
ADD R5, R5, R3 ;next mem location
BR B_LOOP

B_NEW_MIN ADD R6, R0, #0 ;MIN <- current character of current string
ST R5, SaveR5 ;SaveR5 save the current address of lowest
string
ST R4, SaveR4 ;SaveR4 save the current address of the number
of cha
BR B_NEW_S
;----------------------------------------------------------;
B_DONE1 LD R2, SaveString ;Load the the number of remaining
string
AND R1, R1, #0
LD R5, SaveR5
LD R4, SaveR4
LDR R3, R4, #0
B_OUTPUT LDR R0, R5, #0
OUT
AND R0, R0, #0 ;when we regconize 5 in 'B_LOOP' Label means
that this is already displayed chracter
ADD R0, R0, #5
STR R0, R5, #0
ADD R5, R5, #1 ;next character of string
ADD R3, R3, #-1 ;check whether there is any characters in the
string left
BRp B_OUTPUT
ADD R0, R1, #10 ;after finish each string. ENTER to the new
line
OUT
ADD R2, R2, #-1 ;check whether there is any strings left
BRz B_END
ST R2, SaveString
AND R6, R6, #0
BR B_BEGIN

B_END LD R7, SaveR7


RET
;-------------------------------------------------------------------;
SaveR4 .FILL 0
SaveR4_BACKUP .FILL 0
SaveR5 .FILL 0
SaveR5_BACKUP .FILL 0
;-------------------------------------------------------------------;
.END

You might also like