Instructions
Objective
Write an assembly language assignment program to count length of string and display it in LC3
Requirements and Specifications
Write an assembler program that prompts a user to input some type of string. It can be anything from a famous sentence, a song lyric, saying, etc. Then write and call a function that will count the number of characters entered and return the number of characters entered back to the main program. The main program will then tell the user how many characters they had entered. You don't need to worry about spaces, commas, periods, etc. Everything typed can be considered a character.
Remember, there is a difference between an integer representation of a number and the character representation of a number. i.e. To print the number 31 you need to print the character 3 which is ×33 in ASCII and 1 is x31 in ASCII.
If you initialise any memory locations or registers, make sure you comment that at the beginning of your code.
Screenshots of output
Source Code
.ORIG x3000
MAIN
; Prompt user to enter string
LEA R0, PROMPT ; load address of prompt
PUTS ; print the prompt
; Count characters in string
JSR COUNTCHARS ; count characters in string
; Convert number to ascii
LEA R4, NUMBER ; load address to save number
AND R2, R2, #0 ; start in zero
STR R2, R4, #0 ; save ending zero
CONVNUM AND R2, R2, #0 ; quotient, start in zero
DIV10 ADD R3, R0, #-10 ; compare number with 10
BRn SAVEDIGIT ; if less than 10, save digit
ADD R2, R2, #1 ; else, increment quotient
ADD R0, R3, #0 ; copy subtraction to number
BR DIV10 ; repeat
SAVEDIGIT LD R3, ASCII0 ; load ascii 0
ADD R0, R0, R3 ; convert digit to ascii
ADD R4, R4, #1 ; increment position in converted string
STR R0, R4, #0 ; save converted digit
ADD R0, R2, #0 ; copy quotient to R0
BRp CONVNUM ; if not zero, convert next digit
; Print result message
LEA R0, RESULT ; load address of result string
PUTS ; print the string
; Print number
PRINTDIGIT LDR R0, R4, #0 ; load digit from converted number
BRz PRINTEND ; if it's zero, end print
OUT ; else, print digit
ADD R4, R4, #-1 ; go to previous digit
BR PRINTDIGIT ; print next digit
PRINTEND HALT ; terminate the program
; Data for main
PROMPT .STRINGZ "Please enter a string: "
RESULT .STRINGZ "Number of characters entered: "
ASCII0 .FILL 48 ; ascii value of '0'
NUMBER .BLKW 10 ; space to save converted number digits
; Function to count characters in a string
; Returns:
; R0 = count of characters
COUNTCHARS ST R7, SAVER7 ; save return address
AND R2, R2, #0 ; start count in zero
CNTLOOP GETC ; read character
OUT ; echo on screen
ADD R1, R0, #-10 ; compare character with enter
BRz CNTEND ; if equal, end reading string and end count
ADD R2, R2, #1 ; else, count one more char
BR CNTLOOP ; count next character
CNTEND ADD R0, R2, #0 ; return count in R0
LD R7, SAVER7 ; load return address
RET ; return to calling function
; Data for countchars
SAVER7 .BLKW 1 ; place to save R7
.END
Similar Samples
Explore our sample section to see high-quality programming solutions crafted by our experts. Each example showcases our proficiency across various languages and problem types, ensuring you get a glimpse of the excellence we bring to your programming assignments.
Assembly Language
Assembly Language
Assembly Language
Assembly Language
Assembly Language
Assembly Language
Assembly Language
Assembly Language
Assembly Language
Assembly Language
Assembly Language
Assembly Language
Assembly Language
Assembly Language
Assembly Language
Assembly Language
Assembly Language
Assembly Language
Assembly Language
Assembly Language