Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

that is a mips assembly language programs using qtspim Compose a program to exam

ID: 3680448 • Letter: T

Question

that is a mips assembly language programs using qtspim

Compose a program to examine the string "Hello, world! ", calculating the total numeric value of all the characters in the string (including punctuation marks). The program should load each letter, add that numbers numeric value to the running total, and finally produce a total sum. The program will take a loop, but in this case, you do not need a counter, since the phrase is null terminated. Simply look for a null (0) and then terminate the program by printing out the total. Remember, punctuation (even spaces!) have a numeric value as well. What is the total character numeric sum?

Explanation / Answer

.data #declaration of vars follows str: .asciiz "Hello, worls! " char: .space 2 null: .asciiz "" space: .ascii " " newline: .asciiz " " .text #instructions follow main: la $a0, str #load string message into $a0 for syscall li $v0, 4 #load syscall to print string syscall #print string message la $s1, str #set base address of string to s1 loop: #start of read loop jal getc #jump to getc subroutine lb $t0, char #load the char from char buffer into t0, stripping null sb $t0, 0($s1) #store the char into the nth elem of array lb $t1, newline #load newline char into t1 beq $t0, $t1, done #end of string? jump to done addi $s1, $s1, 1 #increments base address of array j loop #jump to start of read loop readInt: #read string of ascii digits, store into a local variable and #convert into integer, return that int unless string contains #non-integers done: #let s2 be the sum total addi $s1, $s1, -1 #reposition array pointer to last char before newline char la $s0, str #set base address of array to s0 for use as counter addi $s0, $s0, -1 #reposition base array to read leftmost char in string add $s2, $zero, $zero #initialize sum to 0 li $t0, 10 #set t0 to be 10, used for decimal conversion li $t3, 1 lb $t1, 0($s1) #load char from array into t1 blt $t1, 48, error #check if char is not a digit (ascii'9') addi $t1, $t1, -48 #converts t1's ascii value to dec value add $s2, $s2, $t1 #add dec value of t1 to sumtotal addi $s1, $s1, -1 #decrement array address lp: #loop for all digits preceeding the LSB mul $t3, $t3, $t0 #multiply power by 10 beq $s1, $s0, FIN #exit if beginning of string is reached lb $t1, ($s1) #load char from array into t1 blt $t1, 48, error #check if char is not a digit (ascii'9') addi $t1, $t1, -48 #converts t1's ascii value to dec value mul $t1, $t1, $t3 #t1*10^(counter) add $s2, $s2, $t1 #sumtotal=sumtotal+t1 addi $s1, $s1, -1 #decrement array address j lp #jump to start of loop error: #if non digit chars are entered, readInt returns 0 add $s2, $zero, $zero j FIN FIN: li $v0, 1 add $a0, $s2, $zero syscall li $v0, 10 #ends program syscall