MIPS 装配作业 帮助!

发布于 2024-08-12 17:05:21 字数 4731 浏览 4 评论 0原文

我最亲密的朋友正在上 EE 课程(我是他最后的希望:/),我大约 7 年前就开始了解 Java,但是他的 (大纲)最新的 EE 编程作业是使用 MIPS 程序集执行以下操作:

编写一个采用两个正整数(m和 n) 并计算:

x= (m^n) - (1+2+3+…+n) * min(m,n)!

两个整数都应大于零。 我不允许使用任何 R 类型算术指令(add、mult、sub)。相反,我要使用其他指令为其函数编写代码??? “你的程序应该在每次计算后继续获取 m 和 n 的新值,直到用户输入零,这将是你的程序的结束。”

我无法访问他以前的任何作业,并且尝试在不使用 (add, mult, sub) 的情况下一头扎进汇编语言对我来说效果不太好。

ece.ucdavis.edu/~vojin/CLASSES/EEC70/W2001/pr4.pdf 教授似乎正在使用他在加州大学戴维斯分校任教时的 ols 作业。

//编辑 这是问题的 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;
}

输入.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 

My closest friend is going through an EE course (I'm his last hope : /), I have knowledge of Java from about 7 years ago, but his (outline) latest EE programming assignment is to use the MIPS Assembly to do the following:

Write a program that takes two positive integers (m and n) and computes:

x= (m^n) - (1+2+3+…+n) * min(m,n)!

Both the integers should be greater than zero.
I'm not allowed to use any R-type arithmetic instructions (add, mult, sub). Instead I'm to write the code for their functions using other instructions????
"Your program should continue getting new values for m and n after each computation until the user enters zero which would be the end for your program."

I do not have access to any of his previous assignments and trying to dive head 1st into assembly language WITHOUT using (add, mult, sub) isn't working out for me too well.

ece.ucdavis.edu/~vojin/CLASSES/EEC70/W2001/pr4.pdf
Prof seemed to be using an ols assignment from when he was teaching at UC Davis.

//edit
Here is a c++ version of the problem, it does not cover all of the assignment's bases, but it's a starting point:

#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 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴者 2024-08-19 17:05:21

不幸的是,如果这是 C 代码,那么 MIPS 代码将至少比它长 4 倍,而且考虑到限制,还要长得多。这段代码可能需要一段时间,但我可以给出一个伪演练:

x = (m^n) - (1+2+3+…+n) * min(m,n)!

首先让我们注意,就像在任何其他编程语言中一样,我们应该将任务划分为尽可能多的方法。在MIPS中,我们希望将任务划分为尽可能多的“过程”(标签)。我们已经可以看到,我们需要一个指数、阶乘等过程。我们从 m^n 开始。我们不能使用 mult,因为它是 R 型,但我们可以使用 multi,它是 I 型。我们可以使用所有直接 I 类型来替代此赋值。所以我们可以这样做:例如在 for 循环中使用 multi m,m 并执行 n 次,给出 m^n (在每次迭代中,我们将使用指令 mfhi 和 mflo 从乘法寄存器 Hi 和 Lo 中移动结果,用于计算,这样它们就不会被覆盖)。这将是我们的第一个程序。

现在我们看1+2+3+...+n。这还不错,我们只是继续使用addi(一条I型add指令,将“立即”常量添加到寄存器中),例如addi $s1,$s1,b,其中a是1,在a中递增循环并与结果相加 n 次,从而得到 1+2+3+...+n。

最后,我们需要另一个过程来将其乘以 min(m,n)!。阶乘是递归的,因此我们需要一个递归过程来调用自身并充分利用内存堆栈和寄存器溢出(IE 首先将过程参数和返回地址保存到堆栈上,进入主体,再次调用本身)。我认为这足以让您开始。不确定作业何时到期,但祝你好运!

Unfortunately, if that is the C code, then the MIPS code will be at least 4X longer than that and much longer given the restrictions. This code may take a while but I can give a pseudo walk-through:

x = (m^n) - (1+2+3+…+n) * min(m,n)!

Let's first note, like in any other programming language, we should divide our task into as many methods as possible. In MIPS, we want to divide the task into as many "procedures" (labels) as possible. We can already see that we will need a procedure for exponential, factorials, etc. We start with m^n. We can't use mult since it is R-type, but we can use multi, which is I-type. We can use all of the immediate I-types as substitutes for this assignment. So we could do: multi m,m for example in a for loop and do this n times, giving m^n (in each iteration we would have move the results from the multiply registers Hi and Lo, using the instructions mfhi and mflo, for calculation so they don't get overwritten). This would be our first procedure.

Now we look at the 1+2+3+...+n. This is not too bad, we just keep using addi (an add instruction of type-I that adds "immediate" constants to a register) such as addi $s1,$s1,b, where a is 1, which is incremented in a loop and added to the result n times, thus getting 1+2+3+...+n.

Finally, we need another procedure to multiply this by min(m,n)!. Factorial is recursive, so we'll need a recursive procedure that calls itself and makes good use of the memory stack and register spilling (IE first saving the procedure arguments and return address onto the stack, enter the body, the call itself again). I think this is enough to get you started. Not sure when the assignment is due but good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文