Vampier's quest for assembly [hopefully a n00b guide to assembly]

Pagina 1/21
| 2 | 3 | 4 | 5 | 6

Door Vampier

Prophet (2352)

afbeelding van Vampier

14-02-2010, 20:04

Believe it or not, I did not know how to program in MSX z80 assembler up until Feb 12 2010. I had done several attempts which basically ended as soon as I started…. It was pretty frustrating to read about registers, stacks pointers and other terms I did not know anything about.

Anyway after asking Bifi which assembler was a good one to start with I downloaded WBASS2
http://www.xs4all.nl/~wbsoft/msx/

An assembler allows you to program into ‘human readable’ text which is translated directly into machine language which are the 1’s and 0’s that control your chips.

After starting WBASS2 I read the tutorial (WBass manual... only in Dutch) and worked through it until half way. I decided to just start typing what I was told (actually I used the copy/paste function in openMSX through catapult) and was surprised by the structure of assembly… I knew why I couldn’t get started… it doesn’t work like anything close to MSX basic!

I was left with more questions than answers after this brief introduction to assembly. So Bifi gave me a head spinning lecture about registers, pointers and BIOS calls (I know you die hard assembly guys like to stay away from BIOS calls :P)

I am starting to think about an MSXDevX Entry which I was going to use a hybrid screen 2 mode for (screen1 freedom on screen2 but with the benefits of 16 colors per char) Bifi gave me a program for this quite some time ago and it always bugged me to see how slow it was. So I started looking at the BASIC program:

10 COLOR15,0,0:SCREEN2:POKE &HFCB0,1:POKE&HFCAF,1
20 FOR I=0TO2047:J=PEEK(PEEK(4)+256*PEEK(5)+I)
30 VPOKEI,J:VPOKEI+2048,J:VPOKEI+4096,J
40 VPOKE I+8192,248:VPOKEI+10240,31:VPOKEI+12288,244:NEXT I
50 WIDTH 32

So Call Screen 2 and copy the charset to the 3 portions of screen2 (look up how screen2 works if you want to know :P) And as an added bonus make it look nice to illustrate the 3 different areas.

Anyway to stop bugging Bifi all the time I skimmed over http://www.worldofspectrum.org/Z80.html a bit (hey z80 is z80, only the bios calls are different as far as I’m concerned)

So I just started fiddling around with assembly and a great resource I got (from Bifi again) on BIOS calls: http://map.grauw.nl/resources/msxbios.php

ORG   &HC000

;color 15,1,1
INITCO: LD    HL,&HF3E9
		LD    (HL),15
		LD    HL,&HF3EA
		LD    (HL),4
		LD    HL,&HF3EB
		LD    (HL),0
		CALL  &H62
           
;let me type in screen2
INIT:   LD    HL,&HFCB0
		LD    (HL),1

		LD    HL,&HFCAF
		LD    (HL),1
		
;init charset (copy from ram to vram)
SETCHR: LD    HL,7103
		LD    DE,0
		LD    BC,2048
		CALL  &H5C
		LD    HL,7103
		LD    DE,2048
		LD    BC,2048
		CALL  &H5C
		LD    HL,7103
		LD    DE,4096
		LD    BC,2048
		CALL  &H5C
   
;set colors
SETCOL: LD    A,&HF8
		LD    BC,2048
		LD    HL,8192
		CALL  &H56
		LD    A,&H1F
		LD    BC,2048
		LD    HL,&H28
		CALL  &H56
		LD    A,&HF4
		LD    BC,2048
		LD    HL,&H30
		CALL  &H56
;exit
		RET

After assembling this in memory I went to basic and typed:

10 defusr=&hc000
11 screen2:u=usr(0)
12 width 32

And tears of joy (just kidding) appeared... EUREKA!!!! I DID IT!!! My first assembly program was born!

It's still top/down thinking though.

Until this far :) more to come

(also a thanks to n_n and Max Iwamoto for support)

Aangemeld of registreer om reacties te plaatsen

Van ARTRAG

Enlighted (6443)

afbeelding van ARTRAG

14-02-2010, 23:42

Needless to say, you should look at the vdp registers
http://map.grauw.nl/resources/video/v9938/v9938.xhtml
and try to do without bios calls
This is the best way to get something good from msx

(apart from using sjasm as assembler
http://home.wanadoo.nl/smastijn/sjasm.html#sjasm4
crossdevelopment rules)

Van MOA

Champion (293)

afbeelding van MOA

14-02-2010, 23:44

Well done! Smile

Now learn about EQU and start making your code more readable.

(edit)

After that you can try some loops:

   LD B,<number of iterations>
.loop
   .
   .
   DJNZ .loop

or

   LD BC,<large number of iterations>
.loop
   .
   .
   DEC BC
   LD A,B
   OR C
   JP NZ,.loop

and conditionals:

  LD A,<boolean>
  OR A
  JP NZ,.true
.false
  .
  .
  JP .done
.true
  .
  .
.done

Van RetroTechie

Paragon (1563)

afbeelding van RetroTechie

15-02-2010, 16:37

Learning to code in assembly basically consists of 2 parts:

  • 1) Understanding how a CPU (in our case: Z80) works on a low level, from a programmer's point of view.
  • 2) Understanding how the hardware is organized in a machine with this CPU in it (in our case: the MSX), how to address/use the various parts (for sound, video, disk etc). Again, from a programmer's point of view. Understanding how the hardware works is helpful, but not necessary.

Try not to confuse these: if you become a good assembly coder on MSX, and you have a good understanding of what's 'Z80' and what's 'MSX' while coding, then moving to another Z80-based platform is easy since it involves only 2)

IMHO 1) can be learned quickly, but may take a long time+practice to become really good at. And for the MSX, 2) is more work, because some parts are more complex than the Z80 itself. The most important thing you'll learn is that in assembly, actions that take 1 line in BASIC or other languages, can take long sequences of assembly code. This is because in assembly you do everything in really small, low-level steps. So what's the point? Because those really small, low-level steps are executed really fast. And if something is possible on a machine, you can do it in assembly. But you may not be able to do it (at all, regardless of speed) in higher languages like BASIC.

A suggestion if you want to jumpstart 1) : get a Sinclair ZX Spectrum emulator, and fire up a program called "The Complete Machine Code Tutor". Check out the reviews on WoS site in case you have doubts...

This may seem a weird suggestion, but installing a Spectrum emulator + program is relatively quick/easy, for the ZX Spectrum 2) is dead simple, and The Complete Machine Code Tutor is an extremely easy to follow, interactive way to learn Z80 coding. Probably the most comparable on the MSX is a good debugger that allows direct input of Z80 assembly. But sad to say, it doesn't come close. :P

You can run Spectrum emulator on PC in one window, with various docs/webpages with info in other window(s), and any knowledge you gain you can immediately apply on the MSX if you want to. No need to stick to the Spectrum program until you know everything. As for documentation: :-? datasheets / databooks for the various IC's inside the MSX (and the MSX Technical Databook) are the reference docs you want...

Van ARTRAG

Enlighted (6443)

afbeelding van ARTRAG

15-02-2010, 18:57

Some sugegstions

;color 15,1,1
		LD    HL,&HF3E9
		LD    (HL),15
		LD    HL,&HF3EA
		LD    (HL),4
		LD    HL,&HF3EB
		LD    (HL),0
		CALL  &H62

should better be

;color 15,1,1
		LD    HL,&HF3E9
		LD    (HL),15
		INC    HL
		LD    (HL),4
		INC    HL
		LD    (HL),0
		CALL  &H62

while

;let me type in screen2
		LD    HL,&HFCB0
		LD    (HL),1
		LD    HL,&HFCAF
		LD    (HL),1

should better be:

;let me type in screen2
		LD    A,1
		LD    (&HFCB0),A
		LD    (&HFCAF),A

The gain here is very small, but when you code real loops the choice of the addressing mode matters a lot

Van Vampier

Prophet (2352)

afbeelding van Vampier

15-02-2010, 19:57

^_^ didn't even think about that

Van ARTRAG

Enlighted (6443)

afbeelding van ARTRAG

16-02-2010, 00:39

well, you should know also that the vdp has an undocumented video mode (available on almost any msx1 and all msx=>2) where you have only one screen 2 tile bank for the 3 thirds of the video.

This allows you to use less vram and to spend much less time if you want to updae on fly tiles and colors

Van MOA

Champion (293)

afbeelding van MOA

16-02-2010, 17:18

Some sugegstions

<snip>

should better be

;color 15,1,1
		LD    HL,&HF3E9
		LD    (HL),15
		INC    HL
		LD    (HL),4
		INC    HL
		LD    (HL),0
		CALL  &H62

<snip>

If you're nitpicking about speed of initialization code... then this is even faster!

;color 15,1,1
		LD    HL,&HF3E9
		LD    (HL),15
		INC    L
		LD    (HL),4
		INC    L
		LD    (HL),0
		CALL  &H62

p/s: shouldn't it be "color 15,4,0"?

Van Edwin

Paragon (1182)

afbeelding van Edwin

16-02-2010, 20:03

If you're nitpicking about speed of initialization code... then this is even faster!

;color 15,1,1
		LD    HL,&HF3E9
		LD    (HL),15
		INC    L
		LD    (HL),4
		INC    L
		LD    (HL),0
		CALL  &H62

Nitpick:

;color 15,1,1
		LD    HL,15+256*4
		LD    (&HF3E9),HL
		XOR   A
		LD    (&HF3EB),A
		CALL  &H62

Tongue

Van Huey

Prophet (2654)

afbeelding van Huey

18-02-2010, 13:02

So erh... How's it going Vampier? any progress?

Your post triggered me to start too (sjasm + z80 assembly language programming (Lance A. Leventhal 1979)).
The F register in combination with the JR is hard to get used to for me (I probably need practice). But the other stuff seems much simpler than I thought.

Van norakomi

Paragon (1084)

afbeelding van norakomi

18-02-2010, 15:55

well, you should know also that the vdp has an undocumented video mode (available on almost any msx1 and all msx=>2) where you have only one screen 2 tile bank for the 3 thirds of the video.

This allows you to use less vram and to spend much less time if you want to updae on fly tiles and colorsi suppose this counts for screen 4 too ?
Could you tell me some more about this ?

Pagina 1/21
| 2 | 3 | 4 | 5 | 6