In: Computer Science
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
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!