flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > Minor problem with 'if.inc' macro

Author
Thread Post new topic Reply to topic
madmatt



Joined: 07 Oct 2003
Posts: 1045
Location: Michigan, USA
madmatt 07 Aug 2010, 23:06
Hello everyone, I thought I would report a small bug with the 'if.inc' macro include, It seems to produce an extra 'jmp' right after you 'ret', before you reach the '.else' part of the macro. Is there a simple fix for this?

Code:
;[ORIGINAL CODE]
proc DDRestoreBack
     .if     dword [ddback] <> NULL
             cominvk ddback, Restore; ;this reattaches the video memory to the surface
             return
     .else
             return  -1
     .endif
endp

;[CODE DIS-ASSEMBLED WITH OBJCONV BY AGNER FOG]
        nop                                             ; 004210FD _ 90
        nop                                             ; 004210FE _ 90
        nop                                             ; 004210FF _ 90

DDRestoreBack:
        cmp     dword [?_0070], 0                       ; 00421100 _ 83. 3D, 00402248(d), 00
        jz      ?_0482                                  ; 00421107 _ 74, 0E
        mov     eax, dword [?_0070]                     ; 00421109 _ A1, 00402248(d)
        push    eax                                     ; 0042110E _ 50
        mov     eax, dword [eax]                        ; 0042110F _ 8B. 00
        call    near [eax+6CH]                          ; 00421111 _ FF. 50, 6C
        ret                                             ; 00421114 _ C3

; Note: Inaccessible code
        jmp     ?_0483                                  ; 00421115 _ EB, 06

?_0482: mov     eax, 4294967295                         ; 00421117 _ B8, FFFFFFFF
        ret                                             ; 0042111C _ C3

?_0483: nop                                             ; 0042111D _ 90
        nop                                             ; 0042111E _ 90
        nop                                             ; 0042111F _ 90
        nop                                             ; 00421120 _ 90
    

_________________
Gimme a sledge hammer! I'LL FIX IT!
Post 07 Aug 2010, 23:06
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 08 Aug 2010, 00:36
I think that detecting whether the jmp that skips the else (or elseIf) block is required or not is too hard even if considering ret alone only. For instance, I think you won't find a method to detect situations like this:

Code:
proc foo, arg
  .if dword [arg] <> NULL
    call [arg]
    test eax, eax
    js @f
    return
@@:
  .else
    return -1
  .endif
  
  return -2
endp    
This clearly kill the naive method of detecting if the last instruction in the If/ElseIf body was a RET to perform JMP suppression. If we assume that there are no jumps from nowhere into any part of the body nor intra-body jumps, the naive method is still a problem:

Code:
proc foo, arg
  .if dword [arg] <> NULL
    call [arg]
    .if eax >= 0
      return
    .endif
@@:
  .else
    return -1
  .endif
  
  return -2
endp    
But perhaps correctly detecting when the JMP to skip the else is needed is doable with fasm, the other one I think it is impossible to get it right.
Post 08 Aug 2010, 00:36
View user's profile Send private message Reply with quote
madmatt



Joined: 07 Oct 2003
Posts: 1045
Location: Michigan, USA
madmatt 08 Aug 2010, 13:05
Quote:
But perhaps correctly detecting when the JMP to skip the else is needed is doable with fasm, the other one I think it is impossible to get it right.


I was thinking something like having something like '.else NJ' (NJ = no jmp) to signal that a 'jmp' shouldn't be used when the .else macro keyword is used.
Post 08 Aug 2010, 13:05
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8353
Location: Kraków, Poland
Tomasz Grysztar 08 Aug 2010, 13:23
madmatt wrote:
I was thinking something like having something like '.else NJ' (NJ = no jmp) to signal that a 'jmp' shouldn't be used when the .else macro keyword is used.
Isn't it better to just write it this way?
Code:
     .if     dword [ddback] <> NULL 
             cominvk ddback, Restore; ;this reattaches the video memory to the surface 
             return 
     .endif 
             return  -1     
Post 08 Aug 2010, 13:23
View user's profile Send private message Visit poster's website Reply with quote
madmatt



Joined: 07 Oct 2003
Posts: 1045
Location: Michigan, USA
madmatt 08 Aug 2010, 17:49
Tomasz Grysztar wrote:
madmatt wrote:
I was thinking something like having something like '.else NJ' (NJ = no jmp) to signal that a 'jmp' shouldn't be used when the .else macro keyword is used.
Isn't it better to just write it this way?
Code:
     .if     dword [ddback] <> NULL 
             cominvk ddback, Restore; ;this reattaches the video memory to the surface 
             return 
     .endif 
             return  -1     


Yeh, that would work better. But, still seems to need a fix when it is inserting code that is not needed (obviously, this should be a very low priority fix, nothing is truly broken here.). By the way, also discovered that the '.elseif' macro does the same thing. see code below.
Code:
;[ORIGINAL CODE]
proc DDGetDefaultFont  fontaddress, fontnumber
     .if     [fontnumber] = DDCHARFONTVGA8
             cinvoke memcpy, [fontaddress], vgafont8x8, 2048
             return  DD_OK

     .elseif [fontnumber] = DDCHARFONTATR8
             cinvoke memcpy, [fontaddress], atarifont8x8, 2048
             return  DD_OK

     .elseif [fontnumber] = DDCHARFONTAPP8
             cinvoke memcpy, [fontaddress], AppleFont8x8, 2048
             return  DD_OK

     .elseif [fontnumber] = DDCHARFONTC648
             cinvoke memcpy, [fontaddress], c64font8x8, 4096
             return  DD_OK

     .elseif [fontnumber] = DDCHARFONTVGA16
             cinvoke memcpy, [fontaddress], vgafont8x16, 4096
             return  DD_OK
     .else 1
             return -1
     .endif
endp

;[CODE DIS-ASSEMBLED WITH OBJCONV BY AGNER FOG]
        nop                                             ; 00423D7A _ 90
        nop                                             ; 00423D7B _ 90
        nop                                             ; 00423D7C _ 90
        nop                                             ; 00423D7D _ 90
        nop                                             ; 00423D7E _ 90
        nop                                             ; 00423D7F _ 90

DDGetDefaultFont:; Function begin
        push    ebp                                     ; 00423D80 _ 55
        mov     ebp, esp                                ; 00423D81 _ 89. E5
        cmp     dword [ebp+0CH], 0                      ; 00423D83 _ 83. 7D, 0C, 00
        jnz     ?_0716                                  ; 00423D87 _ 75, 24
        push    2048                                    ; 00423D89 _ 68, 00000800
        push    ?_0145                                  ; 00423D8E _ 68, 004029C0(d)
        push    dword [ebp+8H]                          ; 00423D93 _ FF. 75, 08
        call    near [imp_memcpy]                       ; 00423D96 _ FF. 15, 00429516(d)
        add     esp, 12                                 ; 00423D9C _ 83. C4, 0C
        mov     eax, 0                                  ; 00423D9F _ B8, 00000000
        leave                                           ; 00423DA4 _ C9
        ret     8                                       ; 00423DA5 _ C2, 0008

; Note: Inaccessible code
        jmp     ?_0721                                  ; 00423DA8 _ E9, 000000A3

?_0716: cmp     dword [ebp+0CH], 1                      ; 00423DAD _ 83. 7D, 0C, 01
        jnz     ?_0717                                  ; 00423DB1 _ 75, 21
        push    2048                                    ; 00423DB3 _ 68, 00000800
        push    ?_0148                                  ; 00423DB8 _ 68, 004051C0(d)
        push    dword [ebp+8H]                          ; 00423DBD _ FF. 75, 08
        call    near [imp_memcpy]                       ; 00423DC0 _ FF. 15, 00429516(d)
        add     esp, 12                                 ; 00423DC6 _ 83. C4, 0C
        mov     eax, 0                                  ; 00423DC9 _ B8, 00000000
        leave                                           ; 00423DCE _ C9
        ret     8                                       ; 00423DCF _ C2, 0008

; Note: Inaccessible code
        jmp     ?_0721                                  ; 00423DD2 _ EB, 7C

?_0717: cmp     dword [ebp+0CH], 2                      ; 00423DD4 _ 83. 7D, 0C, 02
        jnz     ?_0718                                  ; 00423DD8 _ 75, 21
        push    2048                                    ; 00423DDA _ 68, 00000800
        push    ?_0149                                  ; 00423DDF _ 68, 004059C0(d)
        push    dword [ebp+8H]                          ; 00423DE4 _ FF. 75, 08
        call    near [imp_memcpy]                       ; 00423DE7 _ FF. 15, 00429516(d)
        add     esp, 12                                 ; 00423DED _ 83. C4, 0C
        mov     eax, 0                                  ; 00423DF0 _ B8, 00000000
        leave                                           ; 00423DF5 _ C9
        ret     8                                       ; 00423DF6 _ C2, 0008

; Note: Inaccessible code
        jmp     ?_0721                                  ; 00423DF9 _ EB, 55

?_0718: cmp     dword [ebp+0CH], 3                      ; 00423DFB _ 83. 7D, 0C, 03
        jnz     ?_0719                                  ; 00423DFF _ 75, 21
        push    4096                                    ; 00423E01 _ 68, 00001000
        push    ?_0147                                  ; 00423E06 _ 68, 004041C0(d)
        push    dword [ebp+8H]                          ; 00423E0B _ FF. 75, 08
        call    near [imp_memcpy]                       ; 00423E0E _ FF. 15, 00429516(d)
        add     esp, 12                                 ; 00423E14 _ 83. C4, 0C
        mov     eax, 0                                  ; 00423E17 _ B8, 00000000
        leave                                           ; 00423E1C _ C9
        ret     8                                       ; 00423E1D _ C2, 0008

; Note: Inaccessible code
        jmp     ?_0721                                  ; 00423E20 _ EB, 2E

?_0719: cmp     dword [ebp+0CH], 4                      ; 00423E22 _ 83. 7D, 0C, 04
        jnz     ?_0720                                  ; 00423E26 _ 75, 1F
        push    4096                                    ; 00423E28 _ 68, 00001000
        push    ?_0146                                  ; 00423E2D _ 68, 004031C0(d)
        push    dword [ebp+8H]                          ; 00423E32 _ FF. 75, 08
        call    near [imp_memcpy]                       ; 00423E35 _ FF. 15, 00429516(d)
        add     esp, 12                                 ; 00423E3B _ 83. C4, 0C
        mov     eax, 0                                  ; 00423E3E _ B8, 00000000
        leave                                           ; 00423E43 _ C9
        ret     8                                       ; 00423E44 _ C2, 0008

?_0720: mov     eax, 4294967295                         ; 00423E47 _ B8, FFFFFFFF
        leave                                           ; 00423E4C _ C9
        ret     8                                       ; 00423E4D _ C2, 0008

?_0721: nop                                             ; 00423E50 _ 90
        nop                                             ; 00423E51 _ 90
        nop                                             ; 00423E52 _ 90
        nop                                             ; 00423E53 _ 90
        nop                                             ; 00423E54 _ 90
        nop                                             ; 00423E55 _ 90
        nop                                             ; 00423E56 _ 90
        nop                                             ; 00423E57 _ 90
        nop                                             ; 00423E58 _ 90
        nop                                             ; 00423E59 _ 90
    

_________________
Gimme a sledge hammer! I'LL FIX IT!
Post 08 Aug 2010, 17:49
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.