GB Programming: Difference between revisions

Jump to navigation Jump to search
Content added Content deleted
m (Text replacement - "(hl)" to "[hl]")
m (Replaced parentheses with brackets)
Line 123: Line 123:
|DE
|DE
|HL
|HL
|(BC)
|[BC]
|(DE)
|[DE]
|(HL)
|[HL]
|(imm16)
|[imm16]
|-
|-
|A
|A
Line 288: Line 288:
|Yes
|Yes
|-
|-
|(BC)
|[BC]
|Yes
|Yes
|No
|No
Line 304: Line 304:
|No
|No
|-
|-
|(DE)
|[DE]
|Yes
|Yes
|No
|No
Line 320: Line 320:
|No
|No
|-
|-
|(HL)
|[HL]
|Yes
|Yes
|Yes
|Yes
Line 336: Line 336:
|No
|No
|-
|-
|(imm16)
|[imm16]
|Yes
|Yes
|No
|No
Line 444: Line 444:
So, how does running a program works? What happens is that a special register is incremented (its value is raised by one), then the processor fetches the byte located at the address held by that register, and processes it as an opcode ; when done, everything is repeated. Instructions can be one to three opcodes (bytes) large, so this cycle may repeat for a single instruction.
So, how does running a program works? What happens is that a special register is incremented (its value is raised by one), then the processor fetches the byte located at the address held by that register, and processes it as an opcode ; when done, everything is repeated. Instructions can be one to three opcodes (bytes) large, so this cycle may repeat for a single instruction.


So now, how to access memory? With parentheses! To access memory address $CD38, you just have to use ($CD38). Yay!
So now, how to access memory? With brackets! To access memory address $CD38, you just have to use [$CD38]. Yay!


To access the memory location pointed to by HL, just do... [hl]! It's the same with BC and DE.
To access the memory location pointed to by HL, just do... [hl]! It's the same with BC and DE.
Line 454: Line 454:
Remember to refer to the chart above for the legal LD combinations.
Remember to refer to the chart above for the legal LD combinations.


Obviously, ''ld [$6511], a'' will overwrite the previous value stored here. But ''ld [$6511], hl'' will store a 16-bit value, which is a word long, that is two bytes long! So, not only will ($6511) be overwritten, but ($6512) too! Always be very careful about the memory you're touching. Otherwise, stuff like the [[ZZAZZ glitch]] happen.
Obviously, ''ld [$6511], a'' will overwrite the previous value stored here. But ''ld [$6511], hl'' will store a 16-bit value, which is a word long, that is two bytes long! So, not only will [$6511] be overwritten, but [$6512] too! Always be very careful about the memory you're touching. Otherwise, stuff like the [[ZZAZZ glitch]] happen.


For those wondering, ''ld a, [$6511]'' leaves ($6511) untouched.
For those wondering, ''ld a, [$6511]'' leaves [$6511] untouched.


==Flags==
==Flags==
Line 558: Line 558:
Because two hex digits mean one byte, $D3, as well as $61, is a byte. Since $D3 and H are leftmost in both cases, ld hl, $D361 is actually a shorter form of ld h, $D3 then ld l, $61.
Because two hex digits mean one byte, $D3, as well as $61, is a byte. Since $D3 and H are leftmost in both cases, ld hl, $D361 is actually a shorter form of ld h, $D3 then ld l, $61.


Let's say the following instruction is ld [$2315], hl. Applying the same logic would mean H's value would be stored at ($2315), and L's would be at ($2316). However, you just lost THE GAME; because the z80 is a "little-endian" processor, L's value (the "little-end") is stored first, at ($2315). So ($2315) is $61, and ($2316) is $D3.
Let's say the following instruction is ld [$2315], hl. Applying the same logic would mean H's value would be stored at [$2315], and L's would be at [$2316]. However, you just lost THE GAME; because the z80 is a "little-endian" processor, L's value (the "little-end") is stored first, at [$2315]. So [$2315] is $61, and [$2316] is $D3.


Stop here, and remember this until it becomes natural to you. Because this "little-endian"ness is very tricky for beginners. It is ''very'' important when working with memory.
Stop here, and remember this until it becomes natural to you. Because this "little-endian"ness is very tricky for beginners. It is ''very'' important when working with memory.


Here is an exercise : what values will ($C000) to ($C00F) contain after this code is ran?
Here is an exercise : what values will [$C000] to [$C00F] contain after this code is ran?


Initial values :
Initial values :
Line 673: Line 673:
where reg16 is any 16-bit register pair. AF can be used here.
where reg16 is any 16-bit register pair. AF can be used here.


Also meet SP, which makes all of this possible. SP is the '''hardware Stack Pointer'''. You can INC and DEC it, and you can't use it as a source in LD. Here are equivalents of ''push hl'' and ''pop hl'' (assuming we could use (sp), 'cause we can't :3)
Also meet SP, which makes all of this possible. SP is the '''hardware Stack Pointer'''. You can INC and DEC it, and you can't use it as a source in LD. Here are equivalents of ''push hl'' and ''pop hl'' (assuming we could use [sp], 'cause we can't :3)
{| class="wikitable"
{| class="wikitable"
|PUSH HL
|PUSH HL
Line 1,064: Line 1,064:
sbc hl, bc ; HL = HL - (BC + C flag) = $C303 - ($0300 + $00) = $C003
sbc hl, bc ; HL = HL - (BC + C flag) = $C303 - ($0300 + $00) = $C003


ld [hl], a ; (HL) = ($C003) <- A = $06
ld [hl], a ; [HL] = [$C003] <- A = $06


inc hl ; HL = $C004
inc hl ; HL = $C004


ld b, [hl] ; B = (HL) = ($C004) = $DE
ld b, [hl] ; B = [HL] = [$C004] = $DE


sub a, b ; A = A - B = $06 - $DE = $06 + (-$DE) = $06 + ($21 + $01) = $28, C flag = 0
sub a, b ; A = A - B = $06 - $DE = $06 + (-$DE) = $06 + ($21 + $01) = $28, C flag = 0
Line 1,074: Line 1,074:
Notice here that doing ''sub a, b'' actually increased A's value!
Notice here that doing ''sub a, b'' actually increased A's value!


inc [hl] ; (HL) = ($C004) = $DF
inc [hl] ; [HL] = [$C004] = $DF


inc hl ; HL = $C005
inc hl ; HL = $C005


ld [hl], b ; (HL) = B = $DE
ld [hl], b ; [HL] = B = $DE


ld bc, 9 ; B = $00, C = $09
ld bc, 9 ; B = $00, C = $09
Line 1,084: Line 1,084:
add hl, bc ; HL = HL + BC = $C005 + $0009 = $C00E
add hl, bc ; HL = HL + BC = $C005 + $0009 = $C00E


ld [hl], a ; (HL) = ($C00E) = A = $28
ld [hl], a ; [HL] = [$C00E] = A = $28


ld [$C00B], hl ; ($C00B) = L = $0E, and ($C00C) = H = $C0
ld [$C00B], hl ; [$C00B] = L = $0E, and [$C00C] = H = $C0


Initial values :
Initial values :