Author Topic: UASM 2.40 release  (Read 686 times)

habran

  • Member
  • *****
  • Posts: 1116
    • uasm
Re: UASM 2.40 release
« Reply #15 on: September 25, 2017, 09:24:27 PM »
Thanks mate :t
Cod-Father

nidud

  • Member
  • *****
  • Posts: 1408
    • https://github.com/nidud/asmc
Re: UASM 2.40 release
« Reply #16 on: September 26, 2017, 04:49:22 AM »
The negative values now works  :t

However the "alternative" code crashes. This happens when case values are not in a sequence?
Code: [Select]
    .case 1
    .case 2
    ...
    .case 100000
    .case 100001

The code generated:
Code: [Select]
?_0002: push    rax
        push    rcx
        push    rbx
        push    rsi
        push    rdi
        mov     esi, eax
        mov     ebx, 2359
        xor     ecx, ecx
        lea     rdi, [@C0006]
*       js      ?_0001
?_0003: lea     eax, [rbx+rcx]
        sar     rax, 1       
        cmp     dword ptr [rdi+rax*4], esi
        jz      ?_0004                   
        jge     ?_0005                   
        lea     ecx, [rax+1H]             
        jmp     ?_0006                   

?_0004: pop     rdi                       
        pop     rsi                       
        pop     rbx                       
        pop     rcx                       
        lea     rcx, [@C0004]             
        mov     rax, qword ptr [rcx+rax*8]
*       pop     rcx
        xchg    qword ptr [rsp], rax     
        ret                               

?_0005: lea     ebx, [rax-1H]
?_0006: cmp     ecx, ebx
        jle     ?_0003
        pop     rdi
        pop     rsi
        pop     rbx
        pop     rcx
        pop     rax
        jmp     ?_0001

This approach will add a lot of code (or data in this case) given you have to store the case value in a table in addition to the labels.

A simpler and faster approach is to split them into chunks and create multiple tables, and simply do a direct compare of the leftovers.
Code: [Select]
?_0001: cmp     ecx, 100000
        jl      ?_0002     
        cmp     ecx, 101999
        jg      ?_0002     
        movsxd  rax, ecx   
        push    rcx       
        lea     rcx, [@C093C]
        mov     rax, qword ptr [rcx+rax*8-0C3500H]
        pop     rcx                               
        jmp     rax                               

@C093C  label qword ; switch/case jump table 1
...
?_0002: cmp     ecx, -399
        jl      @C093B   
        cmp     ecx, 659
        jg      @C093B   
        movsxd  rax, ecx
        push    rcx     
        lea     rcx, [IT@C093E]
        movzx   rax, word ptr [rcx+rax*2+31EH]
        lea     rcx, [@C093E]                 
        mov     rax, qword ptr [rcx+rax*8]   
        pop     rcx                           
        jmp     rax                           

@C093E  label qword ; switch/case jump table 2

nidud

  • Member
  • *****
  • Posts: 1408
    • https://github.com/nidud/asmc
Re: UASM 2.40 release
« Reply #17 on: September 26, 2017, 05:14:57 AM »
Test case:
Code: [Select]

    .x64
    .model  flat, fastcall

    option  dllimport:<msvcrt>
    printf  proto :ptr sbyte, :vararg
    exit    proto :dword

    .data
    error  db "Uasm Error: %d, %d",10,0

    .code

sw_uasm proc val

    .switch ecx
    enum = 0
    repeat 100
%   .case @CatStr(%enum)
    mov eax,enum
    enum = enum + 1
    endm
    enum = 100000
    repeat 100
%   .case @CatStr(%enum)
    mov eax,enum
    enum = enum + 1
    endm
    .endswitch
    ret

sw_uasm endp

main proc

    mov esi,99
    .while esi 
        invoke sw_uasm,esi
        .if eax != esi
            invoke printf,addr error,esi,eax
            .break
        .endif
        dec esi
    .endw
    mov esi,100099
    .while esi >= 100000
        invoke sw_uasm,esi
        .if eax != esi
            invoke printf,addr error,esi,eax
            .break
        .endif
        dec esi
    .endw
    invoke exit,0

main endp

    end main

habran

  • Member
  • *****
  • Posts: 1116
    • uasm
Re: UASM 2.40 release
« Reply #18 on: September 26, 2017, 06:23:24 AM »
Good find Nidud,
however, the problem was not there where you thought and that alternative doesn't build large data and is actually much faster than IF - ELSE
I have used this code:
Code: [Select]
.switch eax
.case 1
.case 2
.case  100000
.case  100001
mov  edx, 100000
.case 6
mov  edx, 6
.case 7
mov  edx, 7
.case 8
mov  edx, 8
.case 9
mov  edx, 9
.case 12
mov  edx, 12
mov  edx, 15
.case 16
mov  edx, 16
.case 17
mov  edx, 17
.case 18
mov  edx, 18
.case 19
mov  edx, 19
.case 24
mov edx, 24
.case 27
mov  edx, 27
.case 21
mov  edx, 21
.default
xor edx, edx
.endswitch
and it creates only this data:
Code: [Select]
                            *   ALIGN 8
                            *   @C0004 dq @C0009, @C000A, @C000D, @C000E, @C000F, @C0010, @C0011, @C0012, @C0013, @C0014, @C0015, @C0018, @C0016, @C0017, @C000B
                            *    dq @C000C
                            *   @C0006 dd 1,2,6,7,8,9,12,16,17,18,19,21,24,27,100000,100001

and it works fine after the small fix in HllStartDir();

I have followed stupid PVS to remove :
Code: [Select]
          if ((t->tokval <= T_EAX))
            ; //skip it, no need to write MOV EAX,EAX

so I returned it back here:
Code: [Select]
#if AMD64_SUPPORT
        else if (ModuleInfo.Ofssize == USE64)            //USE64
        {
          if ((t->tokval <= T_EAX))
            ; //skip it, no need to write MOV EAX,EAX
          else  if (t->tokval <= T_BX) // AL, CL, DL, BL, AH, CH, DH, BH, AX, CX, DX, BX
            AddLineQueueX(" movzx eax, %s", tokenarray[i].tokpos);
          else if (t->tokval <= T_EDI && t->tokval != T_EAX)  //ECX,EDX,EBX,ESP,EBP,ESI,EDI
            AddLineQueueX(" mov eax, %s", tokenarray[i].tokpos);
          else {
            if (t->tokval != T_RAX) //skip it, no need to write MOV RAX,RAX
              AddLineQueueX(" mov rax, %s", tokenarray[i].tokpos);
            hll->csize = 8;
          }
        }               //end USE64
#endif
now I just put that back and it works as it suppose to
I'll just recheck everything and then reupload again
Cod-Father

habran

  • Member
  • *****
  • Posts: 1116
    • uasm
Re: UASM 2.40 release
« Reply #19 on: September 26, 2017, 08:40:40 AM »
there were actually 2 bugs :icon_eek:
the second one was popping out RCX 2 times, here:
Code: [Select]
#if AMD64_SUPPORT
        else if (ModuleInfo.Ofssize == USE64) {
          AddLineQueueX("pop rdi");
          AddLineQueueX("pop rsi");
          AddLineQueueX("pop rbx");
          AddLineQueueX("pop rcx");    this was the bug
          GetLabelStr(hll->labels[LDATA1], buff);       
          AddLineQueueX("lea   rcx,%s", buff);
          AddLineQueueX("mov   rax, qword ptr[rcx+rax*8]");
          AddLineQueueX("pop   rcx");
          AddLineQueueX("xchg rax,[rsp]");
          AddLineQueueX("retn");
        }
#endif

so, thank you again to make me aware of it :t
now your source works flowlesly
and this is data that is created for the cases:
Code: [Select]
                            *   ALIGN 8
                            *   @C0004 dq @C0009, @C000A, @C000B, @C000C, @C000D, @C000E, @C000F, @C0010, @C0011, @C0012, @C0013, @C0014, @C0015, @C0016, @C0017
                            *    dq @C0018, @C0019, @C001A, @C001B, @C001C, @C001D, @C001E, @C001F, @C0020, @C0021, @C0022, @C0023, @C0024, @C0025, @C0026
                            *    dq @C0027, @C0028, @C0029, @C002A, @C002B, @C002C, @C002D, @C002E, @C002F, @C0030, @C0031, @C0032, @C0033, @C0034, @C0035
                            *    dq @C0036, @C0037, @C0038, @C0039, @C003A, @C003B, @C003C, @C003D, @C003E, @C003F, @C0040, @C0041, @C0042, @C0043, @C0044
                            *    dq @C0045, @C0046, @C0047, @C0048, @C0049, @C004A, @C004B, @C004C, @C004D, @C004E, @C004F, @C0050, @C0051, @C0052, @C0053
                            *    dq @C0054, @C0055, @C0056, @C0057, @C0058, @C0059, @C005A, @C005B, @C005C, @C005D, @C005E, @C005F, @C0060, @C0061, @C0062
                            *    dq @C0063, @C0064, @C0065, @C0066, @C0067, @C0068, @C0069, @C006A, @C006B, @C006C, @C006D, @C006E, @C006F, @C0070, @C0071
                            *    dq @C0072, @C0073, @C0074, @C0075, @C0076, @C0077, @C0078, @C0079, @C007A, @C007B, @C007C, @C007D, @C007E, @C007F, @C0080
                            *    dq @C0081, @C0082, @C0083, @C0084, @C0085, @C0086, @C0087, @C0088, @C0089, @C008A, @C008B, @C008C, @C008D, @C008E, @C008F
                            *    dq @C0090, @C0091, @C0092, @C0093, @C0094, @C0095, @C0096, @C0097, @C0098, @C0099, @C009A, @C009B, @C009C, @C009D, @C009E
                            *    dq @C009F, @C00A0, @C00A1, @C00A2, @C00A3, @C00A4, @C00A5, @C00A6, @C00A7, @C00A8, @C00A9, @C00AA, @C00AB, @C00AC, @C00AD
                            *    dq @C00AE, @C00AF, @C00B0, @C00B1, @C00B2, @C00B3, @C00B4, @C00B5, @C00B6, @C00B7, @C00B8, @C00B9, @C00BA, @C00BB, @C00BC
                            *    dq @C00BD, @C00BE, @C00BF, @C00C0, @C00C1, @C00C2, @C00C3, @C00C4, @C00C5, @C00C6, @C00C7, @C00C8, @C00C9, @C00CA, @C00CB
                            *    dq @C00CC, @C00CD, @C00CE, @C00CF, @C00D0
                            *   @C0006 dd 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29
                            *    dd 30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59
                            *    dd 60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89
                            *    dd 90,91,92,93,94,95,96,97,98,99,100000,100001,100002,100003,100004,100005,100006,100007,100008,100009,100010,100011,100012,100013,100014,100015,100016,100017,100018,100019
                            *    dd 100020,100021,100022,100023,100024,100025,100026,100027,100028,100029,100030,100031,100032,100033,100034,100035,100036,100037,100038,100039,100040,100041,100042,100043,100044,100045,100046,100047,100048,100049
                            *    dd 100050,100051,100052,100053,100054,100055,100056,100057,100058,100059,100060,100061,100062,100063,100064,100065,100066,100067,100068,100069,100070,100071,100072,100073,100074,100075,100076,100077,100078,100079
                            *    dd 100080,100081,100082,100083,100084,100085,100086,100087,100088,100089,100090,100091,100092,100093,100094,100095,100096,100097,100098,100099

Cod-Father

jj2007

  • Member
  • *****
  • Posts: 7738
  • Assembler is fun ;-)
    • MasmBasic
Re: UASM 2.40 release
« Reply #20 on: September 27, 2017, 01:43:12 AM »
Code: [Select]
include \masm32\include\masm32rt.inc ; plain Masm32 for the fans of pure assembler

.code
start: MsgBox 0, "Hello World", "Hi UAsm team:"
exit
end start

Code: [Select]
Tmp_File.asm(7) : Error A2244: Invalid symbol type in expression: exit
Weird? This works fine (and you will realise why it doesn't like exit):
Code: [Select]
start: MsgBox 0, "Hello World", "Hello..."
MB_YESNOCANCEL or MB_ICONQUESTION
exit