Anonymous user
GB Programming: Difference between revisions
m
Text replacement - "(\bld(?:|i|l|d|h) (?:.+, ?)?)\((.+)\)" to "$1[$2]"
>44HyXpert |
m (Text replacement - "(\bld(?:|i|l|d|h) (?:.+, ?)?)\((.+)\)" to "$1[$2]") |
||
Line 396:
|Store value of register B into register D.
|-
|''ld
|Store the value of register A into memory address $8325.
|}
Line 448:
To access the memory location pointed to by HL, just do... (hl)! It's the same with BC and DE.
So, to retrieve the value at memory address $6511 into register A : ''ld a,
And to store the value of register C into the memory pointed to by HL : ''ld
Remember to refer to the chart above for the legal LD combinations.
Obviously, ''ld
For those wondering, ''ld a,
==Flags==
Line 525:
A : Nowhere :D To multiply, you must write your own routines! However, a nice lil' trick : to do A <- A*2, simply ''add a, a''! To do A <- A*3, do ''ld b, a'', ''add a, a'', ''add a, b'' (you can swap B with any other register, of course). I'll leave you A <- A*4, A*5, A*6 and A*7 as an exercise.
For the rest of the tutorial, you'll see some text prefixed by a ";". These are comments, and are NOT part of the code. This line : "ld
Also, the Game Boy's CPU as four very specific instructions :
{| class="wikitable"
|ld
|Equivalent to ''ld
|-
|ld
|Equivalent to ''ld
|-
|ld a,
|Equivalent to ''ld a,
|-
|ld a,
|Equivalent to ''ld a,
|}
These are often used to operate on
===Overflow===
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.
Let's say the following instruction is ld
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.
Line 573:
<pre>
ld hl, $C303
ld a,
ld b, 3
add a, b
ld c, 0
sbc hl, bc
ld
inc hl
ld b,
sub a, b
inc (hl)
inc hl
ld
ld bc, 9
add hl, bc
ld
ld
</pre>
Line 633:
To push register DE :
<pre>
ld hl,
ld
inc hl ; Move stack pointer
ld
inc hl
ld
</pre>
To pop into register DE :
<pre>
ld hl,
dec hl ; Move stack pointer
ld d,
dec hl ; Repeat
ld e,
ld
</pre>
Line 678:
|<pre>
dec sp
ld
dec sp
ld
</pre>
|-
|POP HL
|<pre>
ld l,
inc sp
ld h,
inc sp
</pre>
Line 696:
<pre>
push af
ld a,
pop de
</pre>
Line 810:
Example :
<pre>
ld a,
cp $63
jr z, placeItems
Line 817:
jr someplace
placeItems:
ld b,
</pre>
If (hl) equals $63, execution jumps to placeItems.
Line 1,054:
ld hl, $C303 ; Now H = $C3 and L = $03
ld a,
ld b, 3 ; B = $03
Line 1,064:
sbc hl, bc ; HL = HL - (BC + C flag) = $C303 - ($0300 + $00) = $C003
ld
inc hl ; HL = $C004
ld b,
sub a, b ; A = A - B = $06 - $DE = $06 + (-$DE) = $06 + ($21 + $01) = $28, C flag = 0
Line 1,078:
inc hl ; HL = $C005
ld
ld bc, 9 ; B = $00, C = $09
Line 1,084:
add hl, bc ; HL = HL + BC = $C005 + $0009 = $C00E
ld
ld
Initial values :
|