Assembly Language MASM32 Can someone help me by telling me what I\'m doing wrong
ID: 3669279 • Letter: A
Question
Assembly Language MASM32
Can someone help me by telling me what I'm doing wrong with this code, it complies but doesn't give me the right answer. It's in MASM 32bit using Irvine32; assembly language. The question instructions for this problem is located right under this sentence.
TITLE:
; Write a procedure named BitWiseMultiply that multiplies any unsigned 32-bit
;integer by EAX, using only shifting and addition. Pass the integer to the procedure in the EBX ;register, and return the product in the EAX register. Write a short test program that calls the ;procedure and displays the product.
INCLUDE Irvine32.inc
.data
str1 BYTE "Results", 0dh,0ah,0dh,0ah,0
Multi DWORD 200
MultiBy DWORD 5
Result DWORD 32 DUP (?)
ResultSize DWORD ?
.code
main PROC
call Clrscr
mov ebx, Multi
mov eax, MultiBy
call BitwiseMultiply
call Display
exit
main ENDP
BitwiseMultiply PROC uses ecx edx esi
mov ecx, 32
mov edx, eax
mov esi, 0
L1:
clc
L2:
inc esi
add eax, Result[esi]
Loop L3
ret
L3:
jc L4
shl edx,1
jmp L2
L4:
mov Result[esi], ebx
inc esi
loop L1
mov ResultSize, esi
mov eax, Result[esi]
mov ecx, ResultSize-1
BitwiseMultiply ENDP
Display PROC
mov edx, OFFSET str1
call WriteString
call WriteDec
call Crlf
call Crlf
ret
Display ENDP
END main
Explanation / Answer
INCLUDE Irvine32.inc
INCLUDE Macros.inc
.data
str1 BYTE "Fast Multiplication Program - ** DEMO **",
0dh,0ah,0dh,0ah,0
.code
main PROC
call Clrscr
mov edx,OFFSET str1
call WriteString
L1:
mWrite "Enter an unsigned integer (0 to quit): "
call ReadDec
.IF CARRY?
mWrite <"Error: Integer is out of range",0dh,0ah>
jmp L1
.ELSE
mov ebx,eax
.ENDIF
cmp eax,0
je quit
L2:
mWrite "Enter an unsigned integer: "
call ReadDec
.IF CARRY?
mWrite <"Error: Integer is out of range",0dh,0ah>
jmp L2
.ENDIF
mWrite <"--------------------------------",0dh,0ah>
; EBX = multiplicand, EAX = multiplier
call FastMultiply ; EAX = product
jc L1 ; CF=1 indicates error
mWrite "The product is: "
call WriteDec ; display product
call Crlf
call Crlf
jmp L1
quit:
exit
main ENDP
;-----------------------------------------------------------------
FastMultiply PROC uses ecx edx esi
COMMENT !
Multiplies any unsigned 32-bit integer by EAX, using only
shifting and addition.
Receives: EBX = multiplier, EAX = multiplicand
Returns: If CF=0, EAX = product; otherwise, CF=1 and
EAX equals 0.
The algorithm used here is more elegant than the one discussed
on page 237.
FOR count = 1 to 32
if lowbit(multiplier) == 1
product += multiplicand; ; check Carry flag
multiplier SHR 1;
multiplicand SHL 1; ; check Carry flag
NEXT count
This was obtained from the web site of Dr. Wang Jian-Sheng at
the National University of Singapore (http://www.cz3.nus.edu.sg/~wangjs/)
----------------------------------------------------------------!
mov edx,0 ; clear product to zero
mov ecx,32 ; loop counter
L1: test ebx,1 ; LSB set?
jz L2 ; no: skip next statement
add edx,eax ; yes: add multiplicand to product
jc L3 ; display error if CF=1
L2:
shr ebx,1 ; shift multiplier right
shl eax,1 ; shift multiplicand left
jc L3 ; display error if CF=1
loop L1 ; repeat for 32 bits
L3: mov eax,edx ; return the product
ret
FastMultiply ENDP
END main
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.