NO

Author Topic: RET issue inside an .IF block  (Read 2663 times)

Offline Vortex

  • Member
  • *
  • Posts: 802
    • http://www.vortex.masmcode.com
RET issue inside an .IF block
« on: April 18, 2012, 08:31:51 PM »
Assembling with Poasm V7.00.0,  the following code will miss a RET instruction :

Code: [Select]
.386
.model flat, stdcall
option casemap :none

.code

main PROC

    .if eax == 0
        ret
    .endif
    ret

main ENDP

END main

The resulting object file disassembled with Agner Fog's objconv tool :

Code: [Select]

_text   SEGMENT PARA PUBLIC 'CODE'

_main@0 PROC NEAR
        test    eax, eax
        jnz     ?_001
        ret
_main@0 ENDP

_text   ENDS

A RET instruction at the end of the code is missing.

After disassembling the same code assembled with MS Macro Assembler Version 6.14.8444 :

Code: [Select]
_text   SEGMENT DWORD PUBLIC 'CODE'                     ; section number 1

_main@0 PROC NEAR
        or      eax, eax                                ; 0000 _ 0B. C0
        jnz     ?_001                                   ; 0002 _ 75, 01
        ret                                             ; 0004 _ C3

?_001:  ret                                             ; 0005 _ C3
_main@0 ENDP

_text   ENDS

This issue was reported by Masm forum members.
Code it... That's all...

Offline AlexN

  • Global Moderator
  • Member
  • *****
  • Posts: 394
    • Alex's Link Sammlung
Re: RET issue inside an .IF block
« Reply #1 on: April 18, 2012, 08:42:27 PM »
I am not very familiar with assembler, but isn't the second ret in your code obsolete. Perhaps POASM tries to optimize the output code (for translating compiler output) .

If it is part of a larger code sequense with the same result, forget this post. ;)
best regards
 Alex ;)

Offline Vortex

  • Member
  • *
  • Posts: 802
    • http://www.vortex.masmcode.com
Re: RET issue inside an .IF block
« Reply #2 on: April 18, 2012, 08:56:46 PM »
Hi AlexN,

There is a problem concerning the RET instruction. An other example :

Code: [Select]
.386
.model flat, stdcall
option casemap :none

.code

main PROC x:DWORD,y:DWORD

    .if eax == 0
        xor ecx,ecx
        ret
    .endif

    ret

main ENDP

start:

     invoke main,10,20
     ret

END start

Output of Masm :

Code: [Select]

_text   SEGMENT DWORD PUBLIC 'CODE'

_main@8 PROC NEAR
        push    ebp
        mov     ebp, esp
        or      eax, eax
        jnz     ?_001
        xor     ecx, ecx
        leave       
        ret     8

?_001:  leave                                         
        ret     8                                     
_main@8 ENDP

_start  PROC NEAR
        push    20                                     
        push    10                                     
        call    _main@8                                 
        ret                                             
_start  ENDP

_text   ENDS

Output of Poasm :

Code: [Select]
_text   SEGMENT PARA PUBLIC 'CODE'

_main@8 PROC NEAR
        push    ebp
        mov     ebp, esp                               
        test    eax, eax                               
        jnz     _start                                 
        xor     ecx, ecx                               
        leave                                         
        ret     8                                     
_main@8 ENDP

_start  PROC NEAR
        push    20
        push    10
        call    _main@8
        ret             
_start  ENDP

_text   ENDS

Poasm should not direct the code execution to the _start label. The stack remains unbalanced after this jump.

Code: [Select]
jnz     _start
« Last Edit: April 18, 2012, 08:58:54 PM by Vortex »
Code it... That's all...

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
Re: RET issue inside an .IF block
« Reply #3 on: April 22, 2012, 05:20:13 PM »
I can reproduce it - will look at it.
/Pelle

Offline Vortex

  • Member
  • *
  • Posts: 802
    • http://www.vortex.masmcode.com
Re: RET issue inside an .IF block
« Reply #4 on: April 22, 2012, 07:44:46 PM »
Hi Pelle,

Many thanks.
Code it... That's all...