# Cheating sheet midterm - .data Prompt.asciiz"Enter the...

• Test Prep
• 4
• 100% (3) 3 out of 3 people found this document helpful

This preview shows page 1 - 2 out of 4 pages.

.data Prompt: .asciiz "Enter the string: " lowerCase: .word 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 lowerLetters: .byte 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t',' u','v','w','x','y','z' upperCase: .word 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 upperLetters: .byte 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q',' R','S','T','U','V','W','X','Y','Z' Space: .asciiz ": " myString: .space 60 newLine: .asciiz "\n" spaceCount: .asciiz "\n The number of spaces is: " yesPalindrome: .asciiz "\n The string is a palindrome!" noPalindrome: .asciiz "\n The string is not a palindrome!" .globl main .text main: la $a0,Prompt li$v0,4 syscall la $a0,myString li$v0,8 li $a1,40 syscall jal count la$a2,lowerCase la $t5,lowerLetters li$t8,26 la $a0,newLine li$v0,4 syscall loopP: lb $a0,0($t5) li $v0,11 syscall la$a0,Space li $v0,4 syscall lw$a0,0($a2) li$v0,1 syscall la $a0,newLine li$v0,4 syscall add $t8,$t8,-1 addi $t5,$t5,1 addi $a2,$a2,4 bgtz $t8,loopP la$a3,upperCase la $t6,upperLetters li$t8,26 la $a0,newLine li$v0,4 syscall jal isPalindrome end:li $v0,10 syscall count: li$s1,97 li $s2,32 li$s3,4 li $t7,0 #number of spaces la$a0,myString loop: la $a2,lowerCase la$a3,upperCase li $t2,0 #temp variable li$t3,0 #temp variable lb $t1,0($a0) addi $a0,$a0,1 beq $t1,$0,return bge $t1,$s1,lower bgt $t1,$s2,upper beq $t1,$s2,space b loop lower: addi $t2,$t1,-97 mult $t2,$s3 mflo $t2 add$a2,$a2,$t2 lw $t3,0($a2) addi $t3,$t3,1 sw $t3,0($a2) b loop upper: addi $t2,$t1,-65 mult $t2,$s3 mflo $t2 add$a3,$a3,$t2 lw $t3,0($a3) addi $t3,$t3,1 sw $t3,0($a3) b loop space: addi $t7,$t7,1 b loop return: jr $ra isPalindrome: findLen:lb$t3, 0($t2) beq$t3, $zero, endLen add$t2, $t2, 1 j findLen endLen: sub$t2, $t2, 2 check: bge$t1, $t2, palin lb$t3, 0($t1) lb$t4, 0($t2) bne$t3, $t4, notPalin addi$t1, $t1, 1 sub$t2, $t2, 1 j check palin: la$a0,yesPalindrome li $v0, 4 syscall j return1 notPalin: la$a0,noPalindrome li $v0, 4 syscall return1: jr$ra the Sum of the first 100 words of data in the memory data segment with the label “pomona”. Store the resulting sum in the next memory location beyond the end of the array pomona. .data pomona: .space 400 result: .word 0 .globl main .text main: la $a0, pomona # Load address pointer li$t0, 0 # Clear sum li $t1, 100 # Initialize loop count loop: lw$t2, 0($a0) #$t2 = Mem(a0) add $t0,$t0, $t2 #$t0 = $t0 +$t2 addi $a0,$a0, 4 # Inc. address pointer addi $t1,$t1, -1 # Dec. loop count bgtz $t1, loop # if ($t1 > 0) branch sw $t0, 0($a0) # Store the result li $v0, 10 # End of program syscall transfer a block of 100 words starting at memory location “SRC” to another area of memory beginning at memory location “DEST”. .data SRC: .space 400 DEST: .space 400 .globl main .text main: la$a1, SRC # $a1 = &SRC # &means address of la$a2, DEST # $a2 = &DEST li$t0, 100 # $t0 = 100 loop: lw$t1, 0($a1) #$t1= Mem($a1) sw$t1, 0($a2) # Mem($a2) = $t1 addi$a1, $a1,4 #$a1 = $a1+4 addi$a2, $a2,4 #$a2 = $a2+4 addi$t0, $t0, -1#$t0 = $t0 - 1 bgtz$t0, loop #Branch if $t0 > 0 li$v0, 10 syscall integers from 1 to N, making use the multiplication and shifting operations. The value N will be passed to the procedure in $a0 and the result will be returned in the$v0 register. SUM: addi $v0,$a0, 1 # $v0 =$a0 + 1 mult $v0,$a0 # $v0 =$v0 * $a0 mflo$v0 sra $v0,$v0, 1 # Shift right arithmetic # is the quick way to # divide by 2 jr $ra Write a function FIB(N, &array) to store the First N elements of the Fibonacci sequence into an array in memory. The value N is passed in$a0, and the address of the array is passed in register \$a1.