Learn Z80 assembly Lesson 2 - LDIR, Labels, Definitions, Conditions,Loops

แชร์
ฝัง
  • เผยแพร่เมื่อ 4 ต.ค. 2024
  • This is Lesson 2 of my Z80 tutorials which will help you learn to develop for the Amstrad CPC, ZX Spectrum & MSX
    Although this is the second of my lessons, its very simple, so you'll have no problem following it if you've not seen Lesson 1 first
    This is the video companion to the text based Lesson 2 on my website:
    www.chibiakumas...
    The commented sourcecode of this lesson, and my Z80 Assembly cheatsheet are freely available from the same site.
    My Books cover most ASM languages and are available on Amazon stores worldwide! Now in Hardback and Large A4 size!
    Check them out here: amzn.to/3hgETuM
    My 1st book covers ASM programming on Z80, 6502, 68000, 8086 and ARM
    My 2nd book book covers ARM Thumb, 65816, 6809, PDP-11 and Risc-V CPUs
    The Large print versions can be a little hard to find... the links are here:
    Vol 1 A4: amzn.to/3T8WzFG
    Vol 2 A4: amzn.to/3WC9jaM
    If you like my content, and want to see more in the future, please support me on patreon:
    / akuyou
    You can also support my content by buying branded merchandise from my Teespring store:
    teespring.com/...
    Discuss on the Chibi Akumas forums:
    www.chibiakumas...

ความคิดเห็น • 44

  • @TerranVisitor
    @TerranVisitor 4 ปีที่แล้ว +3

    Well done video! You mentioned the difficulty in conceptualizing the JUMP instruction. The way I do it is to remember that a Compare instruction is actually a Subtract ... The compare instruction on all processors I have been exposed to is actually performed in the ALU circuitry as a subtraction - and affects the flags as a subtraction would. i.e. The only difference between the machine instructions CMP and SUB is that CMP does not update a register with the subtraction results - they both update the basic flags (Z and C) the same.

  • @TrevorKevorson
    @TrevorKevorson 6 ปีที่แล้ว +14

    Great videos Keith. I've been trying to learn Z80 assembler for about 30 odd years since I had my original CPC 464. While playing around with values in the program I've managed to make some funky effects on the screen. I think I understand how loading screens loaded with a line at a time (I managed to get WinAPE to draw lines of garbage on the screen in mode 0 and it seemed to draw one line then skip a few, then draw another, then skip a few, and each time it went from the bottom of the screen to the top it'd go one line further up :-)
    I've also worked out how to save the binary code to a file and create a basic loader on a tape file to load a Basic program, then the binary code and then run it automatically :-D.
    I think this is possibly the first Z80 tutorial I've been able to follow easily. The fact that you have written instructions and videos to go with it is great :-)

  • @PlanetToborTV
    @PlanetToborTV 3 ปีที่แล้ว +2

    These are incredible. I am so glad I found this channel. Thank you!

  • @FizzyMcSoda
    @FizzyMcSoda 4 ปีที่แล้ว +2

    As someone who has their sights on creating ColecoVision games, these tutorials are really helping out, thanks a bunch @ChibiAkumas!

  • @shankar4510
    @shankar4510 4 ปีที่แล้ว +1

    even though its little hard to undeestand i want to like this video. i will watch this multiple times .

  • @RetroHoo
    @RetroHoo 5 ปีที่แล้ว +2

    Exactly what I was looking for! Most certainly a wealth of content and information. Thanks a lot!

    • @RetroHoo
      @RetroHoo 5 ปีที่แล้ว

      They are! Funny when I owned a CPC as a kid I always wanted to be able to code asm, but never managed. Now I was searching around for an intro into making my home brew z80 project do a couple of simple things (other than nops) and I’m back full circle to the CPC.

  • @PauloDias-nm2eu
    @PauloDias-nm2eu 6 ปีที่แล้ว +4

    Thx Keith. Keep up the good work. thx for this beginner lessons

  • @jakubfikoek2298
    @jakubfikoek2298 6 ปีที่แล้ว +1

    Hello Keith! Great tutorial series:) I've started learning Z80 assembly for a couple of weeks ago and find it really interesting. Hope to see more videos on your channel!

  • @ChibiAkumas
    @ChibiAkumas  3 ปีที่แล้ว +2

    My Books cover most ASM languages and are available on Amazon stores worldwide! Now in Hardback and Large A4 size!
    Check them out here: amzn.to/3hgETuM
    My 1st book covers ASM programming on Z80, 6502, 68000, 8086 and ARM
    My 2nd book book covers ARM Thumb, 65816, 6809, PDP-11 and Risc-V CPUs
    The Large print versions can be a little hard to find... the links are here:
    Vol 1 A4: amzn.to/3T8WzFG
    Vol 2 A4: amzn.to/3WC9jaM

  • @UndevXIII
    @UndevXIII 6 ปีที่แล้ว +4

    Thank you. This is very helpful.

  • @syntax_error6882
    @syntax_error6882 6 ปีที่แล้ว +2

    amazing work thank to share

  • @KENNETHUDUT
    @KENNETHUDUT 4 ปีที่แล้ว

    Now I understand Block Transfer Computations. Logopolis.

  • @IExSet
    @IExSet 4 ปีที่แล้ว +1

    Awesome series!

  • @charliecharliewhiskey9403
    @charliecharliewhiskey9403 3 ปีที่แล้ว

    *Edit:* _Tested it myself and it does work._
    If I *really* wanted to compare to 0 and use z (rather than using nz) would I need to do something like this:
    (P1 = Pass 1, P2 = Pass 2) (Assume A has 2 before start)
    FLabel:
    dec a P1: A becomes 1 - P2: A becomes 0
    (Fill mem) P1: fill with 1 - P2: fill with 0
    cp 0 P1: Z not set - P2: Z is set
    jp z,EndLabel P1: Doesn't use - P2: Jumps to EndLabel
    jp FLabel P1: Jump to FLabel - P2: Is skipped
    EndLabel: P1: Never gets here - P2: Gets here
    ret
    ? I mean sure it's less eloquent than using nz as per your lesson, but it would be functional right?
    Definitely smarter to use NZ for this purpose like you say, I just want to know I have the right understanding of how these can be used.
    *Edit:* Gonna put the code here again without the pass comments, just in case reader is on mobile and it gets crunched:
    FLabel:
    dec a
    (Fill mem code)
    cp 0
    jp z,EndLabel
    jp FLabel
    EndLabel:
    ret

    • @charliecharliewhiskey9403
      @charliecharliewhiskey9403 3 ปีที่แล้ว

      I just tested it and it does indeed work. Even so, I'm not going to use it like this, because needlessly having an extra label and two jump instructions right next to each other adds needless complexity to the code, which I imagine will make it harder to understand if I ever make anything big-ish. Always better to go with simpler code where possible if it has the same effect, I think?
      Exact code I used to test this (breakpoint and Accumulator-watching to confirm, see see count down to 0 then see a 7 means it worked):
      org &8000
      ld a,5
      FLabel:
      dec a
      cp 0
      jp z,EndLabel
      jp FLabel
      EndLabel:
      ld a,7
      ret

  • @ImperatorGrausam
    @ImperatorGrausam 3 ปีที่แล้ว

    Hey Keith, I have a bit of a question about the byte-count. Why did you particularly minus the screensize symbol by one? What does it do?
    Edit: nevermind, I think it's just because you wanted an example of how hex works by showing that it's not exactly like regular math. Also, I realized I asked you this twice. Last time was 2 years ago. It seems your response was deleted. I apologize to ask again.
    Edit to the Edit: Is the Discord server still up? I was in it I guess I left. I can't find the link to it now. Tried making an account on your forum, but I always seem to forget I made one because it requires moderation approval, so I give up, come back, forget I did, and try making another one. Apologies if it seemed like spam. Not to mention I live in Dubai, so I can see why some dude trying to make accounts again and again from a Middle Eastern country may seem like spam.

  • @babute21
    @babute21 6 ปีที่แล้ว +1

    thanks man

  • @vegardt3433
    @vegardt3433 3 ปีที่แล้ว +1

    I've tested the code a bit, and get a weird effect that I can't explain.
    Normally, when I do "call &0000" the computer does a hard reset and restarts. Now, after changing the code in your example from "ld bc,screensize-1" to "ld bc,screensize" and running the program once, calling &0000 does not restart the computer, but it repeats the program. I get that it overwrites the first low bytes of memory, and thus has to have an effect on the program? But why does it have this effect on the call command? The only thing that it does is change the first byte from &01 to &0f. I would have assumed that the computer would crash, but it doesn't really do that. It only changes behavior.

    • @ChibiAkumas
      @ChibiAkumas  3 ปีที่แล้ว +1

      sounds like the bytes written to &0000 are causing some kind of jump. To be honest, when stuff gets corrupted, anything can happen!
      If you really want to figure it out, put a breakpoint at memory address &0000 and do it again, then step through and see what the CPU does!

    • @vegardt3433
      @vegardt3433 3 ปีที่แล้ว

      @@ChibiAkumas Yeah. I did that, and I think I have figured out parts of it at least. The byte pattern &0f is the command RRCA, or rotate A to the right and carry. So it does change A. And then the other two commands that "split off" of the original 3 byte command ( LD BC, &7F89 ), are &89 -> ADC C, and &7f -> LD A,A, adding C to C and then moving A to A. The next two commands do not change, and are OUT (C),C and JP &0591. So it doesn't add a jump, but it removes a command giving C a value of 7F (i think, or is it 89?). The rest seems to change nothing of substance. ADD C,C does nothing, as C is already 0 at that point, and LD A,A also is equivalent to NOP. So then I guess I need to examine address &0591. Maybe it's a restart routine for the last program run? I can look into that myself. And apparently the value of BC has something to do with it also.
      By the way. I'm just running the ParaDos V1.2+ with basic 1.1, as the WinAPE starts with out of the box. I guess the firmware might change with different versions and setups.
      And sorry for the long response. I just love breaking things. It usually gives you quite a bit of useful information on how a system works. Thanks for the great videos!

  • @randallanderson4999
    @randallanderson4999 3 ปีที่แล้ว

    Went to your web page to get the Z80 Instruction sheet you mention, but it is not available. Do you have another page where it is?
    Ok, I found the link in another comment down below.

    • @ChibiAkumas
      @ChibiAkumas  3 ปีที่แล้ว

      Sorry! I fixed the link now, thanks for letting me know

  • @miles7267
    @miles7267 4 ปีที่แล้ว

    Thank you for the tutorial! I’m trying to write programs for the TI-84 plus and this is extremely helpful. For some reason I am unable to find the PDF on your website, any help on finding it would be much appreciated.

    • @ChibiAkumas
      @ChibiAkumas  4 ปีที่แล้ว

      You mean the cheatsheet? It's here:
      www.chibiakumas.com/z80/CheatSheet.pdf

  • @3lpid3
    @3lpid3 4 ปีที่แล้ว

    Does anybody know if there is a way to redefine the four default pen colours in MODE 1 from red, dark blue, yellow and light blue?

  • @JuanLuisRetro
    @JuanLuisRetro 6 ปีที่แล้ว

    Hi Keith! I have a question for you. Is there any alternative method of clearing screen in Amstrad? I developed a similar program with LDIR instruction and executed 100 times recording times. Later, I did same with MODE (0, 1, 2) and CLS getting better times. I dissasembled MODE and CLS routines and I could see Amstrad Basic use a different way of clearing screen, but I was a little bit confused because these routines use I/O ports that I don't know.

    • @JuanLuisRetro
      @JuanLuisRetro 6 ปีที่แล้ว

      Thank you for your answer. I'm going to test it.

  • @jmooree30
    @jmooree30 5 ปีที่แล้ว

    Hi it's me again with a new question.
    When it comes to using the LD(load) command, how do you decipher whether you are loading a number, or a memory address?
    lD A, 4 ; Loads the number 4 into the accumulator(A)
    LD HL, &C000 ; Loads the memory address of the screen into HL
    &C000 converted to decimal is 49152. How does it know this is a memory address and not just a number like the one we passed to the accumulator?

    • @jmooree30
      @jmooree30 5 ปีที่แล้ว

      So I might be wrong here but I'm going to guess that the accumulator always expects a number while HL is used for addressing and expects a memory address to point to.

    • @ChibiAkumas
      @ChibiAkumas  5 ปีที่แล้ว +2

      in your example they are both just numbers...
      LD HL,&C000 loads the number &C000
      but
      LD HL,(&C000) loads HL from the address &C000 because of the brackets...
      in your example Whether &C000 is a number or address just depends how you use it later

    • @jmooree30
      @jmooree30 5 ปีที่แล้ว +2

      @@ChibiAkumas All starting to click now. You are an invaluable mountain of information. Thank you, on to lesson 3.

  • @proxy1035
    @proxy1035 5 ปีที่แล้ว

    now it has been months since i "started" with assemblers and everything, and there is one thing that confuses me about WinApe's Assembler
    how do you make local labels without using macros? usually all Assemblers should support local labels, and it would be annoying for every function to have uniquely labeled... labels. like loop, exit, etc
    overall the WinApe assembler isn't the best thing out there. mostly because i'm used to the modern hex prefix "0x" and th TABs are a bit over the top. then again i can just use Notepad++ to write and Winape to assemble

    • @ChibiAkumas
      @ChibiAkumas  5 ปีที่แล้ว

      I'm afraid I don't think Winape supports local Labels. Winape was based on the old CPC assembler MAXAM, so I guess that didn't support them either.
      To be honest, I agree that Winape isn't the 'Best' assembler, but it's the fact it can compile straight to ram, and the built in debugger than makes Winape so easy for me to use for programming.

  • @ImperatorGrausam
    @ImperatorGrausam 5 ปีที่แล้ว

    So what LDIR did was, it copied memory addresses $0000-4000 to $C000 which is the start of the CPC screen?

    • @ImperatorGrausam
      @ImperatorGrausam 5 ปีที่แล้ว

      @@ChibiAkumas Thanks.

    • @ImperatorGrausam
      @ImperatorGrausam 5 ปีที่แล้ว

      I have another question: Why exactly did you set bc to &3FFF and not the size of the CPC screen? Also, what does ld (hl),0 do?

  • @proxy1035
    @proxy1035 5 ปีที่แล้ว +1

    question, why is the OS WinXP in some Asian language... that seems rather odd
    also i know it's not case sensetive but seeing Assembly in non Uppercase only seems strange.

    • @proxy1035
      @proxy1035 5 ปีที่แล้ว

      @@ChibiAkumas nono it's not bad or anything i was just confused by it.
      it's your PC you can do what you want with it :p
      and the design is exactly like XP's so there was no way i could've known it's actually Win7.

  • @georgysb
    @georgysb 4 ปีที่แล้ว

    I knew Japanese don't have sound "L" in their language so they spell "R" instead of it. But this is the first time I hear a Japanese spelling "R" as "L" =DD
    P.S. You could avoid all that struggling with remembering the Flags effects on CP command, if you paid just a little bit more attention to Z80 datasheet and thus realized what CP command means in terms of CPU actions. And CPU literally performs subtraction (A - number). So basing on this knowledge the Flags effects become totally obvious and there is nothing to remember at all.

  • @blackimp4987
    @blackimp4987 ปีที่แล้ว

    you did an incredible job with your video and we all should be grateful but let me tell you, if you want your video to be useful to someone, you should be less anxious of telling EVERYTHING at once. for instance: you were explaining Ldir command... after the first two operand you started talking about the other platforms using z80 and then forgot to explain what 's the third operand is for. I'm watching your newer videos but it's more or less the same, you speak very fast, tell many interesting thing but code is overlapped, or it's hard to read the code while watching the execution etc. if video requires having some text from your website it's better telling in advance and base the video on that. otherwise if you count on the material on your website, you run fast in your video, and you don't refer explicitly to something you wrote elsewhere it's not that useful watching the videos. it's very chaotic doing examples basing on flags, comaprations that act like subtractions... and none of these things have been explained before. I mean I don't complain, you offer informations for free but I sincerly can't find all these efforts didactically useful.

    • @ChibiAkumas
      @ChibiAkumas  ปีที่แล้ว

      There's pretty much nothing useful I can gain from your comment.
      TH-cam pays me literally $12 a month, so I can't put any more work into the videos, Really they should be scripted and heavily edited, but that would be extremely costly in time.
      Basically, I'm just doing the best I can as quickly as I can in the hope of gaining enough of an audience to do something better... well I was, but it's been 4 years, so clearly it was a waste of time - so now I'm just muddling on best I can.
      As you said, really you'd be better just going to the website and downloading the sources etc... but it's 2023, and people don't visit websites any more, Google hardly indexes them, so you have to make video content for anyone to know you exist.

    • @blackimp4987
      @blackimp4987 ปีที่แล้ว

      @@ChibiAkumas if you need to get some money from your video and you like to teach assembly or whatever do it in a useful way. either you just make video where you implement programs, examples, maybe something interesting for exploiting graphics basing on theory one must learn before, maybe through your websites, OR you make didactic videos and if you want to be didactic you have to be methodic and schematic, not trying to put everything that jumps on your mind in a single video confusing watchers' mind. People that never programmed in assembly must learn code and the best or common procedure to set up a program, how the registers work, how to addressing memory, where is the code compared to where starts the stack and so on. Probably, if you just need to make some money, you would get more views in the first case, taking for granted the code and starting theory and showing some small examples doing "cool" things. id that's your objective. If it's teaching assembly you jave to be much more patient. this is my personal feedback. I'm personally studying assembly thou I studied another didactic assemly at university; i like what you do but I stoped watchin even the more recent series on z80 because it's not usable for learning. I had to go back to another "boring" series with every video a group of commands, one by one with examples, variation and so on. It has very few views thou

    • @ChibiAkumas
      @ChibiAkumas  ปีที่แล้ว

      I'm not even bothering to read your whining, you could at least put a couple of page breaks in there to make it readable, but I'm sure your comments are 'totally valid'.
      My patreons pay me to make content - they are happy with it else they wouldn't pay me to make it
      You do not pay me - nor will you ever - your opinion is worthless to me - For all I know you're a rival, jerk, AI or trolling kid. If I pay attention to every rando online having a whine I'll have given up years ago.
      Don't turn up to a free barbecue and whine about the culinary skills of the chef - maybe you should think about donating some to the food bill first.
      You got something for nothing, and your still not happy.