Download CS 350 Homework Solution: I/O Synchronization, Stack Behavior, and Assembler Programming - and more Assignments Programming Languages in PDF only on Docsity!
Homework # 4 Solution (100 pts total)
Problem 8.2 (4 pts: Ready bit vs synchronized I/O)
A ready bit is not needed if synchronous I/O is used because we know exactly when the
data will arrive and when it will be taken away (input and output). If we do input and/or
output at the correct regular intervals, the output data will be outputted and the input
data will be received.
Problem 9.4 (16 = 4*4 pts: Program with OUT)
(a) 1111000000100001 (xf021)
(b) x
(c) x
(d) HookemHorns
Problem 10.08 (16 pts: Trace stack behavior)
In all the stacks below, the bottom is to the left, the top to the right.
(a) Here's the stack after each operation.
PUSH A : A
PUSH B : A B
POP : A
PUSH C : A C
PUSH D : A C D
POP : A C
PUSH E : A C E
POP : A C
POP : A
PUSH F : A F
So the stack contains A F after the PUSH F operation.
(b) The stack after each operation:
PUSH G : A F G
PUSH H : A F G H
PUSH I : A F G H I
PUSH J : A F G H I J
POP : A F G H I
PUSH K : A F G H I K
POP : A F G H I
POP : A F G H
POP : A F G
PUSH L : A F G L
POP : A F G
POP : A F
PUSH M : A F M
The stack contains the most elements after PUSH J and PUSH K operations.
(c) The stack now contains A F M.
#4: Problem 10.13 (20 = 54 pts: Trace runtime stack)*
... (b)
PSR of Pgm A
! R
x
x???? ! Saved.SSP
(Saved.USP
" User stack) PC x
... (a)
x???? ! Saved.SSP
(R6 " User
stack) PC x
... (c)
PSR for device B
! R
x
PSR of Pgm A
x
x???? ! Saved.SSP
PC
x
... (d)
PSR for device B
x
PSR of Pgm A
! R
x
x???? ! Saved.SSP
PC
x
... (e)
PSR for device B
! R
x
PSR of Pgm A
x
x???? ! Saved.SSP
PC
x
... (f)
PSR for device B
x
PSR of Pgm A
! R
x
x???? ! Saved.SSP
PC
x
... (g)
PSR for device B
x
PSR of Pgm A
x
x???? ! Saved.SSP
(R6 " User
stack) PC x
(a) Before fetching x3006 in Pgm A.
(b) Device B interrupt; Switch to Supervisor Stack, save
Pgm A status, aim PC to ISR for B.
(c) Device C interrupt: Save status of ISR for B; aim PC to
ISR for C. When device D raises its INT signal, it's
temporarily ignored since C has hirer priority than C.
(d) Device C ISR completes: Restore ISR for B, aim PC
back at ISR for B.
(e) Device D interrupt: Save status of ISR for B; aim PC to
ISR for D. The textbook isn't clear on whether or not
we actually execute an instruction of B's ISR before
noticing the D interrupt. If it does, then x6204 is the
return address (assuming the one instruction of B isn't
a branch or jump); if not, x6203 is the return address.
(f) Device D ISR completes: Restore ISR for B; aim PC
back at ISR for B.
(g) Device B ISR finally completes: Switch back to user
stack, aim PC back at Pgm A.
Problem 7 (20 pts: Nested Loops in Assembler)
Write an LC-3 *.asm assembler program that uses nested loops to print the following
lines of output. Assume STRING is the label for a STRINGZ value. For k = 1, 2, ..., length
of STRING, print the first k characters of STRING on one line. For example, if we had
STRING .STRINGZ "abcd", we would print out
a
ab
abc
abcd
Note if STRING is the empty string, you print no output.
; Registers: R1 points to the last char of the current output line's
substring.
; (When R1 points to a null character, we're done printing.)
; R2 = line number (1, 2, ....)
; R3 points to the next character to print on the current line.
; R4 = number of chars yet to print on this line
.ORIG x
LEA R1, STRING ; Init R1 for 1st line
AND R2, R2, #0 ; Init current line number
ADD R2, R2, #1 ; ... to 1
OUTER LDR R0, R1, #0 ; Get this line's last character
BRz DONE ; If it's null, we're done printing
ADD R4, R2, #0 ; Init # chars to print on this line
LEA R3, STRING ; Pt R3 to 1st char of current line
LDR R0, R3, #0 ; Get 1st char of current line
INNER OUT ; Print curr char of curr line
ADD R4, R4, #-1 ; Decr # chars yet to print on this line
BRz EOLINE ; Exit inner loop if no chars to print
ADD R3, R3, #1 ; Pt next char in curr line
LDR R0, R3, #0 ; Get next char in curr line
BR INNER ; Continue inner loop
EOLINE LD R0, NEWLINE ; Get \n
OUT ; Print \n to end this line
ADD R1, R1, #1 ; Move right end of substring to print
ADD R2, R2, #1 ; Incr line number
BR OUTER ; Continue outer loop
DONE HALT
NEWLINE .FILL x0A ; Equivalent to C's '\n'
STRING .STRINGZ "abcdefghijklmnopqrstuvwxyz"
.END