# Enter string and Count Character with Assembly Language Assignment Solution

June 19, 2024
Rehana Magnus
Assembly Language
Rehana Magnus, PhD in Computer Science from the esteemed Acadia Institute of Technology, Canada. With 6 years of experience, specializes in assembly language programming. Proficient in low-level coding, optimizing performance, and enhancing system functionality.
Key Topics
• Assembler Program with Character Strings
Tip of the day
News

## Assembler Program with Character Strings

Write an assembly language homework that prompts the user to enter whatever they want (line/string of characters) followed by ENTER

Call the function to take the string & pass it by where it starts in memory (put in register to set where the string starts). Count how many characters including spaces person entered. The program returns back the number of characters & spaces in the register to return how long the string was via register "You entered: _____ characters". Concert & print out ASCII characters from a number of characters

Solution

```; Registers used: ; R0 = used as argument for traps and functions, as return value and as a temporary register ; R1 = used to point to string and as return value for function div10 ; R2 = used to hold ascii value for '0' ; R3 = used to hold the number of total characters in the string ; R4 = used to hold the number of spaces in the string ; R5 = used to hold the negative of ascii space (-32) ; R6 = used to hold temporary result of comparison ; R7 = holds return address in function calls .ORIG x3000 ; start address of code ; prompt user to enter string LEA R0, prompt ; load prompt address TRAP x22 ; print string LEA R1, prompt ; load string address ; read the string and count total chars and spaces using a loop AND R3, R3, #0 ; number of characters, start in zero AND R4, R4, #0 ; number of spaces, start in zero LD R5, negspc ; load -space to check for spaces ReadString TRAP x20 ; read character TRAP x21 ; echo character ADD R0, R0, #-10 ; see if character was an enter BRz readEnd ; if so, end loop ADD R0, R0, #10 ; restore char ADD R3, R3, #1 ; else, increment total count STR R0, R1, #0 ; save character in string ADD R6, R0, R5 ; see if it was a space BRnp countNext ; if not a space,count next ADD R4, R4, #1 ; else, increment space count countNext ADD R1, R1, #1 ; advance to next position in string BR ReadString ; repeat loop readEnd STR R0, R1, #0 ; save ending zero in string ; here R3 has the total number of chars read and R4 has the number of spaces ; Print the result: LEA R0, result1 ; load address of the first result message TRAP x22 ; print the string ADD R0, R3, #0 ; copy number of chars JSR PrintNum ; print number LEA R0, result2 ; load address of the second result message TRAP x22 ; print the string LD R0, newline ; load newline TRAP x21 ; jump to next line ; Print the space result: LEA R0, result3 ; load address of the 3rd result message TRAP x22 ; print the string ; print number of spaces ADD R0, R4, #0 ; copy number of spaces JSR PrintNum ; print number LEA R0, result4 ; load address of the 4th result message TRAP x22 ; print the string ; print number of non-spaces ADD R0, R3, #0 ; copy total number of chars NOT R4, R4 ; take ones complement of number of spaces ADD R4, R4, #1 ; add 1 to get the negative ADD R0, R0, R4 ; subtract number of spaces to get the non-spaces JSR PrintNum ; print number ; Print the last message: LEA R0, result5 ; load address of the 5rd result message TRAP x22 ; print the string LD R0, newline ; load newline TRAP x21 ; jump to next line HALT ; terminate the program ; Function to divide number in R0 by 10, returns quotient in R0, remainder in R1 div10 ADD R1, R0, #0 ; copy number to R1 AND R0, R0, #0 ; initialize quotient to zero divLoop ADD R1, R1, #-10 ; compare number with 10 BRn endDiv ; if < 10, end division ADD R0, R0, #1 ; else, increment quotient BR divLoop ; repeat loop endDiv ADD R1, R1, #10 ; restore remainder RET ; return ; Function to print number in R0, assumes is <100 PrintNum ST R7, tempR7 ; save return address JSR div10 ; divide number by 10 LD R2, ascii0 ; load ascii '0' ADD R0, R0, #0 ; see if the quotient is zero BRz printLow ; if so, print just low digit ADD R0, R0, R2 ; convert digit to ascii TRAP x21 ; print digit printLow ADD R0, R1, R2 ; convert low digit to ascii TRAP x21 ; print digit LD R7, tempR7 ; restore return address RET tempR7 .FILL 0 ; place to save return address ascii0 .FILL 48 prompt .STRINGZ "Please enter a string: " result1 .STRINGZ "You entered: " result2 .STRINGZ " chars" result3 .STRINGZ "There were " result4 .STRINGZ " spaces and " result5 .STRINGZ " non-space characters " negspc .FILL -32 newline .FILL 10 string .BLKW 100 .END ```

## Similar Samples

Explore our curated samples at ProgrammingHomeworkHelp.com to witness our expertise in diverse programming languages and topics. From introductory exercises to complex projects, these examples showcase our commitment to delivering high-quality solutions tailored to your academic needs. See firsthand how we can assist you in mastering programming concepts effectively.