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 15:01:18 浏览: 65
Here is the LC-3 subroutine for printing "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
LD R2, HELLO_MSG ; Load the address of "Hello" message
LOOP ADD R1, R1, #1 ; Increment counter
BRz END_LOOP ; If counter equals to 0, exit the loop
LD R0, ASCII_NEWLINE ; Load the ASCII code of newline character
OUT ; Output newline character
LD R0, ASCII_SPACE ; Load the ASCII code of space character
OUT ; Output space character
LD R0, R2 ; Load the address of "Hello" message
OUT ; Output "Hello" message
BR LOOP ; Repeat the loop
END_LOOP RET ; Return from subroutine
; Data section
HELLO_MSG .STRINGZ "Hello"
ASCII_SPACE .FILL x0020 ; ASCII code for space character
ASCII_NEWLINE .FILL x000A ; ASCII code for newline character
```
And here is the LC-3 main program to input N from the keyboard, call the subroutine, and output "Hello" for N times:
```
; Main program to call PRINT_HELLO subroutine
; Input: None
; Output: None
.ORIG x3000
; Prompt the user to input N
LEA R0, PROMPT_MSG
PUTS
GETC
OUT
PUTS
; Convert ASCII to decimal
AND R1, R1, #0
AND R2, R2, #0
ADD R2, R2, #-48
ADD R1, R1, R2
GETC
ADD R1, R1, R1
ADD R1, R1, R1
ADD R1, R1, R2
ADD R0, R1, #0
; Call PRINT_HELLO subroutine
JSR PRINT_HELLO
; Exit program
HALT
; Data section
PROMPT_MSG .STRINGZ "Enter N: "
```
This program prompts the user to input N, converts the ASCII code to decimal, calls the PRINT_HELLO subroutine with N as the input, and outputs "Hello" for N times.
阅读全文