0

Are my comments for this MIPS program accurately explaining what each statement line is doing?

.data
str1: .asciiz "Enter the first integer: "
str2: .asciiz "Enter the second integer: "
str3: .asciiz "The sum is "
newline: .asciiz "\n"

.text       # instructions follow this line 
main:   # indicates start of code (first instruction to execute)

addi $v0, $zero, 4 
  # adds zero and imm value 4 and stores in 32-bit function result registers

la $a0, str1 
  #load system register $a0, with the address of the string to be output
syscall

addi $v0, $zero, 5
   # adds zero and imm value 5 and stores in 32-bit function result registers
syscall
add $s0, $zero, $v0
   #adds 0 and value in $v0 and stores in $s0
addi $v0, $zero, 4
   # adds zero and imm value 4 and stores in 32-bit function result registers
la $a0, str2
   #load system register $a0, with the address of the string to be output
syscall

addi $v0, $zero, 5
   # adds zero and imm value 5 and stores in 32-bit function result registers
syscall 
add $s1, $zero, $v0
   #adds 0 and value in $v0 and stores in $s1
add $s2, $s0, $s1
   # adds value in $s0 and value in $s1 and stores in $s2
addi $v0, $zero, 4
   # adds zero and imm value 4 and stores in 32-bit function result registers
la $a0, str3
   # load system register $a0, with the address of the string to be output
syscall

addi $v0, $zero, 1
   # adds zero and imm value 1 and stores in 32-bit function result registers
add $a0, $zero, $s2
   # adds value in $s2 and 0 and stores in system register $a0
syscall

addi $v0, $zero, 4
   # adds zero and imm value 4 and stores in 32-bit function result registers
la $a0, newline
   # load system register $a0, with the address of the string to be output
syscall

addi $v0, $zero, 10
   # adds zero and imm value 10 and stores in 32-bit function result registers
syscall
jr  $ra  
   # jump to address contained in $ra

What does the syscall do, because I've seen different comments for it online? Also, if I wanted to modify this program to print the first integer the amount of time the second integer is, how would I do so? For example: 1st: 2, 2nd: 5, so I print 2 5 times.

Stack Player
  • 1,470
  • 2
  • 18
  • 32
Honinbo Shusaku
  • 1,411
  • 2
  • 27
  • 45

1 Answers1

1

In general syscall instruction on MIPS causes an exception which is handled by operating system. What OS does depends on OS and specific ABI used. Usually, user set up some parameters in specific registers (determined by convention/ABI in use) and performs some 'privileged' operation on behalf of the user.

Considering that you didn't provide specifics of the environment your code is supposed to be executed on, I can't tell you what specific parameter passing convention is used and what OS (or simulator) would do for particular parameters you're passing.

ArtemB
  • 3,496
  • 17
  • 18
  • The execution environment here is the SPIM emulator. –  Mar 13 '14 at 23:10
  • Then Konrad has already pointed you towards the page with the details on what SPIM expects to see in the registers when syscall instruction is executed. Different SPIM versions seem to have different subset of supported functions, so you will need to consult documentation of the SPIM version you're using. Or experiment with different values to see what's supported. In general you can think of 'syscall' as a way to call a subroutine in SPIM. $v0 selects particular function to call and $a0..$a3 provide parameters for that function. – ArtemB Mar 14 '14 at 17:06