For this LC-3 assembly code .. .ORIG x3000 AND R2,R2,#0 LD R3, FILE COUNT LDR R0
ID: 3865807 • Letter: F
Question
For this LC-3 assembly code ..
.ORIG x3000
AND R2,R2,#0
LD R3, FILE
COUNT LDR R0,R3,#0
BRz END_COUNT
ADD R3,R3,#1
ADD R2,R2,#1
BRnzp COUNT
END_COUNT ADD R4,R2,#0
BRz SORTED
OUTERLOOP ADD R4,R4,#-1
BRnz SORTED
ADD R5,R4,#0
LD R3,FILE
INNERLOOP LDR R0,R3,#0
LDR R1,R3,#1
NOT R2,R1
ADD R2,R2,#1
ADD R2,R0,R2
BRnz SWAPPED
STR R1,R3,#0
STR R0,R3,#1
SWAPPED ADD R3,R3,#1
ADD R5,R5,#-1
BRp INNERLOOP
BRnzp OUTERLOOP
SORTED HALT
FILE .FILL x7000
.END
add the prompts for the user to input a number
add a loop for input to continue until zero is entered
sort the array
print out on the screen “Sorted Array”
print out the sorted array, one number per line. Note this will require conversion of binary integers to ASCII.
You will need to use various IO traps for this assignment.
so an example would look like this
Enter a number <Program output>
3 <User input>
Enter a number <Program output>
2 <User input>
Enter a number <Program output>
5 <User input>
Enter a number <Program output>
0 <User input. Note: this will terminate input>
Sorted Array <Program output>
2
3
5
Explanation / Answer
AND R2, R2, #0 ; Initialize R2 <- 0 (counter)
FILE .FILL x3500 ; File location
; Implementing bubble sort algorithm ; R0 File item ; R1 File item ; R2 Work variable ; R3 File pointer ; R4 Outer loop counter ; R5 Inner loop counter .ORIG x3000 ; Count the number of items to be sorted and store the value in R7AND R2, R2, #0 ; Initialize R2 <- 0 (counter)
LEA R0, PROMPT ;Load address of prompt TRAP x22 ;Output prompt to displayLD R3, FILE ; Put file pointer into R3 COUNT LDR R0, R3, #0 ; Put next file item into R0 BRZ END_COUNT ; Loop until file item is 0 ADD R3, R3, #1 ; Increment file pointer ADD R2, R2, #1 ; Increment counter BRNZP COUNT ; Counter loop END_COUNT ADD R4, R2, #0 ; Store total items in R4 (outer loop count) BRZ SORTED ; Empty file ; Do the bubble sort OUTERLOOP ADD R4, R4, #-1 ; loop n - 1 times BRNZ SORTED ; Looping complete, exit ADD R5, R4, #0 ; Initialize inner loop counter to outer LD R3, FILE ; Set file pointer to beginning of file INNERLOOP LDR R0, R3, #0 ; Get item at file pointer LDR R1, R3, #1 ; Get next item NOT R2, R1 ; Negate ... ADD R2, R2, #1 ; ... next item ADD R2, R0, R2 ; swap = item - next item BRNZ SWAPPED ; Don't swap if in order (item <= next item) STR R1, R3, #0 ; Perform ... STR R0, R3, #1 ; ... swap SWAPPED ADD R3, R3, #1 ; Increment file pointer ADD R5, R5, #-1 ; Decrement inner loop counter BRP INNERLOOP ; End of inner loop BRNZP OUTERLOOP ; End of outer loop SORTED HALT
FILE .FILL x3500 ; File location
PROMPT .STRINGZ "Enter the Number: ".END