我最好的朋友正在上EE课程(我是他最后的希望: /),我从7年前就知道Java了,但是他的 (outline) latest EE programming assignment is to use the MIPS Assembly to do the following:
写一个程序,它接受两个正整数(m和n)并计算:
x= (m^n) - (1+2+3+…+n) * min(m,n)!两个整数都应大于零。我不允许使用任何R型算术指令(加法、乘法、减法)。相反,我将使用其他指令为他们的函数编写代码?“您的程序应该在每次计算后继续获取m和n的新值,直到用户输入零,这将是您的程序的结束。”
我无法接触到他之前的任何任务,并且尝试着不使用(add,mult,sub)而直接深入到汇编语言中,对我来说效果不是很好。
ece.ucdavis.edu/~vojin/CLASSES/EEC70/W2001/pr4.pdf教授似乎使用了他在加州大学戴维斯分校任教时的ols作业。
//edit这里是问题的c++版本,它没有涵盖赋值的所有基础,但它是一个起点:
#include <iostream.h>
//x = (m^n) - (1+2+3+...+n) * ((min(m,n))!)
int m; //User Input
int n; //User Input
double answer; //Answer yo.
int findMin(int, int); //Takes 2 int inputs and outputs the smallest int.
int minFound; //Function output
double factorial(int); //Do eet.
double factOutput; //Function output
double sumN(int); //1+2+3+...+n
double sumFound; //Function output
double expMtoN(int, int); //m^n, float for number size,
double expFound; //Function output, float for number size,
int main(void)
{
cout << "Please enter a positive integer (m): ";
cin >> m;
//Escape if zero.
if ( m == 0)
{
cout << "User input for \"m\" is equal to zero; escape on zero." << endl;
return 0;
}
cout << "Please enter a positive integer (n): ";
cin >> n;
//Escape if zero.
if ( n == 0)
{
cout << "User input for \"n\" is equal to zero; escape on zero." << endl;
return 0;
}
expFound = expMtoN(m, n); //m^n
sumFound = sumN(n); //1+2+3+...+n
minFound = findMin(m, n); //Takes 2 int inputs and outputs the smallest int.
factOutput = factorial(minFound); //Factorial math for minFound (z!)
answer = expFound - sumFound * factOutput; //x = (m^n) - (1+2+3+...+n) * ((min(m,n))!)
cout << endl;
cout << m << " raised to the power of " << n << " is: " << expFound << endl;
cout << "Sum of " << n << " is: " << sumFound << endl;
cout << "Lowest number out of " << m << " and " << n << " is: " << minFound << endl;
cout << minFound << " factorial is: " << factOutput << endl;
cout << endl << "x = (m^n) - (1+2+3+...+n) * ((min(m,n))!)" << endl;
cout << "x = " << answer << endl;
}
//all temp variables below are confined to their respective functions.
//return functions output temp into variable from main.
double expMtoN(int userBase, int userExp)
{
double temp = 1; //Must establish 1 so you are not multiplying by zero.
for ( int i = 1; i <= userExp; i++ )
temp *= userBase;
return temp;
}
double sumN(int userN)
{
double temp = 0;
for ( int i = 1; i <= userN; i++ )
temp = temp + i;
return temp;
}
int findMin(int userM, int userN)
{
if( userM <= userN )
return userM;
else
return userN;
}
double factorial(int minFound)
{
double temp;
if ( minFound <= 1 )
return 1;
temp = minFound * factorial(minFound - 1);
return temp;
}
Input.s
;-----------------------------------------------------------------------------
;Subprogram call by symbol "InputUnsigned"
;expect the address of a zero-terminated prompt string in R1
;returns the read value in R1
;changes the contents of registers R1,R13,R14
;-----------------------------------------------------------------------------
.data
;*** Data for Read-Trap
ReadBuffer: .space 80
ReadPar: .word 0,ReadBuffer,80
;*** Data for Printf-Trap
PrintfPar: .space 4
SaveR2: .space 4
SaveR3: .space 4
SaveR4: .space 4
SaveR5: .space 4
.text
.global InputUnsigned
InputUnsigned:
;*** save register contents
sw SaveR2,r2
sw SaveR3,r3
sw SaveR4,r4
sw SaveR5,r5
;*** Prompt
sw PrintfPar,r1
addi r14,r0,PrintfPar
trap 5
;*** call Trap-3 to read line
addi r14,r0,ReadPar
trap 3
;*** determine value
addi r2,r0,ReadBuffer
addi r1,r0,0
addi r4,r0,10 ;Decimal system
Loop: ;*** reads digits to end of line
lbu r3,0(r2)
seqi r5,r3,10 ;LF -> Exit
bnez r5,Finish
subi r3,r3,48 ;´0´
multu r1,r1,r4 ;Shift decimal
add r1,r1,r3
addi r2,r2,1 ;increment pointer
j Loop
Finish: ;*** restore old register contents
lw r2,SaveR2
lw r3,SaveR3
lw r4,SaveR4
lw r5,SaveR5
jr r31 ; Return 发布于 2011-03-14 13:49:55
不幸的是,如果这是C代码,那么MIPS代码将至少比它长4倍,而且在受到限制的情况下会更长。这段代码可能需要一段时间,但我可以给出一个伪演练:
X= (m^n) - (1+2+3+…+n) * min(m,n)!
首先,让我们注意到,就像在任何其他编程语言中一样,我们应该将任务划分为尽可能多的方法。在MIPS中,我们希望将任务划分为尽可能多的“过程”(标签)。我们已经看到,我们将需要一个用于指数、阶乘等的过程。我们从m^n开始。我们不能使用mult,因为它是R-type,但我们可以使用multi,它是I-type。我们可以使用所有直接的i-type来代替这个赋值。所以我们可以这样做:多个m,例如在一个for循环中,这样做n次,给出m^n (在每次迭代中,我们将使用mfhi和mflo指令从乘法寄存器Hi和Lo中移动结果,以便计算,这样它们就不会被覆盖)。这将是我们的第一个程序。
现在我们看看1+2+3+...+n,这还不错,我们只需要继续使用addi (一种类型为i的加法指令,将“立即”常量添加到寄存器中),比如addi $s1,$s1,b,其中a是1,它在循环中递增,并被加到结果中n次,从而得到1+2+3+...+n。
最后,我们需要另一个过程将其乘以min(m,n)!。阶乘是递归的,所以我们需要一个递归过程来调用自己,并充分利用内存堆栈和寄存器溢出(即首先将过程参数和返回地址保存到堆栈上,进入主体,再次调用本身)。我想这足以让你入门了。不确定作业什么时候到期,但祝你好运!
https://stackoverflow.com/questions/1831507
复制相似问题