-3

I am facing issue while apply BODMAS in assembly language. The result of (4 * 2 * 3 - 1 + 10 * 6) according BODMAS rule should be 83 but it shows something else result. Please try to solve this issue. Thanks.

TITLE Add and Subtract, Version 2         (AddSub2.asm)


INCLUDE Irvine32.inc

.data

.code
main PROC

; using BODMAS
; 4*2*3-1+10*6

mov eax,4
mov ebx,2
mov ecx,3
mov edx,1
mov esi,10
mov edi,6


mul ebx
mul ecx

mov ebx,eax
mov eax, esi
mul edi

add edi,edx

mov eax,ebx
sub ebx,edi


call dumpregs
call WriteInt

exit
main ENDP
END main
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • If you don't want to overwrite EDX with the high-half result of a multiply, don't use `mul`. Just use `imul eax, ebx` like a compiler would; it's a normal 2-operand instruction like `add`. Use a debugger to single-step your code; you'll be able to see your first `mul` overwrite EDX with the high-half result (0). – Peter Cordes Jun 23 '20 at 07:16

1 Answers1

-2

TITLE Add and Subtract, Version 2 (AddSub2.asm)

; This program adds and subtracts 32-bit integers ; and stores the sum in a variable.

INCLUDE Irvine32.inc

.data

.code main PROC

; 4*2*3-1+10*6
; Using BODMAS


mov eax, 10
mov ebx, 6
mul ebx

mov ecx, eax

mov eax, 4
mov ebx, 2

mul ebx

mov ebx, 3
mul ebx

add eax,ecx

sub eax, 1

call dumpregs
call WriteInt


call Readchar

exit

main ENDP END main

Jarrar
  • 1