Author Topic: *test & x* Bug in JWasm and AsmC  (Read 3626 times)

jj2007

  • Member
  • *****
  • Posts: 7740
  • Assembler is fun ;-)
    • MasmBasic
*test & x* Bug in JWasm and AsmC
« on: December 31, 2015, 03:45:44 AM »
Code: [Select]
include \masm32\include\masm32rt.inc

.code
start:
  push 2

  test byte ptr [esp], 2
  .if !Zero?
print "& 2 set", 13, 10
  .else
print "& 2 not set", 13, 10
  .endif
  test byte ptr [esp], 2
  .if Zero?
print "& 2 not set", 13, 10, 10
  .else
print "& 2 set", 13, 10, 10
  .endif

  .if byte ptr [esp] & 2
print "& 2 set", 13, 10
  .else
print "& 2 not set", 13, 10
  .endif
  .if !byte ptr [esp] & 2
print "& 2 not set", 13, 10
  .else
print "& 2 set", 13, 10
  .endif
  .if !(byte ptr [esp] & 2)
print "& 2 not set", 13, 10, 10
  .else
print "& 2 set", 13, 10, 10
  .endif

  pop ebx

  .if bl & 2
print "& 2 set", 13, 10
  .else
print "& 2 not set", 13, 10
  .endif
  .if !bl & 2
print "& 2 not set", 13, 10
  .else
print "& 2 set", 13, 10
  .endif
  .if !(bl & 2)
print "& 2 not set", 13, 10
  .else
print "& 2 set", 13, 10
  .endif
 
  exit

end start

Output:
JWasm + AsmC:
8*set

ML 6.14 ... 10.0:
2*set/set, not set, set/set, not set, set

HSE

  • Member
  • ****
  • Posts: 552
  • <AMD>< 7-32>
Re: *test & x* Bug in JWasm and AsmC
« Reply #1 on: December 31, 2015, 12:48:27 PM »
Hi jj!

You are right. Perhaps Habran know the problem because he make some flags additions (there is a note in the JWAsm v2.13 source code).

I don't have used ZERO? in any project, and I suspect, Japhet either.

Regards. HSE

jj2007

  • Member
  • *****
  • Posts: 7740
  • Assembler is fun ;-)
    • MasmBasic
Re: *test & x* Bug in JWasm and AsmC
« Reply #2 on: December 31, 2015, 12:53:17 PM »
I don't have used ZERO? in any project, and I suspect, Japhet either.

Zero? works fine, it is the bitwise testing in .if !something & 4 that is broken; and only in the negated form without brackets. Fortunately, I had used that only once in 20k lines of code 8)

ToutEnMasm

  • Member
  • *****
  • Posts: 1189
    • EditMasm
Re: *test & x* Bug in JWasm and AsmC
« Reply #3 on: December 31, 2015, 07:30:32 PM »
I don't get the same result
Intel(R) Core(TM) i3-4150 CPU @ 3.50GHz
Microsoft Windows 10 Famille Version: 10.0.10586
JWasm v2.13, Sep 17 2015  (Compiled by myself VS2015 Windows 10 64 bits version)

Quote
& 2 set
& 2 set

& 2 set
& 2 set
& 2 set

& 2 set
& 2 set
& 2 set

Code: [Select]
push 2

  test byte ptr [esp], 2
  .if !Zero?
invoke printf_s,TXT("& 2 set", 13, 10)
  .else
invoke printf_s,TXT("& 2 not set", 13, 10)
  .endif
  test byte ptr [esp], 2
  .if Zero?
invoke printf_s,TXT("& 2 not set", 13, 10, 10)
  .else
invoke printf_s,TXT("& 2 set", 13, 10, 10)
  .endif

  .if byte ptr [esp] & 2
invoke printf_s,TXT("& 2 set", 13, 10)
  .else
invoke printf_s,TXT("& 2 not set", 13, 10)
  .endif
  .if !byte ptr [esp] & 2
invoke printf_s,TXT("& 2 not set", 13, 10)
  .else
invoke printf_s,TXT("& 2 set", 13, 10)
  .endif
  .if !(byte ptr [esp] & 2)
invoke printf_s,TXT("& 2 not set", 13, 10, 10)
  .else
invoke printf_s,TXT("& 2 set", 13, 10, 10)
  .endif

  pop ebx

  .if bl & 2
invoke printf_s,TXT("& 2 set", 13, 10)
  .else
invoke printf_s,TXT("& 2 not set", 13, 10)
  .endif
  .if !bl & 2
invoke printf_s,TXT("& 2 not set", 13, 10)
  .else
invoke printf_s,TXT("& 2 set", 13, 10)
  .endif
  .if !(bl & 2)
invoke printf_s,TXT("& 2 not set", 13, 10)
  .else
invoke printf_s,TXT("& 2 set", 13, 10)
  .endif
« Last Edit: January 01, 2016, 12:48:31 AM by ToutEnMasm »
Fa is a musical note to play with CL

jj2007

  • Member
  • *****
  • Posts: 7740
  • Assembler is fun ;-)
    • MasmBasic
Re: *test & x* Bug in JWasm and AsmC
« Reply #4 on: December 31, 2015, 10:11:23 PM »
I don't get the same result

If you really tried with different assemblers, why did you post only one executable? It should be one for ML and one for JWasm or AsmC.

Btw your code doesn't assemble, fatal error A1000: cannot open file : sdk32.inc
And of course, your exe doesn't run, because it requires an exotic DLL that is not present on my Win7-64 installation.


TWell

  • Member
  • ****
  • Posts: 748
Re: *test & x* Bug in JWasm and AsmC
« Reply #5 on: December 31, 2015, 11:10:43 PM »
Code: [Select]
.386
.model flat,c
option casemap :none

TXT MACRO your_text:VARARG
    LOCAL text_string
    .data
     text_string db your_text,0
    .code
    EXITM <addr text_string>
ENDM

printf proto c args:vararg
_getch proto c
exit proto c :dword
includelib msvcrt.lib

.code

main PROC C argc:DWORD,pargv:DWORD
;---- code here --------
push 2

  test byte ptr [esp], 2
  .if !Zero?
invoke printf,TXT("& 2 set", 13, 10)
  .else
invoke printf,TXT("& 2 not set", 13, 10)
  .endif
  test byte ptr [esp], 2
  .if Zero?
invoke printf,TXT("& 2 not set", 13, 10, 10)
  .else
invoke printf,TXT("& 2 set", 13, 10, 10)
  .endif

  .if byte ptr [esp] & 2
invoke printf,TXT("& 2 set", 13, 10)
  .else
invoke printf,TXT("& 2 not set", 13, 10)
  .endif
  .if !byte ptr [esp] & 2
invoke printf,TXT("& 2 not set", 13, 10)
  .else
invoke printf,TXT("& 2 set", 13, 10)
  .endif
  .if !(byte ptr [esp] & 2)
invoke printf,TXT("& 2 not set", 13, 10, 10)
  .else
invoke printf,TXT("& 2 set", 13, 10, 10)
  .endif

  pop ebx

  .if bl & 2
invoke printf,TXT("& 2 set", 13, 10)
  .else
invoke printf,TXT("& 2 not set", 13, 10)
  .endif
  .if !bl & 2
invoke printf,TXT("& 2 not set", 13, 10)
  .else
invoke printf,TXT("& 2 set", 13, 10)
  .endif
  .if !(bl & 2)
invoke printf,TXT("& 2 not set", 13, 10)
  .else
invoke printf,TXT("& 2 set", 13, 10)
  .endif
 
invoke _getch
invoke exit,0
main endp
end main
Code: [Select]
& 2 set
& 2 set

& 2 set
& 2 set
& 2 set

& 2 set
& 2 set
& 2 set

jj2007

  • Member
  • *****
  • Posts: 7740
  • Assembler is fun ;-)
    • MasmBasic
Re: *test & x* Bug in JWasm and AsmC
« Reply #6 on: December 31, 2015, 11:47:27 PM »
Tim,

You don't specify whether your exe was build with ML or with JWasm/AsmC. The whole point of this thread is that different assemblers produce different code.

Build it again with ML to see the difference.

TWell

  • Member
  • ****
  • Posts: 748
Re: *test & x* Bug in JWasm and AsmC
« Reply #7 on: January 01, 2016, 12:22:59 AM »
Code: [Select]
Microsoft (R) Macro Assembler Version 12.00.21005.1
& 2 set
& 2 set

& 2 set
& 2 not set
& 2 set

& 2 set
& 2 not set
& 2 set

Microsoft (R) Macro Assembler Version 14.00.23419.0
& 2 set
& 2 set

& 2 set
& 2 not set
& 2 set

& 2 set
& 2 not set
& 2 set

JWasm v2.12pre, Nov 27 2013, Masm-compatible assembler.
& 2 set
& 2 set

& 2 set
& 2 set
& 2 set

& 2 set
& 2 set
& 2 set

Pelles Macro Assembler, Version 8.00.1
& 2 set
& 2 set

& 2 set
& 2 set
& 2 set

& 2 set
& 2 set
& 2 set
« Last Edit: January 01, 2016, 08:02:32 PM by TWell »

HSE

  • Member
  • ****
  • Posts: 552
  • <AMD>< 7-32>
Re: *test & x* Bug in JWasm and AsmC
« Reply #8 on: January 01, 2016, 02:42:27 AM »
There is no problem with ZERO? or !ZERO?

The difference is that ML ignore "!" when you are using "&" without parenthesis

JWAsm, AsmC and POAsm are correct.

Notes (JJ code):
Code: [Select]
include \masm32\include\masm32rt.inc

.code
start:
  push 2
                                            ;ML     JWASM
  test byte ptr [esp], 2                    ;   test byte ptr ss:[esp],2    BOTH
  .if !ZERO?                                ;je     je
print "& 2 set", 13, 10
  .else
print "& 2 not set", 13, 10
  .endif
  test byte ptr [esp], 2                   
  .if ZERO?                                 ;jne    jne
print "& 2 not set", 13, 10, 10
  .else
print "& 2 set", 13, 10, 10
  .endif
                                            ;   test byte ptr ss:[esp],2    BOTH
  .if byte ptr [esp] & 2                    ; je      je
print "& 2 set", 13, 10
  .else
print "& 2 not set", 13, 10
  .endif
  .if !byte ptr [esp] & 2                   ; je      jne
print "& 2 not set", 13, 10
  .else
print "& 2 set", 13, 10
  .endif
  .if !(byte ptr [esp] & 2)                 ; jne      jne
print "& 2 not set", 13, 10, 10
  .else
print "& 2 set", 13, 10, 10
  .endif

  pop ebx
                                            ;   test bl,2   BOTH
  .if bl & 2
print "& 2 set", 13, 10              ; je      je
  .else
print "& 2 not set", 13, 10
  .endif
  .if !bl & 2                               ; je      jne
print "& 2 not set", 13, 10
  .else
print "& 2 set", 13, 10
  .endif
  .if !(bl & 2)                             ; jne   jne       
print "& 2 not set", 13, 10
  .else
print "& 2 set", 13, 10
  .endif
 
  exit

end start


I'm using the X32dbg (from X64dbg package), apparently very related to OllyDbg

jj2007

  • Member
  • *****
  • Posts: 7740
  • Assembler is fun ;-)
    • MasmBasic
Re: *test & x* Bug in JWasm and AsmC
« Reply #9 on: January 01, 2016, 03:01:04 AM »
There is no problem with ZERO? or !ZERO?

Indeed.

Quote
The difference is that ML ignore "!" when you are using "&" without parenthesis

JWAsm, AsmC and POAsm are correct

This is the interesting question:

.if !somevar & 2

Let's use eax as somevar:
  mov eax, 2
  not eax   ; not is the meaning of the exclamation mark; eax is now -3
  and eax, 2

So what is -3 and 2?

ZERO.

HSE

  • Member
  • ****
  • Posts: 552
  • <AMD>< 7-32>
Re: *test & x* Bug in JWasm and AsmC
« Reply #10 on: January 01, 2016, 03:57:26 AM »
Your logic sounds fantastic!  :biggrin:

But the expression isn't a logical equation, is an instruction for "if .. endif".
                                                                                      (control-flow directive is the name)
"&" means ","
"!" means "jump if not equal"
« Last Edit: January 01, 2016, 06:51:46 AM by HSE »

jj2007

  • Member
  • *****
  • Posts: 7740
  • Assembler is fun ;-)
    • MasmBasic
Re: *test & x* Bug in JWasm and AsmC
« Reply #11 on: May 06, 2016, 07:43:39 AM »
Attached source causes an exception in AsmC. Sorry, it's a confused source, and needs the attached beta, but attempts to isolate the problem were not successful :(

Endsw_ MACRO tmodeX:=<auto>
LOCAL is, tmp$, cases, curCase, ctCase, ctImm, c$, swa$, cL$, cR$, lbl$, tmode, tmodeCt, MbSwMin, MbSwMax
  retn
  @CatStr(<swa$ equ !<swArg$!>>, %MbSct)      ; causes exception with AsmC
;   @CatStr(<swa$ equ swArg$>, %MbSct)
  @CatStr(<swEnd>, %MbSct, <:>)


Code: [Select]
CPU Disasm
Address              Hex dump               Command                            Comments
0043091B             À.  C2 0800            retn 8
0043091E                 00                 db 00
0043091F                 00                 db 00
00430920             Ú$  57                 push edi                           ; asmc.00430920(guessed Arg1,Arg2)
00430921             ³.  52                 push edx
00430922             ³.  8B4C24 10          mov ecx, [arg2]
00430926             ³.  8B7C24 0C          mov edi, [arg1]
0043092A             ³. EB 05              jmp short 00430931
0043092C             ³>  8907               Úmov [edi], eax
0043092E             ³.  83C7 04            ³add edi, 4
00430931             ³>  8B01               +mov eax, [ecx]  <<<<<<<<<<< exception: ecx is zero #########
00430933             ³.  83C1 04            ³add ecx, 4
00430936             ³.  8D90 FFFEFEFE      ³lea edx, [eax+FEFEFEFF]
0043093C             ³.  F7D0               ³not eax
0043093E             ³.  23D0               ³and edx, eax
00430940             ³.  F7D0               ³not eax
00430942             ³.  81E2 80808080      ³and edx, 80808080
00430948             ³. 74 E2              Àjz short 0043092C
0043094A             ³.  8807               mov [edi], al
0043094C             ³.  84C0               test al, al
0043094E             ³. 74 14              jz short 00430964
00430950             ³.  8867 01            mov [edi+1], ah
00430953             ³.  84E4               test ah, ah
00430955             ³. 74 0D              jz short 00430964
00430957             ³.  C1E8 10            shr eax, 10
0043095A             ³.  8847 02            mov [edi+2], al
0043095D             ³.  84C0               test al, al
0043095F             ³. 74 03              jz short 00430964
00430961             ³.  8867 03            mov [edi+3], ah
00430964             ³>  5A                 pop edx
00430965             ³.  5F                 pop edi
00430966             ³.  8B4424 04          mov eax, [arg1]
0043096A             À.  C2 0800            retn 8

nidud

  • Member
  • *****
  • Posts: 1408
    • https://github.com/nidud/asmc
Re: *test & x* Bug in JWasm and AsmC
« Reply #12 on: May 06, 2016, 08:12:44 AM »
Code: [Select]
.elseif [eax].asym.state == SYM_MACRO

.if [eax].asym.mac_flag & SMAC_ISFUNC
;
; macro function calls with <>-literals as arguments of hll directives
; this trigger a special tokenize-mode that handles < and > differently
; EXPANS39.asm: .if @SubStr(<ebx==1 && edi==2, xxx>, 1, 16)
;
lea ecx,[edi+1]
.while BYTE PTR [ecx] == ' '
add ecx,1
.endw
.if BYTE PTR [ecx] == '<'
;
; this fails the C-parsing
;
mov ModuleInfo.asmc_syntax,0
push eax
asmerr( 7007 )
pop eax
and [esi].flags2,not DF_CEXPR

Well, this may not be an issue any more but I have to do some testing. If you apply the -W3 switch you will see where the C-parsing ended. The logic have changed a bit so this may work now.

jj2007

  • Member
  • *****
  • Posts: 7740
  • Assembler is fun ;-)
    • MasmBasic
Re: *test & x* Bug in JWasm and AsmC
« Reply #13 on: May 06, 2016, 09:31:44 AM »
Actually, this was buggy code, too, the final Switch_ macro is now included here. But I wanted to flag it anyway, because one doesn't expect an access violation from an assembler.

wrong:
@CatStr(<swa$ equ !<swArg$!>>, %MbSct)

right:
@CatStr(<swa$ equ !<swArg$>, %MbSct, !>)

nidud

  • Member
  • *****
  • Posts: 1408
    • https://github.com/nidud/asmc
Re: *test & x* Bug in JWasm and AsmC
« Reply #14 on: May 06, 2016, 09:48:32 AM »
The exception comes from a missing macro argument which ends up in strcpy().
Code: [Select]
     3 cmp ??025E, ??025D
K:\test\switch\Tmp_File.asm(240) : error A2006:undefined symbol : ??025E
 MacroLoop(13): teration 1: Macro Called From
  MacroLoop(61): teration 1: Macro Called From
   Endsw_(86): Macro Called From
    K:\test\switch\Tmp_File.asm(240): Main Line Code

The exception handler is currently off in ASMC, so you end up in the debugger instead.