{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

a4solns

# a4solns - Department of Electrical Engineering Course...

This preview shows pages 1–4. Sign up to view the full content.

Department of Electrical Engineering Course ECSE-221, Fall 2003 Introduction to Computer Engineering I Assignment 4 Introduction to Assembly Language Programming Due: Wednesday, November 12 th , 2003 Question 1 Using the code on Pages 33 and 34 of the Module 1 notes (as a guide), write a “C” language function to perform binary division according to the following template: int binarydivision (long dividend, long divisor, long *quotient, long *rmdr); The function returns -1 in the event of a divide by zero error, and 0 on successful completion. In addition to checking for a zero divisor, your algorithm must also handle signs correctly. Pay particular attention to the sign of the remainder. Recall that divisor * quotient + remainder = dividend in all cases. Validate your code by writing a suitable test program. To make implementation in Question 2 easier, it is suggested that you prepare a table of test cases that your main program iterates through. Question 2 Implement the binary division function you wrote in Question 1 in MIPS assembly code, assuming the following convention for passing arguments: Register Argument Mechanism \$4 dividend pass by value \$5 divisor pass by value \$6 quotient pass by value \$7 rmdr pass by value Although this is not quite the convention used by a “C” compiler (quotient and rmdr are pointers and would otherwise by passed by reference), we will use it here since argument passing mechanisms have not been dealt with in detail at this point in the course. In coding your function it is absolutely essential that the context of the calling program is fully preserved. You will also need to write a suitable test program. The SPIM environment includes a number of SYSCALL functions for printing strings and integers. Test your functions with the same table of test cases that you used in Question 1. Your results should be identical. FPF/October 29, 2003

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
div32.c 11/11/03 1:27:18 PM Page 1 /*----------------------------------------------------------------------*/ /* File Name: div32.c */ /* Description: A simple package to support binary division of long */ /* integers using the subtract and shift procedure. The */ /* main is a command line procedure for testing the */ /* div32 function which implements the algorithm. */ /* Author: F.P. Ferrie */ /* Date: September 12, 2002. */ /* Modifications: None */ /*----------------------------------------------------------------------*/ /*----------------------------------------------------------------------*/ /* Includes */ /*----------------------------------------------------------------------*/ #include <stdio.h> #include <stdlib.h> /*----------------------------------------------------------------------*/ /* Defines /*----------------------------------------------------------------------*/ #define MSBLONG 0x40000000 /* mask for msb position in long int */ #define DIV0 -1 /* return code for divide by zero */ #define SUCCESS 0 /* return code for success */ #define BADARG -2 /* Can 't handle -2147483648 */ #define LONGSIZE sizeof(long)*8 /* size of long (bits) */ /*----------------------------------------------------------------------*/ /* Prototypes */ /*----------------------------------------------------------------------*/ int binarydivision(long dividend,long divisor,long *quotient,long *rmdr); /*----------------------------------------------------------------------*/ /* Main - fetch arguments from command line and do rudimentary check. */ /* Invoke the binary division function and display the result */ /* on stdout. */ /*----------------------------------------------------------------------*/ int main(int argc,char *argv[]) { long dividend,divisor,quotient,rmdr; int status; if (argc != 3) { printf("Usage: div32 [dividend] [divisor]\n"); exit(0); } /*--------------------------------------------------------------------*/ /* Arguments 1 and 2 correspond to the dividend and divisor */ /* respectively. Use sscanf to convert each to a long int. */ /*--------------------------------------------------------------------*/ sscanf(argv[1],"%ld",&dividend); sscanf(argv[2],"%ld",&divisor); /*--------------------------------------------------------------------*/
div32.c 11/11/03 1:27:18 PM Page 2 /* Display results and return. */ /*--------------------------------------------------------------------*/

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}