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.
时间: 2024-01-30 13:02:19 浏览: 22
Here is an example LC-3 subroutine to print "Hello" for N times:
```
; Subroutine to print "Hello" for N times
; Input: R0 = N
; Output: None
PRINT_HELLO AND R1, R1, #0 ; Initialize counter to 0
BRnzp LOOP ; Branch to loop
PRINT_STR TRAP x22 ; Print string
.STRINGZ "Hello\n"
LOOP ADD R1, R1, #1 ; Increment counter
NOT R2, R1 ; R2 = -counter
ADD R2, R2, #1 ; R2 = N - counter
BRz END_LOOP ; Branch to end loop if counter = N
BRnzp PRINT_STR ; Print "Hello"
BRnzp LOOP ; Loop back
END_LOOP RET ; Return from subroutine
```
And here is an example LC-3 main program to input N from the keyboard, call the subroutine, and output "Hello" for N times:
```
.ORIG x3000
; Main program to input N, call subroutine, and output "Hello" for N times
LEA R0, PROMPT ; Print prompt to enter N
PUTS
GETC ; Get character
OUT
IN ; Read character
LD R1, ASCII_ZERO ; Convert character to number
ADD R0, R1, #0
OUT
LOOP ; Call subroutine to print "Hello" for N times
LEA R0, NEWLINE ; Print newline
PUTS
HALT
PROMPT .STRINGZ "Enter N: "
NEWLINE .STRINGZ "\n"
ASCII_ZERO .FILL x0030 ; ASCII code for '0'
.END
```