NOOB QUESTION: learning machine code

Anything QL Software or Programming Related.
User avatar
NormanDunbar
Forum Moderator
Posts: 2469
Joined: Tue Dec 14, 2010 9:04 am
Location: Buckie, Scotland
Contact:

Re: NOOB QUESTION: learning machine code

Post by NormanDunbar »

7alken wrote: Sat Apr 26, 2025 1:36 pm hi Norm, wow, what a cool book this one, up to PE ... never saw it till now, probably needs more audience :-)
Petr
Enjoy!

Cheers,
Norm.


Why do they put lightning conductors on churches?
Author of Arduino Software Internals
Author of Arduino Interrupts

No longer on Twitter, find me on https://mastodon.scot/@NormanDunbar.
User avatar
ql_freak
Gold Card
Posts: 481
Joined: Sun Jan 18, 2015 1:29 am

Re: NOOB QUESTION: learning machine code

Post by ql_freak »

The following program I have written today (does nothing, except adding a new SuperBASIC machine code function which does nothing). I have written it with the Computer One Assembler package from Dilwyns page (I normally use of couse QMAC, but will try GWASS):

Code: Select all

;reflection_asm - a reflection library for SuperBASIC
;U N D E R   C O N S T R U C T I O N
;
;ASSEMBLER: Computer One Assembler (because I want to use the symbolic
;debugging of Computer One Monitor and C1 Monitor recommends this Ed.)
;
;TODO: Write my SuperBASIC function "ref_getLnHi" as a S(uper)BASIC
;      machine code extension for S(uper)BASIC
;Start: 2025-04-24 Thu (Cottage) - Last update 2025-04-26 Fri (Home)
;
;NOTE: Maximum line length of C1 editor seems to be 80 chars (or so)! but with the continuation char in first column this editor line will be joined with the previous.
;
;First test if blank lines (without semicolons) are allowed in C1 Ed/As

bp_init         equ     $110    ;Vector BP.INIT to define MC PROC/FNs

start
        lea     define,a1       ;Load Effective Adress of SB PROC/FNs
        move.w  bp_init,a2      ;We must call SB vector BP.INIT
        jmp     (a2)            ;DO IT!

define
        dc.w 0,0                ;No procedures
        dc.w 2                  ;One function (but name is long so 2)
        dc.w ref_getLnLo-*
        dc.b 12,'REF_GETLNLO%'
        dc.w 0                  ;END of FuNctions

ref_getLnLo
        moveq   #0,d0           ;Currently this function does nothing
        rts                     ;  and returns no error (0 I hope)
Assembles with C1Asm and can be LREPRed and the e.g. PRINT GETLNNOLO% - unfortunately it outputs not 0 but nothing. I think to return a value to SuperBASIC from a machine code function, you must put the result (e.g. an int) onto the RI-stack.


http://peter-sulzer.bplaced.net
GERMAN! QL-Download page also available in English: GETLINE$() function, UNIX-like "ls" command, improved DIY-Toolkit function EDLINE$ - All with source. AND a good Python 3 Tutorial (German) for Win/UNIX :-)
Martin_Head
Aurora
Posts: 969
Joined: Tue Dec 17, 2013 1:17 pm

Re: NOOB QUESTION: learning machine code

Post by Martin_Head »

Yes, put the return value on the maths stack, and set D4 to one of-

1 String
2 float
3 word integer


User avatar
tofro
Font of All Knowledge
Posts: 3081
Joined: Sun Feb 13, 2011 10:53 pm
Location: SW Germany

Re: NOOB QUESTION: learning machine code

Post by tofro »

Martin_Head wrote: Sun Apr 27, 2025 8:43 am Yes, put the return value on the maths stack, and set D4 to one of-

1 String
2 float
3 word integer
And make sure you clean up the math stack once you start using it (you need to remove all of the space off (a1,a6) you might have used with the CA_GTxxxx vectors, then put the return value there and adjust bv_rip(a6)). QDOS will do that cleanup for you in procedures, but not functions (That is why it is useful for your first machine code extensions to start with procedures, then elevate to functions, once you get the hang of it).


ʎɐqǝ ɯoɹɟ ǝq oʇ ƃuᴉoƃ ʇou sᴉ pɹɐoqʎǝʞ ʇxǝu ʎɯ 'ɹɐǝp ɥO
User avatar
NormanDunbar
Forum Moderator
Posts: 2469
Joined: Tue Dec 14, 2010 9:04 am
Location: Buckie, Scotland
Contact:

Re: NOOB QUESTION: learning machine code

Post by NormanDunbar »

All this, and more, is covered in my free eBook created from years and years and years of writing for the sadly defunct QL Today magazine.

https://github.com/NormanDunbar/QLAssem ... tober-2020

Also, my occasional eMagazines on the subject, currently up to issue 12 at

https://github.com/NormanDunbar/QLAssem ... e/releases

HTH

Cheers,
Norm.


Why do they put lightning conductors on churches?
Author of Arduino Software Internals
Author of Arduino Interrupts

No longer on Twitter, find me on https://mastodon.scot/@NormanDunbar.
Tinyfpga
Gold Card
Posts: 304
Joined: Thu Sep 27, 2018 1:59 am

Re: NOOB QUESTION: learning machine code

Post by Tinyfpga »

What is the difference between your occasional ( 12 ) Emagazines and your assembly book, and are they written using smsqe or qdos names?

Also your post in the QBAS_SYS thread,

"Briefly,
in order to use the DBAS system, you first have to perform a TRAP #3 instruction to fetch the DBAS main
vector. All the actual database handling functions as simply offsets on that vector.
So, in assembly (and what the SuperBASIC code or C68 Code will be doing under the covers), once the trap
#3 returns, you have an address. Shove that in A%, for example, and now all the code to manipulate the
database is relative to that address.
You might find that create a database is offset by 0 so would be JSR 0(A5) or just JSR (a5) for example;
open a database is offset by 2 and would be JSR 2(A5); and so on."

led me to imagine SMS as a complex plot relying on numerous elements coming together to form a working operating system. Something like a software cutaway diagram.

Years ago Tony Tebby drew a number of drawings that could be considered an introduction to such a diagram.


User avatar
NormanDunbar
Forum Moderator
Posts: 2469
Joined: Tue Dec 14, 2010 9:04 am
Location: Buckie, Scotland
Contact:

Re: NOOB QUESTION: learning machine code

Post by NormanDunbar »

The book is a beginners guide, it starts simple and moves on. The eMagazines are a progression from the end of the book.

The book and eMagazined use QDIS names mostly but the last couple of eMagazines use SMSQ names.

The DBAS explanation applies equally to QDOS and SMSQ. And any operating system is a complex analgum of many different subsystems.

Cheers,
Norm.


Why do they put lightning conductors on churches?
Author of Arduino Software Internals
Author of Arduino Interrupts

No longer on Twitter, find me on https://mastodon.scot/@NormanDunbar.
User avatar
ql_freak
Gold Card
Posts: 481
Joined: Sun Jan 18, 2015 1:29 am

Re: NOOB QUESTION: learning machine code

Post by ql_freak »

Now it does something: RETurns first line number of currently loaded SuperBASIC program (tested on sQLux with Minerva) :-)

Yeah! My first assembler program created with Computer One Assembler :-))) (normally using QMAC)

Code: Select all

;reflection_asm - a reflection library for SuperBASIC
;U N D E R   C O N S T R U C T I O N
;
;ASSEMBLER: Computer One Assembler (because I want to use the symbolic
;debugging of Computer One Monitor and C1 Monitor recommends this Ed.)
;
;TODO: Write my SuperBASIC function "ref_getLnHi" as a S(uper)BASIC
;      machine code extension for S(uper)BASIC
;Start: 2025-04-24 Thu (Cottage) - Last update 2025-04-26 Fri (Home)
;
;NOTE: Maximum line length of C1 editor seems to be 80 chars (or so)! but with the continuation char in first column this editor line will be joined with the previous.
;
;First test if blank lines (without semicolons) are allowed in C1 Ed/As

;Note: We use RI stack (also called arithmetic stack or maths stack)

BP_INIT         equ     $110       ;Vector BP.INIT to define MC PROC/FNs
BV_CHRIX        equ     $11a       ;Vector BV.CHRIX

BV_PFBAS        equ     $10        ;Start of program
BV_PFB          equ     $14        ;End of program
BV_RIP          equ     $58        ;BASIC storage for RI stack

QSTR            equ     1          ;Qdos STRing
QFLT            EQU     2          ;Qdos FLoaT
QINT            EQU     3          ;Qdos INteger (WORD, i.e. short in C!)

start
        lea     define,a1          ;Load Effective Adress of SB PROC/FNs
        move.w  BP_INIT,a2         ;We must call SB vector BP.INIT
        jmp     (a2)               ;DO IT!

define
        dc.w    0,0                ;No procedures
        dc.w    2                  ;One function (but name is long so 2)
        dc.w    ref_getLnLo-*
        dc.b    12,'REF_GETLNLO%'
        dc.w 0                     ;END of FuNctions

ref_getLnLo                        ;0(a6,a1.l) points to RI stack
        move.l  BV_PFBAS(a6),a2    ;Get pointer to start of program
        move.w  4(a6,a2.l),d4      ;Get line number of first line
        move.w  BV_CHRIX,a0        ;We need space on RI stack
        move.l  #2,d0              ;2 bytes required for line number
        move.l  a1,BV_RIP(a6)      ;Stack may move at BV.CHRIX so save it
        jsr     (a0)
        move.l  BV_RIP(a6),a1      ;Restore RI stack pointer
        move.w  d4,0(a6,a1.l)      ;Push line no. (short int) ...
        addq.l  #2,a1              ;... on RI stack AND (don't forget!):
        moveq   #QINT,d4           ;... set return type (integer [short])

        moveq   #0,d0              ;No error
        rts                        ;Return to SuperBASIC


http://peter-sulzer.bplaced.net
GERMAN! QL-Download page also available in English: GETLINE$() function, UNIX-like "ls" command, improved DIY-Toolkit function EDLINE$ - All with source. AND a good Python 3 Tutorial (German) for Win/UNIX :-)
User avatar
NormanDunbar
Forum Moderator
Posts: 2469
Joined: Tue Dec 14, 2010 9:04 am
Location: Buckie, Scotland
Contact:

Re: NOOB QUESTION: learning machine code

Post by NormanDunbar »

That codeshouldnt work! You are trashing the arithmetic stack!

When you call BV_CHRIX to reserve 2 bytes, you must then...

Code: Select all

    Move.l bv_rip(a6),a1
    Subq.l #2,a1
    Move.l a1,bv_rip(a6)
    Move.w d4,0(a6,a1.l)
    Moveq #QINT,d4
    ...
Don't add to make space on the stack, subtract.

I suspect that if you were to use your current version as part of an expression, then results may be interesting!

Cheers,
Norm.


Why do they put lightning conductors on churches?
Author of Arduino Software Internals
Author of Arduino Interrupts

No longer on Twitter, find me on https://mastodon.scot/@NormanDunbar.
User avatar
dilwyn
Mr QL
Posts: 3057
Joined: Wed Dec 01, 2010 10:39 pm

Re: NOOB QUESTION: learning machine code

Post by dilwyn »

Norman, your old article about this subject is still available from my website: https://dilwyn.theqlforum.com/docs/articles/stack.zip, which Peter may find helpful.


Post Reply