Write an ASCII character counter for an array in MIPS assembly language, using s
ID: 3791626 • Letter: W
Question
Write an ASCII character counter for an array in MIPS assembly language, using subrutines.
The starting address of the array of ASCII characters must be passed to the subroutine in $a0. The 32-bit unsigned length of the array of ASCII characters must be passed to the subroutine in $a1. The one byte ASCII character to search for must be passed to the subroutine in $a2.
If the value to search for is valid, the subroutine must count the number of times that the character appears in the supplied ASCII array and return it to the caller as a signed word in register $v0. If the value to search for is invalid , the subroutine must return the value -1 to the caller in register $v0.
The subroutine must preserve the values of the $sX registers, and it cannot use any values from the main program that are not explicitly passed to it as arguments.
Explanation / Answer
The purpose of memory is to store groups of bits, and deliver them (to the processor for loading into registers) upon demand. Most present-day computers store information in multiples of 8 bits, called a byte (or octet). Most also assign a numeric address to each byte. This is convenient because characters can be stored in bytes.
Memory addresses are 32-bit numbers, ranging from 0x00000000 to 0xFFFFFFFF. This is a large amount of memory, most computers do not have actual memory for all of this "address space."
Memory can hold both program instructions and data. One function of the operating system is to assign blocks of
memory for the instructions and data of each process (running program). Another thing a good operating system does is to allow many processes to run concurrently on the computer.
The SPIM simulator always assigns your program to these fixed, even numbered locations, for your convenience:
0x00400000 - Text segment - program instructions
0x10000000 - Data segment
0x7FFFFFFF, and decreasing addresses - Stack segment
A word generally means the number of bits that can be transferred at one time on the data bus, and stored in a register. In the case of MIPS, a word is 32 bits, that is, 4 bytes.
Words are always stored in consecutive bytes, starting with an address that is divisible by 4. Caution: other processors, other definitions.
Some people refer to 16 bits as a word, to others it may mean 64 bits.
he program counter (PC) always holds the address of the next instruction. Normally it is incremented every time an instruction is executed. This controls the flow of the program.
There are 32 general purpose registers, numbered 0..31. In assembly language, they also have symbolic names, which are shown in the register window of the SPIM simulator. These names suggest conventions for use of the registers. Registers may be referred to by name or number in assembly language, for instance, register 4 can be referred to as $4 or $a0, and is usually used for an argument to a procedure.
The first and last registers are special:
$0 is a constant 0, and cannot be changed
$31 stores the return address from a procedure (function) call. It is named $ra.
lw $t0, num1 #load word in num1 into $t0
sw $t0, num2 #store word in $t0 into num2, ie. num2 := num1
li $v0, 4 #load immediate value (constant) 4 into $v0
Operations: Arithmetic and Logic : perform the operation on data in 2 registers, store the result in a 3rd register
example: add $t0, $t3, $t4 # $t0 := $t3 + $t4
Arithmetic is done in registers. There are 3 operands to an arithmetic operation:
1.destination register
2.Source 1 register
3.Source 2 register or constant
The compact way of writing this pattern is: add Rdest, Rsrc1, Src2
The following code calculates the expression (x + 5 - y) * 35 / 3
lw $t0, x #load x from memory
lw $t1, y #load y from memory
add $t0, $t0, 5 # x + 5
sub $t0, $t0, $t1 # x + 5 - y
mul $t0, $t0, 35 #(x + 5 - y)*35
div $t0, $t0, 3 #(x + 5 - y)*35/3
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.