1.Write a LC-3 subroutine for printing “Hello” for N times. Write a main program to input N from the keyboard, call the subroutine and then output “Hello” for N times. (20 points)
时间: 2024-02-12 20:06:04 浏览: 45
Here is a LC-3 subroutine for printing "Hello" for N times:
```
; Subroutine to print "Hello" for N times
; Input: R0 - Number of times to print
; Output: None
.ORIG x3000
PrintHello
LD R1, HelloString ; Load address of "Hello" string into R1
Loop
BRz Exit ; If count is zero, exit the loop
PUTS ; Output "Hello" string
ADD R0, R0, #-1 ; Decrement count
BR Loop ; Repeat loop
Exit
RET
HelloString .STRINGZ "Hello\n"
.END
```
And here is a LC-3 main program to input N from the keyboard, call the subroutine, and output "Hello" for N times:
```
; Main program to call PrintHello subroutine
; Input: None
; Output: None
.ORIG x4000
LEA R0, Prompt ; Load address of prompt message into R0
PUTS ; Output prompt message
IN ; Read input from keyboard into R0
JSR PrintHello ; Call PrintHello subroutine
HALT
Prompt .STRINGZ "\nEnter the number of times to print 'Hello': "
.END
```
Note that the input value N is passed to the PrintHello subroutine in R0. The subroutine then uses a loop to output the "Hello" string N times. After the subroutine returns, the main program halts.