MIPS that multiplies $a0 (integer) by 16

Clash Royale CLAN TAG#URR8PPPMIPS that multiplies $a0 (integer) by 16
I am trying to figure out how the code below works and would gladly accept any help I can get. The question (with the solution) is posted below and I don't understand why they use sll and not mult (which I would). I have also posted my code but I assume that it is wrong.
sll
mult
Question:
Implement (code) an operation (function) that multiplies $a0 (integer) by 16 using native MIPS instructions (shifts, adds, subs, etc.). The result should be stored in $v0. You may use $t0, $t1, $t2 as scrap registers.
Their solution: sll $v0, $a0, 4 0
sll $v0, $a0, 4 0
My solution:
li $a0, 4
li $vo, 0
mult $a0, $a1
<<
1 Answer
1
Of course MULT can be used to multiply$a0 by 16 and the result can be put in $v0. That is a lot more complicated than using SLL though. With SLL as you can see it would just be one operation that accomplishes the entire task. With MULT, you would also have to load a register with 16 (not 4 obviously) and read the result back out of $LO (which you forgot). Doing any of that with pseudo-instructions such as LI or MOVE may violate the requirement to use "native MIPS instructions". The MUL pseudo-instruction would make that look shorter since you don't have to move-from-lo or get 16 into a register, but it's a macro that expands to the same thing, and may again violate the requirement to use "native MIPS instructions". Also MULT itself is typically not a single-cycle operation which is already a reason to avoid it in general (of course it has its uses).
MULT
$a0
$v0
SLL
SLL
MULT
$LO
LI
MOVE
MUL
MULT
SLL $v0, $a0, 4
Is the best solution overall. It's a single, fast instruction, that implements the task without extra stuff around it.
There are other possibilities without MULT though, such as using ADDU to implement shift-left-by-1 and then using it four times.
MULT
ADDU
Also, one pedagogical reason for setting problems like this, is to make you think about what the actual task is and how you could accomplish it, rather than just transliterating the requirements into code as literally as possible.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
division and multiplication by power of 2. In higher level languages you'll use a left shift operator like
<<– phuclv
3 mins ago