his program works with an array of strings . An array of strings differs from an
ID: 3553531 • Letter: H
Question
his program works with an array of strings. An array of strings differs from an array of integers. In general, an array has to contain items that are all the same size so that the address of a particular element can be easily calculated. This is how you have worked with an array of integers on previous assignments. The problem with strings is that they are generally not all the same length. See the example below. To get around this problem, we use the address of each string. All addresses in MIPS are the same size (32 bits, one word). We can create an array of addresses. Here is an example, taken fromtest04.s: mainNumPhrases: .word 4 mainPhrases: .word mainPhrase1 .word mainPhrase2 .word mainPhrase3 .word mainPhrase4 mainPhrase1: .asciiz "abcdefghijklmnopqrstuvwxyz" mainPhrase2: .asciiz "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdeabcdeABCDEXYXYXYZz" mtainPhrase3: .asciiz "1234567890-=`~!@#$%^&*()_+[]{}|';:,./?><" mainPhrase4: .ascii "I have sworn upon the altar of God eternal hostility " .ascii "against every form of tyranny over the mind of man. " .asciiz "-- Thomas Jefferson." The array, mainPhrases, contains four addresses, with each address occupying one word. Since the addresses are all the same size, this creates an array of addresses. As a partial example of how to use this array, suppose we want to print the string that is in position 3 of the array,mainPhrases[3]. To get to this position, we would multiply 3 times the size of one element, which is 4: addi $t0, $zero, 3 # want position 3 sll $t0, $t0, 2 # multiply position by 4 la $t1, mainPhrases # address of beginning of array add $t1, $t1, $t0 # add offset to beginning of array lw $a0, 0($t1) # get the address of the 3rd string addi $v0, $zero, 4 syscall Note that an lw instruction was used to get the address from the array into $a0, not an lainstruction. The la instruction converts a label to an address. The la instruction should only be used when getting the address of a label. Here, we need to load the address from a memory location (not a label) into a register; thus, the use of lw. Array of integers: You will need to declare an array of integers to hold the counts of the number of each letter. output example abcdefghijklmnopqrstuvwxyz a: 1 b: 1 c: 1 d: 1 e: 1 f: 1 g: 1 h: 1 i: 1 j: 1 k: 1 l: 1 m: 1 n: 1 o: 1 p: 1 q: 1 r: 1 s: 1 t: 1 u: 1 v: 1 w: 1 x: 1 y: 1 z: 1 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdeabcdeABCDEXYXYXYZz a: 5 b: 5 c: 5 d: 5 e: 5 f: 2 g: 2 h: 2 i: 2 j: 2 k: 2 l: 2 m: 2 n: 2 o: 2 p: 2 q: 2 r: 2 s: 2 t: 2 u: 2 v: 2 w: 2 x: 5 y: 5 z: 4 1234567890-=`~!@#$%^&*()_+[]{}|';:,./?>< a: 5 b: 5 c: 5 d: 5 e: 5 f: 2 g: 2 h: 2 i: 2 j: 2 k: 2 l: 2 m: 2 n: 2 o: 2 p: 2 q: 2 r: 2 s: 2 t: 2 u: 2 v: 2 w: 2 x: 5 y: 5 z: 4 I have sworn upon the altar of God eternal hostility against every form of tyranny over the mind of man. -- Thomas Jefferson. a: 14 b: 5 c: 5 d: 7 e: 15 f: 8 g: 4 h: 7 i: 7 j: 3 k: 2 l: 5 m: 6 n: 11 o: 13 p: 3 q: 2 r: 10 s: 7 t: 11 u: 3 v: 5 w: 3 x: 5 y: 9 z: 4 his program works with an array of strings. An array of strings differs from an array of integers. In general, an array has to contain items that are all the same size so that the address of a particular element can be easily calculated. This is how you have worked with an array of integers on previous assignments. The problem with strings is that they are generally not all the same length. See the example below. To get around this problem, we use the address of each string. All addresses in MIPS are the same size (32 bits, one word). We can create an array of addresses. Here is an example, taken fromtest04.s: mainNumPhrases: .word 4 mainPhrases: .word mainPhrase1 .word mainPhrase2 .word mainPhrase3 .word mainPhrase4 mainPhrase1: .asciiz "abcdefghijklmnopqrstuvwxyz" mainPhrase2: .asciiz "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdeabcdeABCDEXYXYXYZz" mtainPhrase3: .asciiz "1234567890-=`~!@#$%^&*()_+[]{}|';:,./?><" mainPhrase4: .ascii "I have sworn upon the altar of God eternal hostility " .ascii "against every form of tyranny over the mind of man. " .asciiz "-- Thomas Jefferson." The array, mainPhrases, contains four addresses, with each address occupying one word. Since the addresses are all the same size, this creates an array of addresses. As a partial example of how to use this array, suppose we want to print the string that is in position 3 of the array,mainPhrases[3]. To get to this position, we would multiply 3 times the size of one element, which is 4: addi $t0, $zero, 3 # want position 3 sll $t0, $t0, 2 # multiply position by 4 la $t1, mainPhrases # address of beginning of array add $t1, $t1, $t0 # add offset to beginning of array lw $a0, 0($t1) # get the address of the 3rd string addi $v0, $zero, 4 syscall Note that an lw instruction was used to get the address from the array into $a0, not an lainstruction. The la instruction converts a label to an address. The la instruction should only be used when getting the address of a label. Here, we need to load the address from a memory location (not a label) into a register; thus, the use of lw. Array of integers: You will need to declare an array of integers to hold the counts of the number of each letter. output example abcdefghijklmnopqrstuvwxyz a: 1 b: 1 c: 1 d: 1 e: 1 f: 1 g: 1 h: 1 i: 1 j: 1 k: 1 l: 1 m: 1 n: 1 o: 1 p: 1 q: 1 r: 1 s: 1 t: 1 u: 1 v: 1 w: 1 x: 1 y: 1 z: 1 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdeabcdeABCDEXYXYXYZz a: 5 b: 5 c: 5 d: 5 e: 5 f: 2 g: 2 h: 2 i: 2 j: 2 k: 2 l: 2 m: 2 n: 2 o: 2 p: 2 q: 2 r: 2 s: 2 t: 2 u: 2 v: 2 w: 2 x: 5 y: 5 z: 4 1234567890-=`~!@#$%^&*()_+[]{}|';:,./?>< a: 5 b: 5 c: 5 d: 5 e: 5 f: 2 g: 2 h: 2 i: 2 j: 2 k: 2 l: 2 m: 2 n: 2 o: 2 p: 2 q: 2 r: 2 s: 2 t: 2 u: 2 v: 2 w: 2 x: 5 y: 5 z: 4 I have sworn upon the altar of God eternal hostility against every form of tyranny over the mind of man. -- Thomas Jefferson. a: 14 b: 5 c: 5 d: 7 e: 15 f: 8 g: 4 h: 7 i: 7 j: 3 k: 2 l: 5 m: 6 n: 11 o: 13 p: 3 q: 2 r: 10 s: 7 t: 11 u: 3 v: 5 w: 3 x: 5 y: 9 z: 4Explanation / Answer
.data
mainNumPhrases:
.word 4
mainPhrases:
.word mainPhrase1
.word mainPhrase2
.word mainPhrase3
.word mainPhrase4
mainPhrase1:
.asciiz "abcdefghijklmnopqrstuvwxyz"
mainPhrase2:
.asciiz "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdeabcdeABCDEXYXYXYZz"
mainPhrase3:
.asciiz "1234567890-=`~!@#$%^&*()_+[]{}|';:,./?><"
mainPhrase4:
.ascii "I have sworn upon the altar of God eternal hostility "
.ascii "against every form of tyranny over the mind of man. "
.asciiz "-- Thomas Jefferson."
colon_space:
.asciiz " : "
blank:
.asciiz "0 "
new_line:
.asciiz " "
.align 1
intarray:
.space 52
.text
main :
la $t0,mainPhrases # load the address to $t0
la $s0,mainNumPhrases
lb $s0,0($s0) # initialise the number of phrases count
start:
lw $t1,0($t0) # Load address of first phrase to t1
loop:
li $s3, 0
lb $s1,0($t1) # load character to s1
add $t1,$t1,1 #increment the phrase pointer
beq $s1,$s3,next #next phrase
addi $s3,$s3,0x60
bge $s1,$s3,adjust # if character is larger than 60 then subtract 60, case sensitivity
back:
subi $s3,$s3,0x20
ble $s1,$s3,loop #not an alpahbet next char
addi $s3,$s3,0x1A
bgt $s1,$s3,loop #not a character
subi $s1,$s1,0x41 #extract the offest of the char fron A
la $t2,intarray #load array address
add $s1,$s1,$s1
add $t2,$t2,$s1 #load the current count of that char
lh $s2,0($t2)
addi $s2,$s2,1 #increment
sh $s2,0($t2) #store back
j loop
InfLoop : j InfLoop # stay here for infinite time
next:
addi $t0,$t0,4 #increment the phrase pointer array
subi $s0,$s0,1 #decrement the number of phrases
j print # print
adjust:
subi $s1,$s1,0x20 #decrease by 0x20 for case change
j back
print:
addi $v0,$0,4
move $a0,$t0
subi $a0,$a0,4
lw $a0,0($a0)
syscall # print the phrase
la $a0,new_line
addi $v0,$0,4
syscall
la $t2,blank
la $t3,intarray
li $s4,0x1A # 26 chars to print
addi $s2,$0,0x41 # first print A
continue_print:
subi $s4,$s4,1
sb $s2,0($t2)
addi $v0,$0,4 # command to print integer is 1 to be loade to $v0
move $a0,$t2
syscall
la $a0, colon_space
syscall
addi $v0,$0,1
lh $a0,0($t3)
addi $t3,$t3,2
syscall # print the count
la $a0,new_line
addi $v0,$0,4
syscall
addi $s2,$s2,1
bgt $s4,$0,continue_print #continue to print until all the chars are done
bgt $s0,$0, start
j InfLoop
hope it helps...:)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.