11210 CS 410000 Homework 3

49 阅读2分钟

Dowload link:# 11210 CS 410000 Homework 3

Description

5/5 – (2 votes)

There are two parts in this homework.

PART I. – Procedure call

In this part, you are goint to write a MPS assembly program for the following C program.

  1. #include <stdio.h>
  1. #include <math.h>
  1. int compare(int p, int q){
  1. if(p > q) return p + q;
  1. else return p;
  1. }
  1. int smod(int p , int q){
  1. int div, divd;
  1. if(p > q) div = 2 + pow(2, p%4);
  1. else div = 4 + pow(2, q%4);
  1. div = div * 5;
  1. divd = p * 4 + q;
  1. return divd % div;
  1. }

17

  1. int main(){
  1. int x, y, z, ans;
  1. printf(“input x: “);
  1. scanf(“%d”, &x);
  1. printf(“input y: “);
  1. scanf(“%d”, &y);
  1. printf(“input z: “);
  1. scanf(“%d”, &z);
  1. ans = smod(compare(x, y), z);
  1. printf(“result = %d\n”, ans);
  1. return 0;
  1. }

Input constraints:                     

Output format example:

You must use the procedure (function) call to implement. Also, your program should terminal normally (the output should show “– program is finished running — “).

PART II. – Recursive call

In this part, you are going to write a MIPS assembly program that traces the step-by-step processes of the Towers of Hanoi puzzle and calculates the total number of movements.

The example C code and Input, Output format is shown below.

Tower of Hanoi (moving disks from A to B)

Example C program

  1. #include <stdio.h>
  1. int cnt = 0;
  1. void MoveTower(int disk, char source, char dest, char spare) {
  1. if(disk == 0) {
  1. // Move disk from source to dest
  1. printf(“Move disk %d from %c to %c\n”, disk, source, dest);
  1. ++cnt;
  1. }
  1. else {
  1. // Move the smaller disk from source to spare
  1. MoveTower(disk – 1, source, spare, dest);

14

  1. // Move disk from source to dest
  1. printf(“Move disk %d from %c to %c\n”, disk, source, dest);
  1. ++cnt;

18

  1. // Move the smaller disk from spare to dest
  1. MoveTower(disk – 1, spare, dest, source);
  1. }
  1. }

23

  1. int main() {
  1. int numDisks;
  1. printf(“Please input the total number of disks: “);
  1. scanf(“%d”, &numDisks);
  1. MoveTower(numDisks – 1, ‘A’, ‘B’, ‘C’);

29

  1. printf(“Total number of movement = %d\n”, cnt);
  1. return 0;
  1. }

Input constraints:        

Output format example:

You must use the procedure (function) call to implement. Also, your program should terminal normally (the output should show “– program is finished running — “).

Submission (2 assembly programs)

Please name your assembly program with your student ID in the following format:

arch_hw3_p1_<student_ID>.asm

arch_hw3_p2_<student_ID>.asm

Use the eeclass (eeclass.nthu.edu.tw/) to submit your programs.

Grading Criteria

Correctness: 80%

Comment in program: 10%

Output format: 10%

Remember Plagiarism is strictly prohibited.

Appendix

  1. www.cs.cmu.edu/~cburch/sur…