News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Problem With Logical Operators

Started by allen, March 26, 2014, 12:23:01 AM

Previous topic - Next topic

allen

Hello Im Trying To Understand Logical Operator in this code

if(a >= 10 || b <= 9 && d >22 && e <=5)
w = ((x*e)/4)
else
w++;


and converter into masm32 code


.386
.model flat, Stdcall
option casemap :none   
.stack 100h

  include c:\masm32\include\windows.inc
include c:\masm32\macros\macros.asm
      include c:\masm32\include\msvcrt.inc

include c:\masm32\include\masm32.inc
include c:\masm32\include\gdi32.inc
include c:\masm32\include\user32.inc
include c:\masm32\include\kernel32.inc

includelib \masm32\lib\msvcrt.lib
includelib \masm32\lib\masm32.lib
includelib \masm32\lib\gdi32.lib
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib
.data?
a sdword?
b sdword?
c sdword?
e sdword?
quot sdword?
prod sdword ?

.code

start:

main proc

mov a, sval(input("                        Enter value for a: "))
mov b, sval(input("                        Enter value for b: "))
mov d, sval(input("                        Enter value for d: "))
mov e, sval(input("                        Enter value for e: "))

cmp a,10
jge else01

cmp b,9
jle true

cmp d,20
jle true

true:
mov qnum,4
mov eax,x
mov ebx,e
imul ebx
mov prod,eax
cdq
idiv qnum
mov quot,eqx

print chr$ ("                           The value of w is= "),0
print str$ (quot),0Ah,0

else01
inc w
print chr$ ("                           The value of w is= "),0
print str$ (w),0Ah,0
ret
main endp

end start

qWord

For OR-combined conditions the test continues until one of them is true - if all are false the else-clause applies . AND-combined conditions requires that all of them are true. Also remarks the that && has an higher precedence as ||.
    ;if(a >= 10 || b <= 9 && d >22 && e <=5)
    ; w = ((x*e)/4)
    ;else
    ; w++;
   
    ; MASM
    .if a >= 10 || b <= 9 && d > 22 && e <= 5
        mov eax,x
        imul eax,e
        sar eax,2 ; eax*2^(-2) = eax/4
        mov w,eax
    .else
        inc w
    .endif
   
    ; handcrafted
    cmp a,10
    jge @If_True
    cmp b,9
    jg @Else
    cmp d,22
    jle @Else
    cmp e,5
    jg @Else
@If_True:
        mov eax,x
        imul eax,e
        sar eax,2
        mov w,eax
        jmp @EndIf
@Else:
        inc w
@EndIf:

MREAL macros - when you need floating point arithmetic while assembling!