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

Write simple MIPS assembly code and simulate it using the simulator that was int

ID: 3687164 • Letter: W

Question

Write simple MIPS assembly code and simulate it using the simulator that was introduced in the lab (QtSpim). Refer to the tutorial post on Blackboard. 1.Write MIPS assembly code for swapping the contents of two registers, $t0 and $tl. (Similar example in the tutorial) 2.Write MIPS assembly code to reverse the bits in a register. Assume the register of interest is St3. 3.Write MIPS assembly code to print your name in the following format on the screen/console.Format: My name is Bond, James Bond. So it should be: My name is "your last name", "your first name last name".4.Write MIPS assembly code to sum "n" positive integers. For example, n=5, read 5 numbers in memory and add them together. (Similar example in the tutorial) 5.Write MIPS assembly code to calculate N-factorial. Read n from the memory.Lab Assignment: Provide comments throughout your code. Write a report, which includes: 1.MIPS Code for each program 2.Output Screen shot for registers/console for each program. 3.Conclusion.

Explanation / Answer

Sample code logic for swapping

.text

main;

li $t0,0x100

li $t1,0x200

move $t2,$t0

move $t0,$t1

move $t1,$t2

li$v0,10                       #terminate program
syscall                             # return control to system

Sample code logic for sum of “n” positive integers.

.text
main:           li        $v0, 4          # system call code for print_str
                   la       $a0, prompt # load address of prompt into a0
                   syscall                             # print the prompt message
                   li        $v0, 5          # system call code for read_int
                   syscall                             # reads a value of N into v0

                   blez    $v0, done    # if ( v0 < = 0 ) go to done
                   li        $t0, 0           # clear $t0 to zero

loop:            add    $t0, $t0, $v0          # sum of integers in register $t0
                   addi   $v0, $v0, -1 # summing in reverse order
                   bnez   $v0, loop               # branch to loop if $v0 is != zero

                   li        $v0, 4          # system call code for print_str
                   la       $a0, result    # load address of message into $a0
                   syscall                             # print the string

                   li        $v0, 1          # system call code for print_int
                   move $a0, $t0                 # a0 = $t0
                   syscall        

b        main

done: li        $v0, 4          # system call code for print_str
                   la       $a0, bye                # load address of msg. into $a0
                   syscall                             # print the string

          li $v0, 10              # terminate program
                   syscall                             # return control to system

Sample code logic to calculate N-factorial
.text
main:
# printing the prompt
#printf("Positive integer: ");
la $t0, msgprompt # load address of msgprompt into $t0
lw $a0, 0($t0) # load data from address in $t0 into $a0
li $v0, 4 # call code for print_string
syscall # run the print_string syscall

# reading the input int
# scanf("%d", &number);
li $v0, 5 # call code for read_int
syscall # run the read_int syscall
move $t0, $v0 # store input in $t0

move $a0, $t0 # move input to argument register $a0
addi $sp, $sp, -12 # move stackpointer up 3 words
sw $t0, 0($sp) # store input in top of stack
sw $ra, 8($sp) # store counter at bottom of stack
jal factorial # call factorial

# when we get here, we have the final return value in 4($sp)

lw $s0, 4($sp) # load final return val into $s0

# printf("The value of 'factorial(%d)' is: %d ",
la $t1, msgres1 # load msgres1 address into $t1
lw $a0, 0($t1) # load msgres1_data value into $a0
li $v0, 4 # system call for print_string
syscall # print value of msgres1_data to screen

lw $a0, 0($sp) # load original value into $a0
li $v0, 1 # system call for print_int
syscall # print original value to screen

la $t2, msgres2 #load msgres2 address into $t1
lw $a0, 0($t2) # load msgres_data value into $a0
li $v0, 4 # system call for print_string
syscall # print value of msgres2_data to screen

move $a0, $s0 # move final return value from $s0 to $a0 for return
li $v0, 1 # system call for print_int
syscall # print final return value to screen

addi $sp, $sp, 12 # move stack pointer back down where we started

# return 0;
li $v0, 10 # system call for exit
syscall # exit!

.text
factorial:
# base case - still in parent's stack segment
lw $t0, 0($sp) # load input from top of stack into register $t0
#if (x == 0)
beq $t0, 0, returnOne # if $t0 is equal to 0, branch to returnOne
addi $t0, $t0, -1 # subtract 1 from $t0 if not equal to 0

# recursive case - move to this call's stack segment
addi $sp, $sp, -12 # move stack pointer up 3 words
sw $t0, 0($sp) # store current working number into the top of the stack segment
sw $ra, 8($sp) # store counter at bottom of stack segment

jal factorial # recursive call

# if we get here, then we have the child return value in 4($sp)
lw $ra, 8($sp) # load this call's $ra again(we just got back from a jump)
lw $t1, 4($sp) # load child's return value into $t1

lw $t2, 12($sp) # load parent's start value into $t2
# return x * factorial(x-1); (not the return statement, but the multiplication)
mul $t3, $t1, $t2 # multiply child's return value by parent's working value, store in $t3.

sw $t3, 16($sp) # take result(in $t3), store in parent's return value.

addi $sp, $sp, 12 # move stackpointer back down for the parent call

jr $ra # jump to parent call

.text
#return 1;
returnOne:
li $t0, 1 # load 1 into register $t0
sw $t0, 4($sp) # store 1 into the parent's return value register
jr $ra # jump to parent call

note- the rest of the problems can solve in the same way by using the above sample logics and with required modifications it can help to answer the given question.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote