Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello i am writing a x86 assembly code where i have to do a multiply function wi

ID: 3630615 • Letter: H

Question

Hello i am writing a x86 assembly code where i have to do a multiply function without using any multiply or divide instructions. Only shl I have all my cases working except for negative times negative.

This is my code so far

.intel_syntax noprefix
.data

.globl _x
_x: .long 0   

.global _y
_y:

.text

.globl _fac
_fac:
push ebp   
mov ebp,esp

mov eax,0  
mov edx,1
mov     ebx,_x


mov ecx,_y

cmp edx,ecx
jl LOOP
xchg ecx,ebx


      
LOOP:

cmp edx,ecx
jg DONE
add eax,ebx
SUB ecx,1

jmp LOOP

DONE:



pop ebp #you need this too
ret  #return

It works with the following main.c file

#include <stdio.h>

extern int x, y;
extern int fac();


main()
{
x = -7;
y = -4;
printf("%d * %d = %d ",x,y,fac());


}



Explanation / Answer

Here's a hackish solution for you. Basically if both numbers are negative, then negate each of the numbers and jump back into the code you have. .intel_syntax noprefix .data .globl _x _x: .long 0 .global _y _y: .text .globl _fac _fac: push ebp mov ebp, esp mov eax, 0 mov edx, 1 mov ebx, _x mov ecx, _y cmp ebx, 0 jl NEG NONEG: cmp edx, ecx jl LOOP xchg ecx, ebx LOOP: cmp edx, ecx jg DONE add eax, ebx SUB ecx, 1 jmp LOOP NEG: cmp ecx, 0 jg NONEG neg ebx # If both numbers are negative, negate both neg ecx # and then just use the existing code. jmp NONEG DONE: pop ebp #you need this too ret #return Here's the driver I used to test it. #include extern int x, y; extern int fac(); main() { x = 7; y = 4; printf("%d * %d = %d ",x,y,fac()); x = 7; y = -4; printf("%d * %d = %d ",x,y,fac()); x = -7; y = -4; printf("%d * %d = %d ",x,y,fac()); }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote