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.
时间: 2024-02-13 19:02:22 浏览: 27
Sure, here's an example LC-3 subroutine for printing "Hello" N times:
```
; Subroutine to print "Hello" for N times
; Inputs: R0 - N (number of times to print)
; Outputs: None
LEA R1, HELLO_STRING ; Load address of "Hello" string
LOOP:
ADD R2, R2, #0 ; Clear register R2
LDR R2, R0, #0 ; Load N from memory
BRz END_LOOP ; If N is 0, exit loop
PUTS ; Print "Hello"
ADD R0, R0, #-1 ; Decrement N
BRnzp LOOP ; Loop back to start
END_LOOP:
RET ; Return to calling program
HELLO_STRING:
.STRINGZ "Hello\n" ; String to print
```
And here's an example main program that inputs N from the keyboard, calls the subroutine, and outputs "Hello" for N times:
```
; Main program to input N, call subroutine, and output "Hello" for N times
; Inputs: None
; Outputs: None
.ORIG x3000
; Prompt user to enter N
LEA R0, PROMPT_STRING
PUTS
; Read in N from keyboard
GETC
OUT
SUB R1, R0, #'0' ; Convert character to number
; Call subroutine to print "Hello" for N times
JSR PRINT_HELLO
; Exit program
TRAP x25 ; Halt program
PROMPT_STRING:
.STRINGZ "Enter the number of times to print \"Hello\": "
.END
```
Note that this code assumes that the LC-3 assembler and simulator are set up and running correctly. Also, this code does not include any error checking or input validation, so it may not work correctly if the user inputs a non-numeric value or a negative number for N.