25.7k views
0 votes
(Assignment 1, individual) Create proc3.s Study the proc3.c and re-write the same program in MIPS with the following requirements: 1. Local variables mapping: a. main(): x -> $s0, y -> $s1 b. sum(): p -> $s0, q -> $s1 2. Input arguments mappings: a. sum(): m -> $a0, n-> $a1 b. sub(): a -> $a0, b-> $a1 3. All return values from a function must be stored in V registers in ascending order (i.e. $v0, $v1). 4. Use of stack memory according to register conven

1 Answer

4 votes

Answer:

text

.globl main

main:

li $s0,5 #load 5 to x

li $s1,10 #load 10 to y

move $a0,$s0

move $a1,$s1 #passing argument to sum function

jal sum

add $s1,$s1,$v0 #get y + sum(x,y)

add $s1,$s1,$s0 #get x+ y + sum(x,y)

li $v0,1

move $a0,$s1

syscall #print value of y

li $v0,10 #terminate call

syscall

sum:

addi $sp, $sp, 4

subu $sp,$sp,4 # point to the place for the new item,

sw $ra,($sp) # store the contents of $ra as the new top.

move $t1, $a0 #store parameters m

move $t2, $a1 #store parameters n

add $a0,$t2,1 #get n+1

add $a1,$t1,1 #get m+1

jal sub

move $t3,$v0 #store result to t3

sub $a0,$t1,1 #get m-1

sub $a1,$t2,1 #get n-1

jal sub

move $t4,$v0 #store result to t3

add $v0,$t3,$t4 #return p+q

lw $ra,($sp) # store the contents of $ra as the new top.

addu $sp,$sp,4 # point to the place for the new item,

addi $sp, $sp, 4

jr $ra

sub:

sub $v0,$a1,$a0 #return b-a

jr $ra

Step-by-step explanation:

text

.globl main

main:

li $s0,5 #load 5 to x

li $s1,10 #load 10 to y

move $a0,$s0

move $a1,$s1 #passing argument to sum function

jal sum

add $s1,$s1,$v0 #get y + sum(x,y)

add $s1,$s1,$s0 #get x+ y + sum(x,y)

li $v0,1

move $a0,$s1

syscall #print value of y

li $v0,10 #terminate call

syscall

sum:

addi $sp, $sp, 4

subu $sp,$sp,4 # point to the place for the new item,

sw $ra,($sp) # store the contents of $ra as the new top.

move $t1, $a0 #store parameters m

move $t2, $a1 #store parameters n

add $a0,$t2,1 #get n+1

add $a1,$t1,1 #get m+1

jal sub

move $t3,$v0 #store result to t3

sub $a0,$t1,1 #get m-1

sub $a1,$t2,1 #get n-1

jal sub

move $t4,$v0 #store result to t3

add $v0,$t3,$t4 #return p+q

lw $ra,($sp) # store the contents of $ra as the new top.

addu $sp,$sp,4 # point to the place for the new item,

addi $sp, $sp, 4

jr $ra

sub:

sub $v0,$a1,$a0 #return b-a

jr $ra

The above program takes in Local variables mapping: main(): x -> $s0, y -> $s1 b. sum(): p -> $s0, q -> $s1 Then Input arguments mappings: sum(): m -> $a0, n-> $a1 b. sub(): a -> $a0, b-> $a1

And return all values from a function which must be stored in V registers in ascending order.

User Nick Coons
by
4.4k points