LAB 5 Arithmetic Operations Simple Calculator Objective: Practice arithmetic operation for the 80x86, such as add, subtract, multiple, divide, and mod. When dealing with the multiply, divide, and mod instructions on the 80x86, you must use the AL/AX/EAX and DX/EDX registers. You cannot use arbitrary registers as you can with other operations, such as addition and subtraction. Learn how to write a function in the programming. Use ASM (a computer assembly language program) to write the computer programs. You studied eight 32-bit registers that have the following names: EAX, EBX, ECX, EDX, ESI, EDI, EBP, and ESP; the eight 16- bit registers that have the following names: AX, BX, CX, DX, SI, DI, BP, and SP; and eight 8-bit registers that have the following names: AL, AH, BL, BH, CL, CH, DL, and DH. Design your programming to complete the following arithmetic operation with signed number and unsigned number: The output of program will have a main menu asking the user to 1 Signed Integer Arithmetic Operation (16-bit) 2 Exit After you choose the main menu 2, the program will exit the program. After you choose the main menu 1, a submenu will ask the user to select a. Input two 16-bit signed number operands for addition, and display the sum in the format of decimal and hexadecimal number, respectively. b. Input two 16-bit signed number operands for subtraction and display the difference in the format of decimal and hexadecimal number, respectively. c. Input two 16-bit signed number operands for multiplication and display the product in the format of decimal and hexadecimal number, respectively. d. Input two 16-bit signed number operands for division and display the quotient in the format of decimal and hexadecimal number, respectively (option). e. Input two unsigned number operands for addition, and display the sum in the format of decimal and hex, respectively 1
f. Input two unsigned number operands for subtraction and display the difference in the format of decimal and hex, respectively. g. Input two unsigned number operands for multiplication and display the product in the format of decimal and hex, respectively. h. Input two unsigned number operands for division and display the quotient and remainder in the format of decimal and hex, respectively. After selecting each of the submenus, for example, user selects the submenu of (b), the program asks the user to enter the two numbers. After successful performing the operations, the program will display results in the output: For example, Enter the first sighed integer number in decimal format (for example 12): Enter the second sighed integer number in decimal format (for example -2): For the decimal format, the sum of the 12 and -2 is 10. The identical Hex value for decimal value 10 is 0x000A shown in 16-bit (you may show this information by cout<<hex<< value) The program will ask the user Continue your integer calculator Y/N, enter 'Y' or 'N' If the user chooses Y or y, the program will return to main menus; if the user chooses N or n, the program will exit the program. 2
Background: Type Instruction Example Meaning Addition ADD ADD AX, 0x7b AX <- AX+7B INC INC BX BX <- BX+1 Subtraction SUB SUB CL, 0xA CL <- CL-A DEC DEC CX CX <- CX-1 NEG NEG CX CX <- 0-CX Multiplication MUL (unsigned multiply) MUL CL MUL CX unsigned AX <- AL*CL unsigned (DX::AX) <- AX*CX IMUL (signed multiply) IMUL CL signed AX <- AL*CL IMUL op16 signed (DX::AX) <- AX*op16 Division Performs both division and modulus operations DIV (Unsigned divide) DIV BH unsigned AL <- Q(AX/BH) AH <- R(AX%BH) DIV op16 unsigned AX <- Q( DX::AX/ op16 ) DX <- R(DX::AX % op16) IDIV (signed divide) IDIV BH signed AL <- Q(AX/BH) AH <- R (AX%BH) IDIV op16 signed AX <- Q( DX::AX / op16 ) DX <- R(DX::AX % op16) 3
Review how to write source code from previous lab. In this lab, you can follow the sample code to write your source code //part of calculator source code //calculator.cpp #include <stdio.h> #include <iostream> using namespace std; void addition (short int x, short int y); // addition for signed integers void subtraction (short int x, short int y); // subtraction for signed integers void multiplication (short int x, short int y); // multiplication for signed integers void division ( int x, short int y); // division for signed integers void addition (unsigned short int x, unsigned short int y); // addition for unsigned integers void subtraction (unsigned short int x, unsigned short int y); // subtraction for unsigned integers void multiplication (unsigned short int x, unsigned short int y); // multiplication for unsigned integers void division ( unsigned int x, unsigned short int y); // division for signed integers int main() //more variables should be declared char ch1, ch2, ch3; unsigned short ur1, ur2, ur; signed short r1, r2, r; // more declaration here cout << "Start your calcalator Y/N, enter 'Y (or y)' or 'N( or n)' "<< endl; cin>> ch1; ch1 = ch1; while (ch1=='y' ch1=='y') printf ("Menu:\n"); printf ("1, 16-bit Integer Arithmetic Operation\n"); printf ("2, Exit\n"); printf ("Menu Options:\n "); std::cin >> ch2; ch2 = ch2; //Submenu: if (ch2=='1') printf ("Submenu - input your choice\n"); printf ("(a) Input two signed number operands for addition, and display the sum in the format of decimal and hex, respectively.\n"); printf ("(b) Input two signed number operands for subtraction and display the difference in the format of decimal and hex, respectively.\n"); printf ("(c) Input two signed number operands for multiplication and display the product in the format of decimal and hex, respectively.\n"); printf ("(d) Input two signed number operands for division and display the quotient and remainder in the format of decimal and hex, respectively.\n"); 4
printf ("(e) Input two unsigned number operands for addition, and display the sum in the format of decimal and hex, respectively.\n"); printf ("(f) Input two unsigned number operands for subtraction and display the difference in the format of decimal and hex, respectively.\n"); printf ("(g) Input two unsigned number operands for multiplication and display the product in the format of decimal and hex, respectively.\n"); printf ("(h) Input two unsigned number operands for division and display the quotient and remainder in the format of decimal and hex, respectively.\n"); else goto EndLable ; std::cin >> ch3; ch3 = ch3; //your code for signed number and unsighed number arithmatic operations switch (ch3) case 'a': printf ("Input two signed number operands in decimal format\n"); scanf_s("%d %d", &r1, &r2); //read two signed numbers from keyboard addition (r1, r2); printf ("======================================================\n"); break; case 'b': printf ("Input two signed number operands in decimal format\n"); scanf_s("%d %d", &r1, &r2); //read two signed numbers from keyboard subtraction (r1, r2); printf ("======================================================\n"); break; // continue your source code cout << "Continue your integer calculator Y/N. Enter: 'Y' or 'N' "<< endl; cin>> ch1; ch1 = ch1; EndLable: cout << "Exit program"<< endl; //getchar(); system ("pause"); //hold screen return 0; void addition (short int x, short int y) short int r; 5
_asm MOV AX, x; MOV BX, y; ADD AX, BX; MOV r, AX; cout<< "The decimal sum of "<<x <<" and "<<y << " is " << r<< endl; cout<< "The hexadecimal sum of "<<hex<<x <<" and "<< hex<< y << " is " << hex<< r<< endl; void subtraction (short int x, short int y) short int r; _asm MOV AX, x; MOV BX, y; SUB AX, BX; MOV r, AX; cout<< "The decimal subtraction of "<<x <<" minus "<<y << " is " << dec<< r<< endl; cout<<"the hexadecimal subtraction of "<< hex<<x <<" minus " <<hex<<y << " is " <<hex<<r<< endl; // continue your source code for each function declared at the top of the programming // continue your source code for each function declared at the top of the programming void multiplication (short int x, short int y) // multiplication for signed integers void division ( int x, short int y) // division for signed integers void addition (unsigned short int x, unsigned short int y) // addition for unsigned integers void subtraction (unsigned short int x, unsigned short int y) // subtraction for unsigned integers void multiplication (unsigned short int x, unsigned short int y) // multiplication for unsigned integers void division ( unsigned int x, unsigned short int y) // division for signed integers 6