Implement unsigned 8-bit multiplication and division algorithms in MIPS assembly
ID: 3546245 • Letter: I
Question
Implement unsigned 8-bit multiplication and division algorithms in MIPS assembly language. You need to follow the multiply and divide algorithms as given in the textbook. You must NOT use MIPS multiplication or division instructions.
The output of the program must include product, quotient and remainder. Also, your program must check for
Implement unsigned 8-bit multiplication and division algorithms in MIPS assembly language. You need to follow the multiply and divide algorithms as given in the textbook. You must NOT use MIPS multiplication or division instructions. The output of the program must include product, quotient and remainder. Also, your program must check for "0" as an input and must give error message for divide-by-zero operation. Also, ask the user if he wants to enter another set of inputs.Explanation / Answer
THIS WILL HELP YOU
For all 3 operand instructions, where only 2 appear in the source code, the first operand is both a source and the destination of the result.
cannot increase the number of operands.
dis always a register.
s2can be be a register or animmediate(a constant in the machine code)
Examples:
move $4, $9 # copy contents of $9 into $4
mul $12, $13, $14 # place 32-bit product of $13 and $14 into $12
# does not work correctly if result requires
# more than 32 bits
add $8, $9, $10 # two's complement sum of $9 and $10 placed in $8
add $20, $20, 1 # add (immediate value) 1 to the value in $20,
# result goes to $20
Note that there are other MAL arithmetic and logical instructions, but this list is sufficient for now.
On a real processor, an integer division operation gives two results: the quotient and the remainder. What happens with these two results differs among architectures.
Load and Store Instructions
On a load/store architecture, the only instructions that specify operands that come from or go to memory areloadsandstores.
mnemonic number operands operation
of operands
lw 2 d, addr a word is loaded from addr and placed into d;
the addr must be word aligned
lb 2 d, addr a byte is loaded from addr and placed into
the rightmost byte of d;
sign extension defines the other bits of d
lbu 2 d, addr a byte is loaded from addr and placed into
the rightmost byte of d;
zero extension defines the other bits of d
li 2 d, immed the immediate value is placed into d
sw 2 d, addr a word in d is stored to addr;
the addr must be word aligned
sb 2 d, addr a byte in the rightmost byte of d is stored to addr
la 2 d, label the address assigned to label is placed into d
Notes:
addris specified within the source code in one of 3 ways:
displacement(reg)
The immediate value (displacement) is added to the contents of theregto form an effective address.
(reg)
The contents of theregis the address.
label
The address is as assigned tolabel.
dmust be a register specification
Examples:
la $10, x # place the address assigned for label x into register $10
lw $8, x # load the word from memory at address x into register $8
sb $9, y # store the contents of register $9 to memory at address y
lb $8, ($12) # load the byte from the address given by the contents
# of register $12 into the least significant byte of
# register $8, sign extending to define the other bits
sw $10, 8($9) # store the contents of register $10 to the address
# obtained by adding the value 8 to the contents of
# register $9
Control Instructions
Control instructions are the branches and/or jumps.
The MIPS architecture implements only a subset of these, and tasks the assembler to synthesize thosenotincluded in the instruction set from those included. From this list, you cannot distinguish which are synthesized from those that are included.
mnemonic number operands operation
of operands
b 1 label unconditional branch to label
beq 3 r1, r2, label branch to label if (r1) == (r2)
bne 3 r1, r2, label branch to label if (r1) != (r2)
bgt 3 r1, r2, label branch to label if (r1) > (r2)
bge 3 r1, r2, label branch to label if (r1) >= (r2)
blt 3 r1, r2, label branch to label if (r1) < (r2)
ble 3 r1, r2, label branch to label if (r1) <= (r2)
beqz 2 r1, label branch to label if (r1) == 0
bnez 2 r1, label branch to label if (r1) != 0
bgtz 2 r1, label branch to label if (r1) > 0
bgez 2 r1, label branch to label if (r1) >= 0
bltz 2 r1, label branch to label if (r1) < 0
blez 2 r1, label branch to label if (r1) <= 0
j 1 label unconditional jump to label
jal 1 label unconditional jump to label, return address in $31
Examples:
beq $8, $14, do_else # branch to do_else if the contents of register $8
# is the same as the contents of register $14
Input and Output Instructions
Input and output are implemented by the simulator that we are using for the course. They are not actually instructions, but ways for the source code (instructions) to cause the simulator to do I/O functionality. Since the simulator was written in C, the easiest I/O functionality to mimic are simple, standard C I/O functions.
mnemonic number operands operation
of operands
putc 1 r1 print the ASCII character in the least
significant byte of r1
getc 1 r1 read a character, placing it in the least
significant byte of r1
puts 1 r1/label print the null-terminated string that begins
at the address within r1 or given by label
Examples:
.data
str1: .asciiz "hello."
.text
puts str1
-------------
|hello.
| ^
more output starts here, when more is printed
.data
str1: .asciiz "hello."
.text
la $12, str1 # address of first character in string
puts $12 # address of first character to print is in $12
-------------
|hello.
| ^
more output starts here, when more is printed
.data
str1: .asciiz "hello. My name is George."
.text
puts str1
-------------
|hello.
|My name is George.
| ^
more output starts here, when more is printed
.data
str1: .ascii "Hi. "
str2: .asciiz "I am a badger."
.text
puts str1