Question

In: Computer Science

Write an Assembly code to input two integer numbers from keyboard, computes the division of two...

Write an Assembly code to input two integer numbers from keyboard, computes the division of two numbers WITHOUT using division operator and print out the reminder and quotient to the screen.

Note: program using “division operator” will earn no credit for this task. You are ALLOWED to use the “print” and “read” macro

Solutions

Expert Solution

An assembly language is a low-level language understood by the machine. Any C/C++ program is converted into an assembly language in a compiler. The platform I used was Ubuntu, Virtual Box.

In order to convert any code from C to assembly, use the following syntax in the command prompt:

$ gcc -S -o filename.asm filename.c

$ gedit filename.asm

C program :

#include <stdio.h>
#include <limits.h>

// Function to perform division (x / y) of two numbers x and y without
// using division operator in the code
int divide(int x, int y)
{
   // handle divisibility by 0
   if (y == 0)
   {
       printf("Error!! Divisible by 0");
       exit(1);
   }

   // store sign of the result
   int sign = 1;
   if (x * y < 0)
       sign = -1;

   // convert both dividend and divisor to positive
   x = abs(x), y = abs(y);

   // initialize quotient by 0
   int quotient = 0;

   // loop till dividend x is more than the divisor y
   while (x >= y)
   {
   x = x - y;       // perform reduction on dividend
   quotient++;       // increase quotient by 1
   }

   printf("Remainder is %d\n", x);

   return sign * quotient;
}

// main function to perform division of two numbers
int main(void)
{
   int dividend = 5;
   int divisor = 2;

   printf("Quotient is %d\n", divide(dividend, divisor));

   return 0;
}

Assembly Code:

   .file   "P0.c"
   .section   .rodata
.LC0:
   .string   "Error!! Divisible by 0"
.LC1:
   .string   "Remainder is %d\n"
   .text
   .globl   divide
   .type   divide, @function
divide:
.LFB2:
   .cfi_startproc
   pushq   %rbp
   .cfi_def_cfa_offset 16
   .cfi_offset 6, -16
   movq   %rsp, %rbp
   .cfi_def_cfa_register 6
   subq   $32, %rsp
   movl   %edi, -20(%rbp)
   movl   %esi, -24(%rbp)
   cmpl   $0, -24(%rbp)
   jne   .L2
   movl   $.LC0, %edi
   movl   $0, %eax
   call   printf
   movl   $1, %edi
   call   exit
.L2:
   movl   $1, -8(%rbp)
   movl   -20(%rbp), %eax
   imull   -24(%rbp), %eax
   testl   %eax, %eax
   jns   .L3
   movl   $-1, -8(%rbp)
.L3:
   movl   -20(%rbp), %eax
   sarl   $31, %eax
   xorl   %eax, -20(%rbp)
   subl   %eax, -20(%rbp)
   movl   -24(%rbp), %eax
   sarl   $31, %eax
   xorl   %eax, -24(%rbp)
   subl   %eax, -24(%rbp)
   movl   $0, -4(%rbp)
   jmp   .L4
.L5:
   movl   -24(%rbp), %eax
   subl   %eax, -20(%rbp)
   addl   $1, -4(%rbp)
.L4:
   movl   -20(%rbp), %eax
   cmpl   -24(%rbp), %eax
   jge   .L5
   movl   -20(%rbp), %eax
   movl   %eax, %esi
   movl   $.LC1, %edi
   movl   $0, %eax
   call   printf
   movl   -8(%rbp), %eax
   imull   -4(%rbp), %eax
   leave
   .cfi_def_cfa 7, 8
   ret
   .cfi_endproc
.LFE2:
   .size   divide, .-divide
   .section   .rodata
.LC2:
   .string   "Quotient is %d\n"
   .text
   .globl   main
   .type   main, @function
main:
.LFB3:
   .cfi_startproc
   pushq   %rbp
   .cfi_def_cfa_offset 16
   .cfi_offset 6, -16
   movq   %rsp, %rbp
   .cfi_def_cfa_register 6
   subq   $16, %rsp
   movl   $22, -8(%rbp)
   movl   $-7, -4(%rbp)
   movl   -4(%rbp), %edx
   movl   -8(%rbp), %eax
   movl   %edx, %esi
   movl   %eax, %edi
   call   divide
   movl   %eax, %esi
   movl   $.LC2, %edi
   movl   $0, %eax
   call   printf
   movl   $0, %eax
   leave
   .cfi_def_cfa 7, 8
   ret
   .cfi_endproc
.LFE3:
   .size   main, .-main
   .ident   "GCC: (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609"
   .section   .note.GNU-stack,"",@progbits

I hope your problem was solved and you like my answer, for any further questions kindly leave a comment below. Have a great day!


Related Solutions

Write Java code that accepts the integer input (from keyboard) in an arraylist named num1 and...
Write Java code that accepts the integer input (from keyboard) in an arraylist named num1 and stores the even integers of num1 in another arraylist named evennum.
in c++ QUESTION 4: Write the code to do the following: -read input from the keyboard...
in c++ QUESTION 4: Write the code to do the following: -read input from the keyboard by displaying the message on the screen to ask and read the following information: * customer ID (string) * customer name (string)                                                        * balance (float) -open output file customer .txt to write -Write to the output file customer.txt the following information:                 Customer ID – customer name – balance For example: 1561175753 - James Smith – 1255.25 -close file QUESTION 5: -create one notepad...
IN C++ PLEASE Requirements Write a program that takes in user input of two integer numbers...
IN C++ PLEASE Requirements Write a program that takes in user input of two integer numbers for height and width and uses a nested for loop to make a rectangle out of asterixes. The creation of the rectangle (i.e. the nested for loop) should occur in a void function that takes in 2 parameters, one for height and one for width. Make sure your couts match the sample output (copy and paste from those couts so you don't make a...
Write a driver to get a String input from keyboard and if the input string has...
Write a driver to get a String input from keyboard and if the input string has less than 10 characters, throw a StringTooShortException. public class StringTooShortException extends Exception {     //-----------------------------------------------------------------     // Sets up the exception object with a particular message.     //-----------------------------------------------------------------     public StringTooShortException()     {         super("String does not have enough characters");     } }
Write a MIPS assembly language to transpose a square integer matrix in code
Write a MIPS assembly language to transpose a square integer matrix in code
Design a program that will ask the user to input two integer numbers and then perform...
Design a program that will ask the user to input two integer numbers and then perform the basic arithmetic operations such as addition and subtraction. Each calculation is done by a separate function. The main function gets the input from the user, then calls the addition function and the subtraction function one at a time to perform the calculations. Each calculation function (addition or subtraction function) performs an arithmetic operation and then returns the calculation results back to where it...
Write a program to find the prime numbers IN JAVA Ask user to input the integer...
Write a program to find the prime numbers IN JAVA Ask user to input the integer number test the number whether it is a prime number or not Then, print “true” or “false” depending on whether the number is prime or isn’t. Hint: number is prime when is has exactly 2 factors: one and itself. By this definition, number 1 is a special case and is NOT a prime. Use idea of user input, cumulative sum, and loop to solve...
Write a mips assembly code program that ask the user to enter an integer value, and...
Write a mips assembly code program that ask the user to enter an integer value, and then print the result of doubling that number.
Write a program that prompts the user to input their first name from the keyboard and...
Write a program that prompts the user to input their first name from the keyboard and stores them in the variable "firstName". It does the same for last name and stores it in the variable "lastName". It then uses strcat to merge the two names, separates them by a space and stores the full name into a string variable called "fullName". In the end, the program must print out the string stored within fullName. ANSWER IN C LANGUAGE ! You...
write a python program that inputs 10 integer values from the keyboard and then displays their...
write a python program that inputs 10 integer values from the keyboard and then displays their sum. use for loop
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT