Exiting from ASM

New to FreeBASIC? Post your questions here.
fzabkar
Posts: 110
Joined: Sep 29, 2018 2:52
Location: Australia

Exiting from ASM

Postby fzabkar » Apr 08, 2021 22:43

Is there a proper way to exit this ASM block? I'm JMP-ing to an empty line because I can't see any other way to do it. Is there a "quit" command which I could use in place of "jmp Fin"?

Code: Select all

Dim bytvar As Byte
Dim bstatus As Byte

Do   
    Input "Enter a byte value (0 to quit): ", bytvar

    If bytvar = 0 Then
        Exit Do
    End If

' If bytvar = 1, then Status = 1, else Status = 0
   
    ASM
            cmpb [bytvar], 1
            jne Stat0
            movb [bstatus], 1
            jmp Fin
    Stat0:  movb [bstatus], 0
    Fin:
   
    End Asm

   
    Print "Status = "; bstatus

Loop

Sleep
MrSwiss
Posts: 3826
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Exiting from ASM

Postby MrSwiss » Apr 08, 2021 22:52

That is pretty simple (as in other block statements):

Code: Select all

Asm
    ' ...
End Asm
There isn't anything like Exit Asm currently (FBC 1.07.n).
fzabkar
Posts: 110
Joined: Sep 29, 2018 2:52
Location: Australia

Re: Exiting from ASM

Postby fzabkar » Apr 08, 2021 23:35

Thanks.
caseih
Posts: 1705
Joined: Feb 26, 2007 5:32

Re: Exiting from ASM

Postby caseih » Apr 08, 2021 23:54

Any "exit asm" command would just be a high-level syntactic sugar for the exact same jump. Since the goal of ASM is to allow low-level assembly code directly in your program, I would assume any user of this facility would want to do their own jumps explicitly. That's the point of ASM after all.
fzabkar
Posts: 110
Joined: Sep 29, 2018 2:52
Location: Australia

Re: Exiting from ASM

Postby fzabkar » Apr 09, 2021 1:40

caseih wrote:Any "exit asm" command would just be a high-level syntactic sugar for the exact same jump. Since the goal of ASM is to allow low-level assembly code directly in your program, I would assume any user of this facility would want to do their own jumps explicitly. That's the point of ASM after all.

Well, I explicitly want to JMP to the end of the ASM. ;-) Jumping to an empty line seems inelegant, if not illogical.

I notice that ASM has a "leave" instruction, but that incorporates a RET. In any case, if there is a LEAVE pseudo-instruction or directive or whatever it is called, then I see no reason why there couldn't be an EXIT ASM.
caseih
Posts: 1705
Joined: Feb 26, 2007 5:32

Re: Exiting from ASM

Postby caseih » Apr 09, 2021 1:55

Well good news, then. If jmp is what you explicitly want, then you explicitly type it! You seem to prefer an implicit jump, however. Just to be pedantic. ;)

While it's true that assemblers heavily rely on macros to add syntactic sugar, in this case your reference to "leave" is an actual opcode, if I'm not mistaken, which cleans up the stack frame and jumps to the caller. It's real instruction, not a psuedo instruction.

I'm not sure exactly how much work FB puts into the ASM block, but traditionally the contents of the block are passed more or less directly to the assembler. So if you wanted an EXIT ASM statement it would have to be something that was part of and provided by the assembler, unless I'm very much mistaken. That could be the reason why no one ever thought of implementing that before.
grindstone
Posts: 797
Joined: May 05, 2015 5:35
Location: Germany

Re: Exiting from ASM

Postby grindstone » Apr 09, 2021 7:26

fzabkar wrote:Jumping to an empty line seems inelegant, if not illogical.

Then just use the NOP (=No Operation) statement:

Code: Select all

Dim bytvar As Byte
Dim bstatus As Byte

Do   
    Input "Enter a byte value (0 to quit): ", bytvar

    If bytvar = 0 Then
        Exit Do
    End If

' If bytvar = 1, then Status = 1, else Status = 0
   
    ASM
            cmpb [bytvar], 1
            jne Stat0
            movb [bstatus], 1
            jmp Fin
    Stat0:  movb [bstatus], 0
    Fin:  nop
   
    End Asm

   
    Print "Status = "; bstatus

Loop

Sleep
jj2007
Posts: 2144
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Exiting from ASM

Postby jj2007 » Apr 09, 2021 8:53

Here is a variant from your swap & endianness thread:

Code: Select all

' Assembly language function to reverse the endian-ness of a block of 32-bit dwords

Function EndianRev32 naked ( ByRef srcNums As uLong, ByRef destNums As uLong, ByVal elements as uLong ) As uLong
  ASM
   push esi
   push edi
   mov ecx, [esp+8+12]   'elements
   mov esi, [esp+8+4]    'srcNums
   mov edi, [esp+8+8]    'destNums
   push ecx
  L1:   lodsd
   bswap eax
   stosd
   dec ecx
   jg L1
   pop eax   ' return #elements
   pop edi
   pop esi
   ' mov [function], ecx won't work in naked functions
   ret 12      ' return with three ulong arguments
  End ASM   
End Function

dim s(10) as ulong=>{100, 101, 102, 103, 104, 105, 106, 107, 108, 109}
dim d(10) as ulong

Print "Swapping src->dest:"
Print EndianRev32(s(0), d(0), 10);" elements swapped"
  For ct As long=0 To 9
   Print ct, hex(d(ct))
  Next

Print "Swapping dest->dest:"
Print EndianRev32(d(0), d(0), 10);" elements swapped"
  For ct As long=0 To 9
   Print ct, hex(d(ct))
  Next
sleep

caseih wrote:traditionally the contents of the block are passed more or less directly to the assembler
True if you use the naked keyword, see above.

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 5 guests