Implement, in LC-3 assembly language, a Pig Latin generator. The user is to inpu
ID: 674923 • Letter: I
Question
Implement, in LC-3 assembly language, a Pig Latin generator.
The user is to input a word of no more than 19 characters, followed by the key. A real world program should not allow for more than 19 characters to be entered but for this program, checking for too many characters is NOT necessary. We will assume that the user will not enter a message longer than 19 characters. Store the word as one character per memory location, the word must be stored in memory. The input buffer should not be allocated for more than 20 characters (up to 19 characters plus the character). Hint #1: Using a TRAP x20 (GETC) followed by a TRAP x20 (OUT) will get a keystroke and echo the character back to the console. Hint #2: The key is ASCII x0A. Hint #3: For reasons that will become clear to you later, do not use R7 to store anything for this assignment.
If you could please comment the code that would be greatly appreciated!
Note: I'm sorry if this has already been asked, I tried a quick search to check for duplicates but couldn't see any that I know of.
Explanation / Answer
Implement, the LC-3 assembly language, a Pig Latin generator.
.ORIG x3000 ; start with x3000
LD R3, PTR ; R3 is the pointer to the characters
LEA R0, INMES ; input message at the R0
PUTS ; print the input message
AND R0, R0, #0 ; to clear R0
AND R1, R1, #0 ; clear the R1
AND R2, R2, #0 ; clear the R2
LD R1, SIZE ; set the maximum size of letters
LD R2, COUNT ; start the counter from with -1
LD R4, INIT ; put the memory on address of first letter to R4
CLEAR STR R0, R3, #0 ; clear the memory location at R3
ADD R2, R2, #1 ; add 1 to the counter
ADD R3, R3, #1 ; to add 1 to R3
ADD R2, R2, #-10 ; check if the counter is 19
ADD R2, R2, #-9
BRnp CLEAR ; if the counter isn't 19 clear next location
AND R1, R1, #0 ; clear the R1
AND R2, R2, #0 ; clear the R2
LD R3, PTR ; The R3 is pointer to characters
INPUT GETC ; input from the keyboard
OUT ; echo input to the console
ADD R2, R2, #1 ; increment the counter
BRz FIRST ; To store first letter at R4
STR R0, R3, #0 ; To store the contents of R0 into memory location of R3 + 0
ADD R3, R3, #1 ; increase the R3 (memory location) by 1
ADD R0, R0, #-10 ; check if 'enter' is the input
BRZ OUTPUT ;check if 'enter', input is done
ADD R1, R1, #-1 ; counter with maximum size of letters
BRP INPUT ; if less than the maximum number input more
FIRST STR R0, R4, #0 ; store the first letter to adress in R4
BRnzp INPUT ; go back to the INPUT
OUTPUT LDI R4, INIT ; load the data at INIT to R4
STR R4, R3, #-1 ; To store R4 (INIT data) to R3 (memory location)
LD R5, LETa ; load letter a to the R5
STR R5, R3, #0 ; To store letter a to R3
LD R6, LETy ; load the letter y to R6
STR R6, R3, #1 ;To store letter y to R3
LEA R0, OUTMES ; load the out message to R0
PUTS ; print the out message
LD R0, PTR ; The load memory location where the letters are stored into R0
PUTS ; output to the whole word to console
HALT ; halt the machine
SIZE .FILL #20 ; set the size to 20
COUNT .FILL #-1 ;
PTR .FILL x4000 ; set the memory location to x4000
INIT .FILL x4100 ; The first letter memory location
LETa .FILL x0061 ; 'a' memory the location
LETy .FILL x0079 ; 'y' memory the location
INMES .STRINGZ "English Word: "
OUTMES .STRINGZ "Pig-Latin Word: "
.END ; end the program
Program2
TRAP x20 (GETC) TRAP x21(out) assembly languge program.
TRAP_GETC
LDI R0,OS_KBSR ; waiting for the keystroke
BRzp TRAP_GETC
LDI R0,OS_KBDR ; read and return
RET
;;; OUT - Write a character in R0 to console.
TRAP_OUT
ST R1,OS_OUT_SAVE_R1 ; save the R1
TRAP_OUT_WAIT
LDI R1,OS_DSR ; wait for the display to ready
BRzp TRAP_OUT_WAIT
STI R0,OS_DDR ; write the character and return it
LD R1,OS_OUT_SAVE_R1 ; restore the R1
RET
;;; PUTS - Write the NUL-terminated string of characters to console,
;;; starting from the address with R0.
TRAP_PUTS
ST R0,OS_SAVE_R0 ; save R0, R1
ST R1,OS_SAVE_R1
ADD R1,R0,#0 ; move the string pointer (R0) into R1
TRAP_PUTS_LOOP
LDR R0,R1,#0 ; write a characters in string using OUT
BRz TRAP_PUTS_DONE
OUT
ADD R1,R1,#1
BRnzp TRAP_PUTS_LOOP
TRAP_PUTS_DONE
LD R0,OS_SAVE_R0 ; restore R0, R1
LD R1,OS_SAVE_R1
RET
;;; IN -That prompt the user for a single a character input, which is stored
;;; in R0 and also echoed to console.
TRAP_IN
LEA R0,TRAP_IN_MSG ; To prompt for input
PUTS
GETC ; read the character
OUT ; echo back to the monitor
ST R0,OS_SAVE_R0 ; to save the character
AND R0,R0,#0 ; write the linefeed, too
ADD R0,R0,#10
OUT
LD R0,OS_SAVE_R0 ; to restore the character
RET ; this doesn't work, because
;;; PUTSP - Write a NUL-terminated a string of characters, packed 2 per
;;; memory the location, to console, starting from the address in R0.
TRAP_PUTSP
ST R0,OS_SAVE_R0 ; save R0, R1, R2, R3
ST R1,OS_SAVE_R1
ST R2,OS_SAVE_R2
ST R3,OS_SAVE_R3
ADD R1,R0,#0 ; move string pointer (R0) into R1
TRAP_PUTSP_LOOP
LDR R2,R1,#0 ; read a next two characters
LD R0,LOW_8_BITS ; use the mask to get low byte
AND R0,R0,R2 ;check if low byte is NUL, quit printing
BRz TRAP_PUTSP_DONE
OUT ; otherwise to print the low byte
AND R0,R0,#0 ; The shift high byte into R0
ADD R3,R0,#8
TRAP_PUTSP_S_LOOP
ADD R0,R0,R0 ; The shift R0 left
ADD R2,R2,#0 ; to move MSB from R2 into R0
BRzp TRAP_PUTSP_MSB_0
ADD R0,R0,#1
TRAP_PUTSP_MSB_0
ADD R2,R2,R2 ; The shift R2 left
ADD R3,R3,#-1
BRp TRAP_PUTSP_S_LOOP
ADD R0,R0,#0 ; if the high byte is NUL, quit printing
BRz TRAP_PUTSP_DONE
OUT ; otherwise to print the low byte
ADD R1,R1,#1 ; to keep going
BRnzp TRAP_PUTSP_LOOP
TRAP_PUTSP_DONE
LD R0,OS_SAVE_R0 ; restore R0, R1, R2, R3
LD R1,OS_SAVE_R1
LD R2,OS_SAVE_R2
LD R3,OS_SAVE_R3
RET
;;; HALT - to trap the handler for halting machine
TRAP_HALT
LDI R0,OS_MCR
LD R1,MASK_HI ; clear the run bit in MCR
AND R0,R0,R1
STI R0,OS_MCR ; The halt
BRnzp OS_START ; To restart machine