Write an assembly program that takes two values, a and b, multiplies them, and then stores the result in c. When the program begins: • a is located in $fp + 1 • b is located in $fp + 2 At the end of the program: • a is located in $fp + 1 • b is located in $fp + 2 • c is located in $fp + 3 In doing this, there must be at least one function, namely, one that takes two parameters and returns the result of multiplying them. You may use additional functions to make things easier if you would like to. A possible approach The following is a possible approach to doing the multiplication, expressed in C++ code.
int larger(int p1, int p2) { if (p1 > p2) return p1; }
//*************************
int smaller(int p1, int p2) { if (p1 < p2) return p2; }
//*************************
int rsltSign(int p1, int p2) { if (((p1 < 0) && (p2 > 0)) || ((p1 > 0) && (p2 < 0))) return 1; return 0; }
//*************************
int absValue(int p) { if (p < 0) return -p; return p; }
//*************************
int mult(int p1, int p2)
{ int result = 0, rsltSign, small, large;
rsltSign = (p1, p2);
p1 = absValue(p1);
p2 = absValue(p2);
small = smaller(p1, p2);
large = larger(p1, p2);
for (int i = 0; i < small; i++)
result += large;
if (rsltSign == 1) result = -result;
return result; }
This is all I got right now what I have below
lw $s0, $fp, 1
lw $s1, $fp, 2
#result
add $s2, $zero, $zero
#rsltSign
add $a0, $zero, $zero
#small
add $t0, $zero, $zero
#large
add $t1, $zero, $zero
#p
add $a1,$zero,$zero
#c
add $t2, $zero, $zero
addi $v0, $zero, rsltSign
jalr $vo, $ra
add $a1, $a1, $s0
addi $v0, $zero, absValue
jalr $vo, $ra
add $s0, $zero, $zero
add $s0, $s0, $vo
add $a1, $zero, $zero
add $a1, $a1, $s1
addi $v0, $zero, absValue
jalr $v0, $ra
add $s1, $zero, $zero
add $s1, $a1, $v0
addi $v0, $zero, check
jalr $v0, $ra
addi $v0, $zero, check2
jalr $v0, $ra
add $a2, $zero, $zero
addi $v0, $zero, loop
jalr $v0, $ra
addi $v0, $zero, checksign
jalr $v0, $ra
addi $v0, $zero, end
jalr $v0, $ra
#rsltSign
rsltSign: blt $s0, $zero, next
blt $zero, $s0, next 2
add $a0, $zero, $zero
jalr $ra, $zero
#absValue
.file 1 ""
.section .mdebug.abi32
.previous
.nan legacy
.module fp=32
.module nooddspreg
.abicalls
.text
.align 2
.globl larger
.set nomips16
.set nomicromips
.ent larger
.type larger, @function
larger:
.frame $fp,8,$31 # vars= 0, regs= 1/0, args= 0, gp= 0
.mask 0x40000000,-4
.fmask 0x00000000,0
.set noreorder
.set nomacro
addiu $sp,$sp,-8
sw $fp,4($sp)
move $fp,$sp
sw $4,8($fp)
sw $5,12($fp)
movz $31,$31,$0
lw $3,8($fp)
lw $2,12($fp)
nop
slt $2,$2,$3
beq $2,$0,$L2
nop
lw $2,8($fp)
b $L1
nop
$L2:
$L1:
move $sp,$fp
lw $fp,4($sp)
addiu $sp,$sp,8
j $31
nop
.set macro
.set reorder
.end larger
.size larger, .-larger
.align 2
.globl smaller
.set nomips16
.set nomicromips
.ent smaller
.type smaller, @function
smaller:
.frame $fp,8,$31 # vars= 0, regs= 1/0, args= 0, gp= 0
.mask 0x40000000,-4
.fmask 0x00000000,0
.set noreorder
.set nomacro
addiu $sp,$sp,-8
sw $fp,4($sp)
move $fp,$sp
sw $4,8($fp)
sw $5,12($fp)
movz $31,$31,$0
lw $3,8($fp)
lw $2,12($fp)
nop
slt $2,$3,$2
beq $2,$0,$L5
nop
lw $2,12($fp)
b $L4
nop
$L5:
$L4:
move $sp,$fp
lw $fp,4($sp)
addiu $sp,$sp,8
j $31
nop
.set macro
.set reorder
.end smaller
.size smaller, .-smaller
.align 2
.globl rsltSign
.set nomips16
.set nomicromips
.ent rsltSign
.type rsltSign, @function
rsltSign:
.frame $fp,8,$31 # vars= 0, regs= 1/0, args= 0, gp= 0
.mask 0x40000000,-4
.fmask 0x00000000,0
.set noreorder
.set nomacro
addiu $sp,$sp,-8
sw $fp,4($sp)
move $fp,$sp
sw $4,8($fp)
sw $5,12($fp)
movz $31,$31,$0
lw $2,8($fp)
nop
bgez $2,$L8
nop
lw $2,12($fp)
nop
bgtz $2,$L9
nop
$L8:
lw $2,8($fp)
nop
blez $2,$L10
nop
lw $2,12($fp)
nop
bgez $2,$L10
nop
$L9:
li $2,1 # 0x1
b $L11
nop
$L10:
move $2,$0
$L11:
move $sp,$fp
lw $fp,4($sp)
addiu $sp,$sp,8
j $31
nop
.set macro
.set reorder
.end rsltSign
.size rsltSign, .-rsltSign
.align 2
.globl absValue
.set nomips16
.set nomicromips
.ent absValue
.type absValue, @function
absValue:
.frame $fp,8,$31 # vars= 0, regs= 1/0, args= 0, gp= 0
.mask 0x40000000,-4
.fmask 0x00000000,0
.set noreorder
.set nomacro
addiu $sp,$sp,-8
sw $fp,4($sp)
move $fp,$sp
sw $4,8($fp)
lw $2,8($fp)
movz $31,$31,$0
nop
bgez $2,$L13
nop
lw $2,8($fp)
nop
subu $2,$0,$2
b $L14
nop
$L13:
lw $2,8($fp)
$L14:
move $sp,$fp
lw $fp,4($sp)
addiu $sp,$sp,8
j $31
nop
.set macro
.set reorder
.end absValue
.size absValue, .-absValue
.align 2
.globl mult
.set nomips16
.set nomicromips
.ent mult
.type mult, @function
mult:
.frame $fp,56,$31 # vars= 24, regs= 2/0, args= 16, gp= 8
.mask 0xc0000000,-4
.fmask 0x00000000,0
.set noreorder
.cpload $25
.set nomacro
addiu $sp,$sp,-56
sw $31,52($sp)
sw $fp,48($sp)
move $fp,$sp
.cprestore 16
movz $31,$31,$0
sw $4,56($fp)
sw $5,60($fp)
sw $0,24($fp)
lw $2,60($fp)
nop
sw $2,32($fp)
lw $4,56($fp)
lw $2,%got(absValue)($28)
nop
move $25,$2
.reloc 1f,R_MIPS_JALR,absValue
1: jalr $25
nop
lw $28,16($fp)
sw $2,56($fp)
lw $4,60($fp)
lw $2,%got(absValue)($28)
nop
move $25,$2
.reloc 1f,R_MIPS_JALR,absValue
1: jalr $25
nop
lw $28,16($fp)
sw $2,60($fp)
lw $5,60($fp)
lw $4,56($fp)
lw $2,%got(smaller)($28)
nop
move $25,$2
.reloc 1f,R_MIPS_JALR,smaller
1: jalr $25
nop
lw $28,16($fp)
sw $2,36($fp)
lw $5,60($fp)
lw $4,56($fp)
lw $2,%got(larger)($28)
nop
move $25,$2
.reloc 1f,R_MIPS_JALR,larger
1: jalr $25
nop
lw $28,16($fp)
sw $2,40($fp)
sw $0,28($fp)
b $L16
nop
$L17:
lw $3,24($fp)
lw $2,40($fp)
nop
addu $2,$3,$2
sw $2,24($fp)
lw $2,28($fp)
nop
addiu $2,$2,1
sw $2,28($fp)
$L16:
lw $3,28($fp)
lw $2,36($fp)
nop
slt $2,$3,$2
bne $2,$0,$L17
nop
lw $3,32($fp)
li $2,1 # 0x1
bne $3,$2,$L18
nop
lw $2,24($fp)
nop
subu $2,$0,$2
sw $2,24($fp)
$L18:
lw $2,24($fp)
move $sp,$fp
lw $31,52($sp)
lw $fp,48($sp)
addiu $sp,$sp,56
j $31
nop
Write an assembly program that takes two values, a and b, multiplies them, and then stores...
The following MIPS assembly code contains a mistake that violates the MIPS convention in terms of using stack for storing/protecting registers. What is the mistake and how should it be fixed? Correct the corresponding lines in the code. For the corrected code, sketch the stack frame contents at the time when the instruction ‘move $s1, $a1’ is being executed. f: addi $sp, $sp, 12 sw $ra, 8($sp) sw $s1, 4($sp) sw $s0, 0($sp) move $s0, $a0 move $s1, $a1 #...
The task will be to write a program in assembler to enter a number and calculate its associated Fibonacci number using a procedure (subroutine) that is called recursively. Factorial and Fibonacci(outline Programs) # MIPS assembly assembly assemblyassemblycode .data n: .word 4 .text main: la $s0,n lw $a0, 0($s0) jal factorial # move what ever is returned into $a0 move $a0, $v0 li $v0,1 syscall b finished factorial: add $sp , $sp , -8 # make room sw $a0, 4($sp )...
.data prompt: .asciiz "Input an integer x:\n" result: .asciiz "Fact(x) = " .text main: # show prompt li $v0, 4 la $a0, prompt syscall # read x li $v0, 5 syscall # function call move $a0, $v0 jal factorial # jump factorial and save position to $ra move $t0, $v0 # $t0 = $v0 # show prompt li $v0, 4 la $a0, result syscall # print the result li $v0, 1 # system call #1 - print int move $a0,...
WRITE THE FOLLOWING CODE IN FLOATING POINT NUMBERS IN ASSEMBLY LANGUAGE USING MIPS IN MARS .data prompt: .asciiz "\nMaximum number is : " prompt1: .asciiz "\nMinimum number is : " prompt2: .asciiz "\nRange of the array is : " size: .word 10 #load array array: .word 23, -12, 45, -32, 52, -72, 8, 13,22,876 .text #load address of array and size la $s4,array #load address of A lw $t0,size #load i to t0 jal getArrayRange li $v0, 4 la...
Subroutines in MIPS Determines the minimum of two integers Functions within the MIPS slides describe how one can use subroutines (also called procedures, functions, and methods) in MIPS. Because of the importance of subroutines in modern programming, most hardware designers include mechanisms to help programmers. In a high-level language like C or Java, most of the details of subroutine calling are hidden from the programmer. MIPS has special registers to send information to and from a subroutine. The registers $a0,...
(12 pts) The following code fragment processes an array and produces two important values in registers $v0 and $v1. Assume that the array consists of 5000 words indexed 0 through 4999, and its base address is stored in $a0 and its size (5000) in $a1. Describe what this code does. Specifically, what will be returned in $v0 and $v1? add $al, şal, $al add $al, şal, $al add $v0, şzero, $zero add $to, şzero, $zero add $t4, $a0, $to lw...
I have this MIPS program and I'm having trouble with it. This program is user inputs numbers until zero and sorts and print the numbers in order. Please soove this issue. You can use any sorting algorithm except bubble sort. Need it as soon as possible. Here is the code:.datanum: .word 0space: .byte ' ' .text main: # la $t0, val # loads val into a register # li $t1, 0 #keeps track of how many numbers entered la $a0,...
im trying to complete mips program code about a calculator program that can calculate integer addition / subtraction written using the MIPS assembler. im having hard times to debug this. The input is given to the array of Formula char (base address $ s0) in the form of a formula. The null character (\ 0, ASCII code 0) is placed at the end. The calculation result is given to the register $ s1 and the overflow is ignored. For example,...
Turn the Following c-code into MIPS assembly code. You are given the main procedure which calls multiply. You are also given the argument registers to be used. /* C-program */ int multiply (int number, int times) { int f; f = number * times return f; } # MIPS PROGRAM # assumes we have called the program leaf_example # $a0=number, $a1=times, $s0=f main: $addi $a0,$zero,3 $addi $a1,$zero,2 jal multiply # place the address into $ra j EXIT2 EXIT2: j OS...
Write a MIPS program with the following specifications: Use the my_mul function in question 1 to create a function which computes factorials: n! = n. (n - 1). … 3 .2 . 1 Note: You can only use my_mul function, and not use other functions). Each function must save all variables it modifies. below is my_mul my_mul : #multiply $a0 with $a1 #does not handle negative $a1 ! #Note : This is an inefficient way to multiply ! addi $sp,...