Variables in memory and constants

This program keeps a price and a shipping cost in memory, adds a tax, and stores the total back in memory. The answer is 305, which needs two bytes rather than one.

TAX equ 20 is an assembler constant. Before it builds the program, the assembler replaces each use of TAX with the number 20. It reserves no memory and has no address; its name simply makes the number's purpose clear.

TAX equ 20

    .org 0x8000
    ld hl, (price)      ; total = price
    ld de, (shipping)
    add hl, de          ; total = total + shipping
    ld de, TAX
    add hl, de          ; total = total + TAX
    ld (total), hl      ; write the answer back into memory
    halt

    .org 0x9000
price:      .dw 250
shipping:   .dw 35
total:      .ds 2

A word is a two-byte value. Here price, shipping, and total are words, so .dw puts the first two into memory and the register pairs hl and de hold them while add hl, de adds them. ld de, (shipping) reads the word stored at the address named shipping. Later, ld de, TAX loads the number 20 directly into de.

Open the program in the editor, choose Build, then Run. In the memory panel, enter 9000 as the address. The registers panel uses hexadecimal, so the final hl value is 0131 (305).

labeladdressbytes before Runbytes after Run
price0x9000FA 00FA 00
shipping0x900223 0023 00
total0x900400 0031 01

The editor starts unused memory cleared, so total appears as 00 00 before the run. .ds 2 reserves those two bytes; it does not write the zeroes. After the run, total contains 0x0131. Words are little endian: the low byte comes first in memory, so 250 (0x00FA) appears as FA 00, and 305 appears as 31 01.

Try changing shipping: .dw 35 to shipping: .dw 40. Before you build and run, predict the six bytes beginning at 9000.

Check your answer

The total is then 310 (0x0136), so expect FA 00 28 00 36 01.