Write a complete MIPS assembly language program that implements the following ps
ID: 3794427 • Letter: W
Question
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 main
end program h2
Miscellaneous program requirements and hints:
Write the code so its output and behavior would match mine shown below, where user input is in bold:
Enter an integer >= 0 for w? 9 Enter an integer >= 0 for x? 17 Enter an integer < 0 for y? -5 z = 415
De ne the four variables w, x, y, and z as words in the .data section. Initialize each to 0.
Place the string literals in the .data section using the .asciiz directive.
Information about the MARS system calls can be found by selecting Help | Help on the main menu (or hit F1). In
the Help window, click on the Syscalls tab. Information on MARS-implemented MIPS32 instructions can be found by clicking on the Basic Instructions and Extended (pseudo) Instructions tabs. Information on MARS-impl- emented assembler directives can be found by clicking on the Directives tab. For this program you will need to use the following directives: .data for the data section; .text for the text section; .word for the de nitions of the inte- ger variables; and .asciiz to place the strings in the data section.
For my solution, I used these MIPS32 instructions so familiarize yourself with them: add (addition), addi (addi- tion with an immediate), div (division), lw (load word), mul (multiplication), sll (shift logical left), sub (sub- traction), sw (store word), syscall (perform system call). I also used these pseudoinstructions: la (load address), and neg (negate). Note: MARS implements many non-standard pseudoinstructions, documented in the Help sys- tem. You are not permitted to use these non-standard pseudoinstructions in your homework assignments or exams. The only instructions you may use are those that we discussed and are documented in the notes. The rea
Explanation / Answer
.data //tells the assembler that we are in data segment
msg1: .asciiz "Enter an integer >=0 for w ?"
msg2: .asciiz "Enter an integer >=0 for x"
msg3: .asciiz "Enter an integer <0 for y"
msg4: .asciiz "z = "
w: .word 0
x: .word 0
y: .word 0
z: .word 415
.text //tells the assembler that we are in text segment
main:
# promt the user to enter w
li $v0 , 4
la $a0, msg1
syscall
#get the user input w
li v0, 5
syscall
move w, v0 // move it to w
# promt the user to enter x
li $v0 , 4
la $a0, msg2
syscall
#get the user input x
li v0, 5
syscall
move x, v0 // move input to x
# promt the user to enter x
li $v0 , 4
la $a0, msg3
syscall
#get the user input x
li v0, 5
syscall
move y, v0 // move the input to y
// to perform the modulo op
move t1, -y
move t2, 7
div t1, t2
mfhi t3 // t3 = t1%t2
add r1, w, x
mul r1, r1, 16
mul r2, 3, t3
sub r1, r1, r2
move z, r1 // move the final result into z
#print z=
li $v0 , 4
la $a0, msg4
syscall
//print the value of z
li $v0, 1
lv $a0, z
syscall
exitProgram: li $v0, 10 # system call to
syscall # terminate program
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.