• Welcome to Theos PowerBasic Museum 2017.

(Optimization) Iimplicit and explicit type casting

Started by Theo Gottwald, January 02, 2007, 09:41:45 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Theo Gottwald

I always wanted to know, how the compiler would do a type casting.
As VB has no Poke Command, I used the chance to make one in a PB-DLL.

' First try:

SUB PokeB ALIAS "PokeB" (BYVAL T01 AS LONG,BYVAL T02 AS LONG)
' As we need "Poke DWORD,BYTE", we make a explicit type-cast here.
' We tell the compiler to use the low-byte from the low-word of the LONG variable.
  POKE T01,LO(BYTE,LO(WORD,T02))   
END SUB 


This is what the compiler makes out of it:


' Move T01-Content into EAX-Register
408E17 8B4508                 MOV EAX, DWORD PTR [EBP+08]
' Move EAX-Register into EBX-Register
408E1A 8BD8                   MOV EBX, EAX
' Move T02 into EAX Register
408E1C 8B450C                 MOV EAX, DWORD PTR [EBP+0C]
' Explizit Type-Cast
408E1F 0FB7C0                 MOVZX EAX,EAX
408E22 0FB6C0                 MOVZX EAX,AL
' Poke-Command
408E25 8803                   MOV BYTE PTR [EBX], AL


If I had not done any explicit Type-cast, the compiler just does it properly and even shorter:

SUB PokeB ALIAS "PokeB" (BYVAL T01 AS LONG,BYVAL T02 AS LONG)
   POKE T01,T02
END SUB


408E28 8B4508                 MOV EAX, DWORD PTR [EBP+08]
408E2B 8B5D0C                 MOV EBX, DWORD PTR [EBP+0C]
408E2E 8818                   MOV BYTE PTR [EAX], BL



Finally I could have done it in "Hand optimized ASM like this:

SUB PokeB ALIAS "PokeB" (BYVAL T01 AS LONG,BYVAL T02 AS LONG)
    !MOV EAX,T02
    !MOV EBX,T01
    !MOV Byte PTR[EBX],AL
END SUB 


which is exactly the same what the compiler would make out of it.


Donald Darden

This is a consequence of overspecification.  The statement:
     POKE T01,LO(BYTE,LO(WORD,T02))
told the compiler to add two functional calls (the use of LO()) to the process.  You might do this in cases where you are unsure what the compiler would do, or are striving to override a default behavour of the compiler.  As you gain experience in working with the results of the compiler, you will come to know what to expect in such circumstances.