Opening a file from assembly

Discussions related to using the integrated assembler
Ric
Posts: 261
Joined: Tue 17 Apr 2018, 21:03

Opening a file from assembly

Post by Ric »

Could someone please explain why th efollowing code does not work

Code: Select all

 
      PROCassembly

      CALL start

      CLOSE#0

      END

      DEF PROCassembly

      DIM code% NOTEND AND 2047, code% 2048-1

      FOR I% = 0 TO 2 STEP 2
  
        P% = code%
        [
  
        opt I%
        .text DB "success.bbc"
        DB &D
        .start
        mov ebx,text
        mov eax,0
        mov al,1
        call "osopen"
        ret
  
        ]
      NEXT

      ENDPROC
Kind Regards Ric.

6502 back in the day, BB4W 2017 onwards, BBCSDL from 2023
Richard Russell
Posts: 540
Joined: Tue 18 Jun 2024, 09:32

Re: Opening a file from assembly

Post by Richard Russell »

Ric wrote: Sun 14 Dec 2025, 21:50 Could someone please explain why the following code does not work

Code: Select all

        mov ebx,text
        mov eax,0
        mov al,1
        call "osopen"
        ret
The relevant section of the manual states:

Code: Select all

CALL "osopen" ; Open a file, EDX addresses filename, AL = 0 (read),
              ; 1 (create) or 2 (update), channel number returned in EAX
The main difference from your code is that the documentation says edx but your code has ebx. Also your code has a mov eax,0 which is superfluous.
Ric
Posts: 261
Joined: Tue 17 Apr 2018, 21:03

Re: Opening a file from assembly

Post by Ric »

Thanks Richard,
I was always under the impression that if you were only loading the low or high byte, the rest of the register remained unchanged, thus the need to flush it first.
Kind Regards Ric.

6502 back in the day, BB4W 2017 onwards, BBCSDL from 2023
Richard Russell
Posts: 540
Joined: Tue 18 Jun 2024, 09:32

Re: Opening a file from assembly

Post by Richard Russell »

Ric wrote: Mon 15 Dec 2025, 11:13 I was always under the impression that if you were only loading the low or high byte, the rest of the register remained unchanged, thus the need to flush it first.
The rest of the register remains unchanged, yes, but that's fine since the subsequent code doesn't care about the rest of the register. If it did, osopen would be documented as receiving the parameter in eax, not in al, and your code would need to be:

Code: Select all

        mov edx,text
        mov eax,1
        call "osopen"
        ret
This code will work, of course, but it's wasteful because it's zeroing the other 24 bits of eax unnecessarily.

In a circumstance, which isn't the case here, when you need to zero-extend or sign-extend the LS 8 bits of eax into the remaining 24 bits then use the specific instructions provided:

Code: Select all

        movzx eax,al	; zero-extend al into eax
        movsx eax,al	; sign-extend al into eax
Finally, remember that on a 64-bit CPU zero-extending a 32-bit register into a 64-bit register happens automatically, it doesn't need any explicit code. For example xor eax,eax zeroes all 64-bits of rax, not just the lower 32-bits as you might expect.
Ric
Posts: 261
Joined: Tue 17 Apr 2018, 21:03

Re: Opening a file from assembly

Post by Ric »

Thanks Richard
Kind Regards Ric.

6502 back in the day, BB4W 2017 onwards, BBCSDL from 2023