5
\$\begingroup\$

This code computes the function (3x^2-4x+16) / (5x^2+2x-4). I ran the program and it works, but I am fairly new to assembly language and am not quite sure how to make the most efficient use of the registers. Does this look ok or is there a better way to do it?

.text
.globl main
main:
ori 8,ドル 0,ドル 3 #put x into 8ドル
ori 9,ドル 0,ドル 3 #puts 3 into 9ドル
ori 10,ドル 0,ドル 4 #puts 4 into 10ドル
ori 11,ドル 0,ドル 16 #puts 16 into 11ドル
mult 8,ドル 8ドル #Squares x
mflo 13ドル #13ドル = x^2
mult 9,ドル 13ドル #Computes 3x^2
mflo 14ドル # 14ドル = 3x^2
mult 10,ドル 8ドル # lo = 4x
mflo 15ドル # 9ドル = 4x
sub 16,ドル 14,ドル 15ドル # 16ドル = 3x^2 -4x
add 17,ドル 16,ドル 11ドル #17ドル = 3x^2 - 4x + 16 
ori 8,ドル 0,ドル 1 #put x into 8ドル
ori 9,ドル 0,ドル 5 #puts 5 into 9ドル
ori 10,ドル 0,ドル 2 # put 2 into 10ドル
ori 11,ドル 0,ドル 4 # puts 4 into 11ドル
mult 8,ドル 8ドル #Squares x
mflo 13ドル #13ドル = x^2
mult 9,ドル 13ドル #Computes 5x^2
mflo 14ドル # 14ドル = 5x^2
mult 10,ドル 8ドル # lo = 2x
mflo 15ドル # 9ドル = 2x
add 16,ドル 14,ドル 15ドル # 16ドル = 5x^2 +4x
sub 18,ドル 16,ドル 11ドル #17ドル = 5x^2 + 2x - 4
div 17,ドル 18ドル #divides 2 functions
mflo 8ドル #quotient
mfhi 9ドル #remainder
## End of file
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Feb 27, 2013 at 22:53
\$\endgroup\$
3
  • \$\begingroup\$ I won't provide you any assembly advice but would it be relevant to compile some simple C/C++ code and check the corresponding assembly code. Here's my attempt : int main(int x,char *a){return (3*xx-4*x+16)/(5*x*2+2*x-4);}. This is stupid code but I wanted to keep it simple. (You can check the assembly output with gcc.godbolt.org ) \$\endgroup\$ Commented Feb 28, 2013 at 0:25
  • \$\begingroup\$ use $zero, $s... and $t... for registers. makes code more readable and robust. en.wikipedia.org/wiki/MIPS_architecture#Compiler_register_usage \$\endgroup\$ Commented Feb 28, 2013 at 7:44
  • \$\begingroup\$ As long as you do not do load or store, what difference does it make if you use 7 temp registers or 8? \$\endgroup\$ Commented Feb 28, 2013 at 7:48

1 Answer 1

3
\$\begingroup\$

There are multiple issues:

first, try to make the program work: These two lines are contradictory

ori 8,ドル 0,ドル 3 #put x into 8ドル
ori 8,ドル 0,ドル 1 #put x into 8ドル

Perhaps the real idea is to move a function parameter to 8ドル.

The second thing is redundant calculation: Instead of squaring x twice, you should be able to

mul 8,ドル 8ドル
mflo 13ドル
mflo 14ドル

To catch the result in two registers.

There's no need to reserve registers for all the immediates, as one can at least add the last coefficients 16 and (-4) with

addi $x, $x, 16

Multiplications with small constants are also often better executed with a series of shifts and adds. Especially here x*4 equals x<<2 and x*2 == x+x, which leads to one less instruction for both operations.

answered Feb 28, 2013 at 11:13
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.