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...
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
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
Write a C++ program that accepts a positive integer number from the keyboard . The purpose...
Write a C++ program that accepts a positive integer number from the keyboard . The purpose of the program is to find and the display all the square pair numbers between 1 and that number. The user should be able to repeat the process until he/she enters n or N in order to terminate the process and the program. Square numbers are certain pairs of numbers when added together gives a square number and when subtracted also gives a square...
In Coral Code Please!!!! The assignment is to get an integer from input, and output that...
In Coral Code Please!!!! The assignment is to get an integer from input, and output that integer squared, ending with newline. (Note: This assignment is configured to have students programming directly in the zyBook. Instructors may instead require students to upload a file). Below is a program that's been nearly completed for you. Click "Run program". The output is wrong. Sometimes a program lacking input will produce wrong output (as in this case), or no output. Remember to always pre-enter...
Write code in C please. #1 Write a function multiples() which will take an integer input...
Write code in C please. #1 Write a function multiples() which will take an integer input and it will print out all the multiples of this number starting from 2 but not including itself. For example, multiples(10) will print 2, 5 and multiples(100) will print 2, 4, 5, 10, 20, 25, 50 #2 Write and test a Fibonacci() function that uses a loop instead of recursion to calculate Fibonacci numbers.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT