This problem is adapted from an earlier edition of P&H.
Consider the following code used to implement the instruction: foo $s0,$s1,$s2
mask: .word 0xFFFFF83F
start: la $t0,mask
lw $t0,0($t0)
la $s0,shftr
lw $s0,0($s0)
and $s0,$s0,$t0
andi $s2,$s2,0x1f
sll $s2,$s2,6
or $s0,$s0,$s2
la $t5,shftr
sw $s0,0($t5)
shftr: sll $s0,$s1,0
Add meaningful comments to the code. Please explain each line and write a paragraph describing how it works. Why do you suppose that writing “self-modifying code” such as this is a bad idea (and often times not actually allowed)?
Please explain it. thank you.
Note: We have not covered the .word directive yet. This is an assembler directive, which is essentially an instruction to the assembler (NOT an assembly language instruction). In this case, it tells the assembler to reserve a word of memory initialized to 0xfffff83f. Mask is the address of this word (just as start is the address of the word in memory containing the machine code for that lw instruction).
Answer
mask: .word 0xFFFFF83F
start: la $t0,mask //$t0 = 0xfffff83f, 1111111...100000111111 ( meaning word is loaded to register t0 )
lw $t0,0($t0) //load word at RAM address ($t0+0) into register $t0
la $s0,shftr //$s0 = sll instruction( shifting to left) means loading the shift instruction.
lw $s0,0($s0) // clear the shamt
and $s0,$s0,$t0 //clears the shamt field in the sll instruction stored in $s0
andi $s2,$s2,0x1f //clear all but the last 5 bits in $s2
sll $s2,$s2,6 //move those bits to the position that corresponds to shamt
or $s0,$s0,$s2 //set the shamt field in $s0 as the value from $s2
la $t5,shftr // load all sll instruction in register t5
sw $s0,0($t5) //store the sll instruction back into memory
shftr: sll $s0,$s1,0 //shifts the contents of $s0 left 0 which is stored in $s0. perform the modified instr.
The last shift instruction is modified so that its shamt field is written by the 5 LSBs from $s2. It's actually modifying the instruction in-memory to perform the shift.
self modifying means we are changing code generated by the compiler which will make our code machine dependent.because of this it is bad idea as it complicates the instruction cache implementation, the instruction cache is usually read only cache. Also it makes detecting defects that overwrite code harder to detect.
This problem is adapted from an earlier edition of P&H. Consider the following code used to...
WRITE THE FOLLOWING CODE IN FLOATING POINT NUMBERS IN ASSEMBLY LANGUAGE USING MIPS IN MARS .data prompt: .asciiz "\nMaximum number is : " prompt1: .asciiz "\nMinimum number is : " prompt2: .asciiz "\nRange of the array is : " size: .word 10 #load array array: .word 23, -12, 45, -32, 52, -72, 8, 13,22,876 .text #load address of array and size la $s4,array #load address of A lw $t0,size #load i to t0 jal getArrayRange li $v0, 4 la...
Please comment the MIPS code to help me understand. Here is what the code accomplishes. Here is the code, partially commented. .data Matrix: .word 41,45,5, 34,8, 15,16,23,44,48,12,32,18,47,22,8,22 .word 46,40,42,33,13,38,27,6, 29,25,18,40,47,22,26,14,3 .word 7, 48,35,9, 43,38,9, 49,28,25,42,5, 44,10,5, 38,14 .word 46,33,16,6, 13,20,31,1, 8, 17,1, 47,28,46,14,28,7 .word 32,2, 48,25,41,29,14,39,43,46,3, 39,32,49,41,28,46 .word 5, 43,2, 48,13,4, 33,41,32,19,9, 25,30,22,2, 9, 40 .word 14,47,22,18,47,3, 35,44,18,6, 33,22,11,6, 47,50,4 .word 28,34,20,30,18,27,38,5, 26,40,37,23,16,13,37,8,7 .word 48,38,39,12,10,39,23,20,21,20,33,16,24,21,25,3,46 .word 49,38,40,38,13,47,5, 13,4, 13,23,26,12,30,29,29, 3 .word 8, 20,10,13,31,7, 12,41,12,21,28,26,43,14,35,10,19 .word 49,33,25,26,24,29,46,22,7, 5, 15,41,10,31,19,41,27 .word 48,9,...
Please answer the following questions involving MIPS assembly code: A) For the C statement below, what is the corresponding MIPS assembly code? Assume f, g are stored in S1 and S2. f = g + (-f -5) B) For the C statement below, what is the corresponding MIPS assembly code? Assume i and j are assigned in registers S1 and S2 respectively and base of address of arrays A and B are in registers S6 and S7. B[8] = A[i...
im trying to complete mips program code about a calculator program that can calculate integer addition / subtraction written using the MIPS assembler. im having hard times to debug this. The input is given to the array of Formula char (base address $ s0) in the form of a formula. The null character (\ 0, ASCII code 0) is placed at the end. The calculation result is given to the register $ s1 and the overflow is ignored. For example,...
I want to calculate Y[2]=X[3]+X[4] I'm not sure that I wrote right codes. .text main . la $50, x #get base address of x la $s1, y #get base address of y lw $to, 12 ($50)#get data from memory of x[3] lw $t1 , 1 6($50) #get data from memory of x[4] add $t2, $t0, $t1 sw $t2, 8($51 ) #store result to y[2] li $v0, 10 #exit program syscall data x: word 5, 1, 17,-4, 6, 3 y: .word...
There is an example below Now that everything is working you can try the following exercises. To complete them you will need to refer to the documentation in Appendix A The MiteASM Assembler and Appendix B The MiteFPGA Processor. Write an assembly language program for an over counter for a cricket umpire. This should 1. display a count on the 7-segment display. The count should increase by 1 when button 0 is pressed. It should reset to 0 when button...
5 Exercises Now that everything is working you can try the following exercises. To complete them you will need to refer to the documentation in Appendix A- The MiteASM Assembler and Appendix B - The MiteFPGA Processor. Write an assembly language program for an over counter for a cricket umpire. This should display a count on the 7-segment display. The count should increase by 1 when button 0 is 1. pressed. It should reset to 0 when button 1 is...
Create a program that performs the following operations: 1. Prompt for and accept a string of up to 80 characters from the user. • The memory buffer for this string is created by: buffer: .space 80 #create space for string input The syscall to place input into the buffer looks like: li $v0,8 # code for syscall read_string la $a0, buffer #tell syscall where the buffer is li $a1, 80 # tell syscall how big the buffer is syscall 2....
Write a complete MIPS assembly language program that implements the following pseudocode. program h2 define global integer variables w, x, y, z -- in the .data section function main() SysPrintStr("Enter an integer >= 0 for w? ") w ← SysReadInt() SysPrintStr("Enter an integer >= 0 for x? ") x ← SysReadInt() SysPrintStr("Enter an integer < 0 for y? ") y ← SysReadInt() z ← 16(w + x) - (3 × -y mod 7) SysPrintStr("z = ") SysPrintInt(z) SysExit() end function...