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

6. (14 pts) Download h3-2.s from the course website. Read Appendir A which discu

ID: 3803721 • Letter: 6

Question

6. (14 pts) Download h3-2.s from the course website. Read Appendir A which discusses how local array variables are allocated in the stack frame of a function and how an array is passed as an argument in a function call. Let array be a 1-dimensional array of ints. The size of array, i.e., the number of elements in array, is specified by variable n. Shown below is the pseudocode for a function count() which has array and n as parameters. This function walks through array counting the number of negative values and the number of non-negative values in the array. It returns both counts. function count (array int n int) int, int int i, neg, mom meg for i 0 to n 1 do if array, 0 then neg neg 1 else nonneg onneg 1 end for return neg, nonneg end function search Complete the following exercises (name your source code file h3-2.s) a. Rewrite the pseudocode for count converting the for loop into a functionally-equivalent while loop. b. Rewrite the count0 function from Exercise 6a converting the code using a while loop into equivalent code that only uses if statements and goto's. Also, rewrite the if else statement inside the loops to only use an if statement and a goto. c. The rewritten court() function from 6b should now map directly onto the assembly language version of the func- tion. Write the function in assembly language. d. Test your program using the provided main() function in h3-2.s which allocates an array of 128 integers, with each element being a randomly-generated integer in the range H500, 500]. main calls count0 passing the address of a 0 in the $a0 register. count() must return the values of local variables neg and nonneg in the $v0 and $v1 registers. With the pseudorandom number generator seed 1234, the array a should contain 67 negative integers and 33 non-megative integers.

Explanation / Answer

main: # Allocate stack frame addi $sp, $sp, -524 # Allocate 131 words in stack frame # SysSetSeed(1, 1234) addi $v0, $zero, SYS_SET_SEED # $v0 = SysSetSeed service code addi $a0, $zero, 1 # $a0 = pseudorandom number generator id addi $a1, $zero, 1234 # $a1 = seed syscall # SysSetSeed(1, 1234) # for i = ... sw $zero, 512($sp) # i = 0 addi $t9, $zero, 127 # $t9 = index of last element of a addi $t8, $zero, 500 # $t8 = constant 500 main_loop_begin: lw $t0, 512($sp) # $t0 = i bgt $t0, $t9, main_loop_end # if i > 127 drop out of loop # a[i] = SysRandInt(1, 1000) - 500 addi $v0, $zero, SYS_RAND_INT_RNG # $v0 = SysRandInt service code addi $a0, $zero, 1 # $a0 = pseudorandom number generator id addi $a1, $zero, 1000 # $a1 = upper limit of range [0, 1000] syscall # SysRandInt(1, 1000) sub $t1, $a0, $t8 # $a0 = SysRandInt(1, 1000) - 500 sll $t0, $t0, 2 # $t0 = 4i add $t0, $sp, $t0 # $t0 = a + 4i = &a[i] sw $t1, 0($t0) # a[i] = SysRandInt(1, 1000) - 500 # i = i + 1 lw $t0, 512($sp) # $t0 = i addi $t0, $t0, 1 # $t0 = i + 1 sw $t0, 512($sp) # i = i + 1 j main_loop_begin # continue looping main_loop_end: move $a0, $sp # $a0 = &a addi $a1, $zero, 128 # $a1 = 128 jal count # Call count(a, 128) sw $v0, 516($sp) # neg = first return value sw $v1, 520($sp) # nonneg = second return value # SysPrintStr ("Number of negative ints: ") addi $v0, $zero, SYS_PRINT_STR # $v0 = SysPrintStr service code la $a0, s_neg # $a0 = address of string to be printed syscall # Call SysPrintStr(...) # SysPrintInt (neg) addi $v0, $zero, SYS_PRINT_INT # $v0 = SysPrintInt service code lw $a0, 516($sp) # $a0 = neg syscall # SysPrintInt(neg) # SysPrintChar(' ') addi $v0, $zero, SYS_PRINT_CHAR # $v0 = SysPrintChar service code addi $a0, $zero, ' ' # $a0 = ' ' syscall # SysPrintChar(' ') # SysPrintStr ("Number of non-negative ints: ") addi $v0, $zero, SYS_PRINT_STR # $v0 = SysPrintStr service code la $a0, s_nonneg # $a0 = address of string to be printed syscall # Call SysPrintStr(...) # SysPrintInt (nonneg) addi $v0, $zero, SYS_PRINT_INT # $v0 = SysPrintInt service code lw $a0, 520($sp) # $a0 = nonneg syscall # SysPrintInt(nonneg) # SysPrintChar(' ') addi $v0, $zero, SYS_PRINT_CHAR # $v0 = SysPrintChar service code addi $a0, $zero, ' ' # $a0 = ' ' syscall # SysPrintChar(' ') # Call SysExit() addi $sp, $sp, 524 # Deallocate stack frame addi $v0, $zero, SYS_EXIT # $v0 = SysExit service code syscall # Call SysExit() # count()

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote