How to change asm code to work with new c back end

Bare metal programming in PureBasic, for experienced users
novablue
Enthusiast
Enthusiast
Posts: 165
Joined: Sun Nov 27, 2016 6:38 am

How to change asm code to work with new c back end

Post by novablue »

Hello i am a noob with asm i just want to know how to make this code work with the new c back end compiler.
User avatar
idle
Always Here
Always Here
Posts: 5042
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: How to change asm code to work with new c back end

Post by idle »

I'm away from computer but read the examples here https://www.purebasic.fr/english/viewtopic.php?t=78308
juergenkulow
Enthusiast
Enthusiast
Posts: 544
Joined: Wed Sep 25, 2019 10:18 am

Re: How to change asm code to work with new c back end

Post by juergenkulow »

Please rewrite the assembler line ROL C,N in PureBasic or C and put the line in Procedure.c RolChar and look at the EXE result generated with Optimizer in the debugger x64dbg. Little help: Intel manual page 1173
Please ask your questions, because switch on the cognition apparatus decides on the only known life in the universe.Wersten :DDüsseldorf NRW Germany Europe Earth Solar System Flake Bubble Orionarm
Milky Way Local_Group Virgo Supercluster Laniakea Universe
novablue
Enthusiast
Enthusiast
Posts: 165
Joined: Sun Nov 27, 2016 6:38 am

Re: How to change asm code to work with new c back end

Post by novablue »

I am sorry i don't know anything about assembler, could someone with enough knowledge rewrite this code so it works with the c back end.
User avatar
idle
Always Here
Always Here
Posts: 5042
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: How to change asm code to work with new c back end

Post by idle »

here only makes sense for unicode though

Code: Select all

Macro AsmInput(var) 
  !:[var] "r" (v_#var) 
EndMacro  

Macro AsmInput2(var0,var1) 
  !:[var0] "r" (v_#var0),[var1] "r" (v_#var1) 
EndMacro  

Macro AsmInput3(var0,var1,var2) 
  !:[var0] "r" (v_#var0),[var1] "r" (v_#var1),[var2] "r" (v_#var2)  
EndMacro

Macro AsmOutput(var)
  !".att_syntax;"
  !:[var] "=r" (v_#var)
EndMacro  
Macro BeginAsm() 
  !__asm__(
  !".intel_syntax noprefix;"
EndMacro

Macro BeginAsmGoto() 
  !asm goto(
  !".intel_syntax noprefix;"
EndMacro 
Macro EndAsm() 
  !);
EndMacro   
CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
   Macro rax : eax : EndMacro
CompilerEndIf  
  
Procedure.c RolChar(c.c, n)
  Protected ret.c = 0  
  CompilerIf #PB_Compiler_Backend = #PB_Backend_C 
    BeginAsm()
    !"mov rcx,%[n];"
    CompilerIf #PB_Compiler_Unicode
      !"mov ax, %[c];"
      !"rol ax, cl;" 
      !"mov %[ret], ax;"
    CompilerElse
      !"mov al,%[c];"
      !"rol al, cl;"
      !"mov %[ret], al;"
    CompilerEndIf
    AsmOutput(ret) 
    AsmInput2(c,n)
    EndAsm()
  CompilerElse 
    !movzx rcx, byte [p.v_n]
    CompilerIf #PB_Compiler_Unicode
      !movzx rax, word [p.v_c]
      !rol ax, cl
      !mov [p.v_ret], ax 
    CompilerElse
      !movzx rax, byte [p.v_c]
      !rol al, cl
      !mov [p.v_ret], al
    CompilerEndIf
    
  CompilerEndIf 
  
  ProcedureReturn ret   
EndProcedure

Procedure Encode(*String.Character, i = 0)
  Protected.c c
  While *String\c
    i + 1 : c = -RolChar(*String\c, i + c)
    Swap *String\c, c : *String + SizeOf(Character)
  Wend
EndProcedure

Procedure Decode(*String.Character, i = 0)
  Protected.c c
  While *String\c
    i + 1 : c = RolChar(-*String\c, -(i + c))
    *String\c = c : *String + SizeOf(Character)
  Wend
EndProcedure


S.s = "This is a test string 123"
Encode(@S, 3)
;ShowMemoryViewer(@s,StringByteLength(s)) 
;CallDebugger 

Decode(@S, 3)
Debug s 

novablue
Enthusiast
Enthusiast
Posts: 165
Joined: Sun Nov 27, 2016 6:38 am

Re: How to change asm code to work with new c back end

Post by novablue »

Thank you that works!

I tried to convert the following code and it seems to be working. I left out the dword in the first asm line or it would give me an error. is this correct?

Original:

Code: Select all

Procedure.l Endian(val.l) 
    !MOV Eax,dword[p.v_val]
    !BSWAP Eax
    ProcedureReturn
EndProcedure

Debug Endian(12345) ; 959447040
Converted:

Code: Select all

Macro AsmInput(var) 
    !:[var] "r" (v_#var) 
EndMacro  

Macro AsmInput2(var0,var1) 
    !:[var0] "r" (v_#var0),[var1] "r" (v_#var1) 
EndMacro  

Macro AsmInput3(var0,var1,var2) 
    !:[var0] "r" (v_#var0),[var1] "r" (v_#var1),[var2] "r" (v_#var2)  
EndMacro

Macro AsmOutput(var)
    !".att_syntax;"
    !:[var] "=r" (v_#var)
EndMacro

Macro BeginAsm() 
    !__asm__(
    !".intel_syntax noprefix;"
EndMacro

Macro BeginAsmGoto() 
    !asm goto(
    !".intel_syntax noprefix;"
EndMacro

Macro EndAsm() 
    !);
EndMacro

Procedure.l Endian(val.l) 
    Define ret.l = 0 
    
    CompilerIf (#PB_Compiler_Backend = #PB_Backend_C) 
        BeginAsm()
        !"MOV Eax, %[val];"
        !"BSWAP Eax;"
        !"MOV %[ret], Eax;"
        AsmOutput(ret)
        AsmInput(val)
        EndAsm()
    CompilerElse 
        !MOV Eax,dword[p.v_val]
        !BSWAP Eax
        !mov [p.v_ret], Eax
    CompilerEndIf
    
    ProcedureReturn ret
EndProcedure

Debug Endian(12345) ; 959447040
User avatar
idle
Always Here
Always Here
Posts: 5042
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: How to change asm code to work with new c back end

Post by idle »

Looks OK you can also use __ builtin bswap in c instead. I'm away from computer
juergenkulow
Enthusiast
Enthusiast
Posts: 544
Joined: Wed Sep 25, 2019 10:18 am

Re: How to change asm code to work with new c back end

Post by juergenkulow »

It do not work under Windows X64 C Backend Optimizer:

Code: Select all

; [08:19:53] [ERROR] Zeile: 90
; [08:19:53] [ERROR] Ungültiger Speicherzugriff. (Schreibfehler an der Adresse 99226640)
; Unhandled exception at 0x00000001400010af in RolIdle.exe: 0xC0000005: Access violation writing location 0x00000000006b1e52.
; 		RCX	00000000000085A5	
; 		DX	BFFB	
; 		AX	4005	
; 		CL	A5	
; 		EDX	FFFFBFFB	

; 0000000140001070 | 4C:8B05 41330000         | mov r8,qword ptr ds:[1400043B8]         | r8:"Àúh", 00000001400043B8:&"Àúh"
; 0000000140001077 | 45:0FB708                | movzx r9d,word ptr ds:[r8]              | r8:"Àúh"
; 000000014000107B | 6645:85C9                | test r9w,r9w                            |
; 000000014000107F | 74 7B                    | je rolidle.1400010FC                    |
; 0000000140001081 | 31D2                     | XOr edx,edx                             |
; 0000000140001083 | B9 03000000              | mov ecx,3                               |
; 0000000140001088 | EB 09                    | jmp rolidle.140001093                   |
; 000000014000108A | 66:0F1F4400 00           | nop word ptr ds:[rax+rax],ax            |
; 0000000140001090 | 41:89C1                  | mov r9d,eax                             |
; 0000000140001093 | 48:83C1 01               | add rcx,1                               |
; 0000000140001097 | 41:0FB74448 FA           | movzx eax,word ptr ds:[r8+rcx*2-6]      |
; 000000014000109D | 48:01CA                  | add rdx,rcx                             |
; 00000001400010A0 | 48:89D1                  | mov rcx,rdx                             |
; 00000001400010A3 | 6644:89C8                | mov ax,r9w                              |
; 00000001400010A7 | 66:D3C0                  | rol ax,cl                               |
; 00000001400010AA | 66:89C2                  | mov dx,ax                               |
; 00000001400010AD | F7DA                     | neg edx                                 |
; 00000001400010AF | 6641:895448 F8           | mov word ptr ds:[r8+rcx*2-8],dx         |<------------------------------------
; 00000001400010B5 | 41:0FB7D1                | movzx edx,r9w                           |
; 00000001400010B9 | 66:85C0                  | test ax,ax                              |
; 00000001400010BC | 75 D2                    | jne rolidle.140001090                   |
; 00000001400010BE | 41:0FB710                | movzx edx,word ptr ds:[r8]              | r8:"Àúh"
; 00000001400010C2 | 66:85D2                  | test dx,dx                              |
; 00000001400010C5 | 74 35                    | je rolidle.1400010FC                    |
; 00000001400010C7 | B9 03000000              | mov ecx,3                               |
; 00000001400010CC | 0F1F40 00                | nop dword ptr ds:[rax],eax              |
; 00000001400010D0 | 48:83C1 01               | add rcx,1                               |
; 00000001400010D4 | 0FB7C0                   | movzx eax,ax                            |
; 00000001400010D7 | F7DA                     | neg edx                                 |
; 00000001400010D9 | 48:01C8                  | add rax,rcx                             |
; 00000001400010DC | 48:F7D8                  | neg rax                                 |
; 00000001400010DF | 48:89C1                  | mov rcx,rax                             |
; 00000001400010E2 | 66:89D0                  | mov ax,dx                               |
; 00000001400010E5 | 66:D3C0                  | rol ax,cl                               |
; 00000001400010E8 | 66:89C0                  | mov ax,ax                               |
; 00000001400010EB | 41:0FB75448 FA           | movzx edx,word ptr ds:[r8+rcx*2-6]      |
; 00000001400010F1 | 6641:894448 F8           | mov word ptr ds:[r8+rcx*2-8],ax         |
; 00000001400010F7 | 66:85D2                  | test dx,dx                              |
; 00000001400010FA | 75 D4                    | jne rolidle.1400010D0                   |
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: How to change asm code to work with new c back end

Post by wilbert »

This also seems to work for rotate left

Code: Select all

Global achar.a = 'a'
Global uchar.u = 'b'

; rotate byte
!v_achar = __builtin_ia32_rolqi(v_achar, 1);

; rotate word
!v_uchar = __builtin_ia32_rolhi(v_uchar, 1);

Debug Bin(achar)
Debug Bin(uchar)
Windows (x64)
Raspberry Pi OS (Arm64)
Post Reply