1

im learning mips and i dont understand what to put inside the (complete the codes) section and my teacher confuses the hell outta everyone so its a struggle trying to understand therefore any help is welcomed

# Power program
# ----------------------------------
# Data Segment
.data
var_x:      .word   3

var_y:      .word   5   
result:     .word   0

# ----------------------------------
# Text/Code Segment

.text
.globl  main
main:
    lw  $a0, var_x  # load word var_x in memory to $a0
    lw  $a1, var_y  # load word var_y in memory to $a1
    jal power       # call the function power   
    sw  $v0, result # save word from $v0 to result in memory

    # complete other codes here to print result in console window

    # -----
    # Done, exit program.
    li  $v0, 10     # call code for exit
    syscall         # system call
.end main

# ----------------------------------
#   power
# arguments:    $a0 = x
#       $a1 = y
# return:   $v0 = x^y
# ----------------------------------

.globl  power
power:  
        add   $t0,$zero,$zero   # initialize $t0 = 0, $t0 is used to record how many times we do the operations of multiplication
        addi $v0,$v0,1          # set initial value of $v0 = 1
power_loop: beq $t0, $a1, exit_L    
        mul $v0,$v0,$a0         # multiple $v0 and $a0 into $v0 
        addi $t0,$t0,1          # update the value of $t0   
        j   power_loop
exit_L:     jr  $ra
.end    power
  • Looks like all that is expected is to print the result? Do you have any problems with that? – Jester Oct 30 '19 at 12:56
  • in the #complete other codes here to print result in console window line under that do I have to write anything is what is confusing me because #lines are provided by the teachers so it means that I have to write something for the code – Failed Programmer Oct 30 '19 at 13:01
  • 1
    Yes, you need to write code that prints the result. It says so in the comment. – Jester Oct 30 '19 at 13:04
  • oh,i misread the earlier post my bad and may I ask what do I write since im pretty bad at mips – Failed Programmer Oct 30 '19 at 13:06
  • MARS has a system call for printing integers. Read the docs, and/or google `site:stackoverflow.com print integer mips` – Peter Cordes Oct 30 '19 at 15:27

1 Answers1

0

First, you need to move your sum from $v0 to $a0. Then, just append this to your code to print the results in the output box/console.

add $a0, $v0, $zero     # copy the value into the arg register $a0
#print the result (in $a0) in decimal to the output
li  $v0, 1              # service 1 is print integer
syscall                 # print_int($a0)
A P
  • 2,131
  • 2
  • 24
  • 36