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
.ORIG x3000MAIN; Prompt user to enter stringLEA R0, PROMPT ; load address of promptPUTS ; print the prompt; Count characters in stringJSR COUNTCHARS ; count characters in string; Convert number to asciiLEA R4, NUMBER ; load address to save numberAND R2, R2, #0 ; start in zeroSTR R2, R4, #0 ; save ending zeroCONVNUM AND R2, R2, #0 ; quotient, start in zeroDIV10 ADD R3, R0, #-10 ; compare number with 10BRn SAVEDIGIT ; if less than 10, save digitADD R2, R2, #1 ; else, increment quotientADD R0, R3, #0 ; copy subtraction to numberBR DIV10 ; repeatSAVEDIGIT LD R3, ASCII0 ; load ascii 0ADD R0, R0, R3 ; convert digit to asciiADD R4, R4, #1 ; increment position in converted stringSTR R0, R4, #0 ; save converted digitADD R0, R2, #0 ; copy quotient to R0BRp CONVNUM ; if not zero, convert next digit; Print result messageLEA R0, RESULT ; load address of result stringPUTS ; print the string; Print numberPRINTDIGIT LDR R0, R4, #0 ; load digit from converted numberBRz PRINTEND ; if it's zero, end printOUT ; else, print digitADD R4, R4, #-1 ; go to previous digitBR PRINTDIGIT ; print next digitPRINTEND HALT ; terminate the program; Data for mainPROMPT .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 charactersCOUNTCHARS ST R7, SAVER7 ; save return addressAND R2, R2, #0 ; start count in zeroCNTLOOP GETC ; read characterOUT ; echo on screenADD R1, R0, #-10 ; compare character with enterBRz CNTEND ; if equal, end reading string and end countADD R2, R2, #1 ; else, count one more charBR CNTLOOP ; count next characterCNTEND ADD R0, R2, #0 ; return count in R0LD R7, SAVER7 ; load return addressRET ; return to calling function; Data for countcharsSAVER7 .BLKW 1 ; place to save R7.END