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-26 07:03:10 浏览: 27
Here's a possible LC-3 subroutine for printing "Hello" N times:
```
; Subroutine to print "Hello" N times
; R0: number of times to print "Hello"
LEA R1, HELLO ; Load address of "Hello" string
LD R2, COUNT ; Load current count from memory
LOOP ADD R2, R2, #-1 ; Decrement count
BRZ DONE ; If count is zero, exit loop
PUTS ; Print "Hello"
BR LOOP ; Repeat loop
DONE ST R0, COUNT ; Store final count to memory
RET
HELLO .STRINGZ "Hello\n" ; String to print
COUNT .FILL X'0000' ; Initial count
```
And here's a possible main program that uses this subroutine:
```
; Main program to input N, call the subroutine, and output "Hello" N times
.ORIG X3000
; Prompt user for input
LEA R0, PROMPT
PUTS
GETC
OUT
IN
PUTS
; Convert input to decimal
AND R1, R1, #0
ADD R2, R2, #-48
ADD R1, R1, R2
ADD R1, R1, R1
; Call subroutine to print "Hello" N times
JSR PRINT_HELLO
; Exit program
TRAP X25
PROMPT .STRINGZ "Enter a number: "
.BLKW 1
.END
```
When executed, this program will prompt the user to enter a number, call the subroutine to print "Hello" N times, and then exit. The subroutine itself uses a loop to print "Hello" N times, and stores the final count in memory so that it can be used later if needed.