Chapter 4.2: Variables

Microprocessors
DB / DW / DDNaming rulesMemory layoutORG 100h and .COMArrays and DUPLEA and OFFSETEQU constantsAdding and comparingDoublewords
Sercan KÜLCÜ · Giresun University · Computer Engineering

Introduction

Learning Outcomes

Definitions

Define variables and arrays with DB, DW, DD, DUP and ?.

Memory layout

Work out the offsets and byte order the assembler gives each variable.

Addresses

Get a variable’s address with LEA and OFFSET and index into arrays.

Programs

Trace programs that move, add and compare data between variables.

Variables

Variables

  • A variable stands for a memory location.
  • For a programmer the name var1 is far easier to use than the address 5A73:235B.
  • The assembler supports two basic types:
    • BYTE (8 bits): name DB value (Define Byte)
    • WORD (16 bits): name DW value (Define Word)
  • There is also DD (Define Doubleword, 32 bits): 4 bytes.
counter DB 10h     ; BYTE
sum     DW 0FFFFh  ; WORD
counter10sumFFsum+1FF…1 byte2 bytes

Variables

Names and Initial Values

  • A variable name must start with a letter; letters and digits may follow.
  • Register and instruction names (AX, MOV…) cannot be variable names.
  • A variable may have no name: it still has an address.
  • It can hold any numeric value (binary, decimal, hex, character).
  • Use ? for a variable with no initial value (emu8086 stores 00).
Definition?
num1 DB 5valid
1num DB 5starts with a digit
ax DW 0register name
DB 0Ahno name, but has an address
result DW ?no initial value
letter DB 'A'41h

Variables

Value Ranges

DirectiveSizeUnsignedSignedExample
DB1 byte0 … 255−128 … 127x DB -1 → FFh
DW2 bytes0 … 65535−32768 … 32767y DW -2 → FE FF
DD4 bytes0 … 4,294,967,295−231 … 231−1z DD 1 → 01 00 00 00
Memory stores only the bit pattern; signed or unsigned depends on the instruction that uses the value (JGE compares signed, JAE unsigned). DB 300 does not fit in a byte → the assembler reports an error.

Variables · Step through the code

Code Trace: Is FFh 255 or −1?

ORG 100h
MOV AL, x        ; FFh
ADD AL, 1        ; 255 + 1 ?
MOV AL, x
CBW              ; signed: −1
MOV BX, y        ; FFFEh
NEG BX           ; −(−2)
RET
x DB -1
y DW -2
memoryFFx · 0110FEy · 0111FFy+1 · 011200FFhAXunsigned 255signed 255AL: 255 / −10000hBXunsigned 0signed 0CF0OF0ZF0

In memory x = FFh, y = FE FF (FFFEh, little-endian). AL = x = FFh: 255 unsigned, −1 signed.

memoryFFx · 0110FEy · 0111FFy+1 · 01120000hAXunsigned 0signed 0AL: 0 / 00000hBXunsigned 0signed 0CF1OF0ZF1unsigned overflow: CF

ADD AL, 1: unsigned, 255 + 1 = 256 does not fit in a byte → AL = 00h, CF = 1. Signed, −1 + 1 = 0 is correct: OF = 0.

memoryFFx · 0110FEy · 0111FFy+1 · 011200FFhAXunsigned 255signed 255AL: 255 / −10000hBXunsigned 0signed 0CF1OF0ZF1

AL is FFh again.

memoryFFx · 0110FEy · 0111FFy+1 · 0112FFFFhAXunsigned 65535signed −1AL: 255 / −10000hBXunsigned 0signed 0CF1OF0ZF1sign extension

CBW treats AL as signed: MSB = 1 → AH = FFh, AX = FFFFh = −1 (16 bits).

memoryFFx · 0110FEy · 0111FFy+1 · 0112FFFFhAXunsigned 65535signed −1AL: 255 / −1FFFEhBXunsigned 65534signed −2CF1OF0ZF1

BX = y = FFFEh: 65534 unsigned, −2 signed.

memoryFFx · 0110FEy · 0111FFy+1 · 0112FFFFhAXunsigned 65535signed −1AL: 255 / −10002hBXunsigned 2signed 2CF1OF0ZF0NEG: −(−2) = 2

NEG BX: 0 − (−2) = 0002h. The instruction chose the meaning: ADD/CF read the pattern as unsigned, CBW/NEG as signed.

DB/DW store only the bit pattern; the instruction that uses the value decides whether it is signed or unsigned.

Variables

Variable Type and Size Checking

  • The assembler keeps a type with every variable name: DB → BYTE (1), DW → WORD (2), DD → DWORD (4).
  • If the other operand is a register, the sizes must match; otherwise assembly stops.
  • With an immediate, the variable’s type sets the size: MOV w, 5 writes a word.
  • Operands like [BX] have no type; with an immediate they need BYTE PTR or WORD PTR.
  • BYTE PTR changes the type for that instruction only; the definition stays the same.
Instruction (w DW 1234h, b DB 5)Result
MOV AL, werror: 1 byte / 2 bytes
MOV AL, BYTE PTR wAL = 34h (low byte)
MOV AL, BYTE PTR w+1AL = 12h (high byte)
MOV w, 5w = 0005h (word)
MOV AX, berror: 2 bytes / 1 byte
MOV [BX], 5error: size unknown
MOV b, werror: memory to memory
INC bb = 6 (byte increment)
Type checking happens at assembly time and catches many easy-to-miss errors. The processor knows no types at run time; it reads or writes the size the instruction encodes.

Variables · Step through the code

Code Trace: Two Names for the Same Memory (LABEL)

ORG 100h
MOV AX, wval       ; WORD
MOV BL, bytes      ; low
MOV BH, bytes+1    ; high
MOV bytes, 0FFh    ; one byte
MOV CX, wval       ; word
RET
wval LABEL WORD
bytes DB 34h, 12h
memory34bytes011512bytes+10116wval (WORD) = 1234hAX1234hBX0000hCX0000hread as a WORD

wval LABEL WORD reserves nothing; it only gives address 0115h a second name of type WORD. MOV AX, wval reads both bytes: AX = 1234h.

memory34bytes011512bytes+10116wval (WORD) = 1234hAX1234hBX0034hCX0000h

bytes is the BYTE-typed name of the same address: BL = 34h. No PTR needed.

memory34bytes011512bytes+10116wval (WORD) = 1234hAX1234hBX1234hCX0000h

bytes+1 is the second byte: BH = 12h. BX = 1234h, thanks to little-endian order.

memoryFFbytes011512bytes+10116wval (WORD) = 12FFhAX1234hBX1234hCX0000hone byte written

The type is BYTE, so MOV bytes, 0FFh writes only address 0115h: memory is FF 12.

memoryFFbytes011512bytes+10116wval (WORD) = 12FFhAX1234hBX1234hCX12FFh

Read through the WORD name, the change shows: CX = 12FFh. Two names, one memory location.

memoryFFbytes011512bytes+10116wval (WORD) = 12FFhAX1234hBX1234hCX12FFh

RET. LABEL produced no bytes; the program grew only by the two-byte definition.

name LABEL type gives the next byte a new name and type. To process a buffer both byte by byte and word by word, it reads better than writing PTR everywhere.

Memory Layout

Example Code

ORG 100h
MOV AL, var1   ; AL ← var1
MOV BX, var2   ; BX ← var2
RET            ; end program
VAR1 DB 7      ; BYTE, 7
var2 DW 1234h  ; WORD, 1234h
  • Code first, variables after RET: the CPU never reaches them.
  • The assembler turns the source into a sequence of bytes and replaces variable names with memory locations.
  • It is case-insensitive: VAR1 and var1 are the same variable.
0100A001010801020101038B01041E0105090106010107C3010807010934010A12MOV AL, [0108h]MOV BX, [0109h]RETVAR1var2

Memory Layout · Step through the code

Code Trace: var1 and var2

AX0000AHALBX0000BHBLIP01000B56:0107 … 010A0107C3010807010934010A12

The program is loaded at 0B56:0100; DS = CS = 0B56h, IP = 0100h.

AX0007AHALBX0000BHBLIP01030B56:0107 … 010A0107C3010807010934010A12

MOV AL, [0108h]: the value of VAR1 → AL = 07h. IP = 0103h.

AX0007AHALBX1234BHBLIP01070B56:0107 … 010A0107C3010807010934010A12

MOV BX, [0109h]: 34h is the low byte, 12h the high byte → BX = 1234h. IP = 0107h.

AX0007AHALBX1234BHBLIP00000B56:0107 … 010A0107C3010807010934010A12

RET (C3 at 0107h) pops 0000h: IP = 0000h, and the INT 20h in the PSP ends the program. Everything from 0108h on is data.

ORG 100h
MOV AL, var1   ; AL ← var1
MOV BX, var2   ; BX ← var2
RET            ; end program
VAR1 DB 7      ; BYTE, 7
var2 DW 1234h  ; WORD, 1234h

Memory Layout

What Does the Assembler Do?

  • It turns the source into a sequence of bytes and replaces variable names with offsets: MOV AL, var1 → MOV AL, [0108h].
  • When a .COM file is loaded, DS gets the value of CS (so do ES and SS).
  • An offset is the distance from the start of the segment.
  • VAR1 is at offset 0108h; its full address is 0B56:0108.
  • var2 is at offset 0109h; its full address is 0B56:0109.
    • As a WORD it takes 2 bytes (0109h and 010Ah).
    • The low byte sits at the low address: 34h comes before 12h (little-endian).
The segment value (0B56h) depends on where DOS loads the program and may differ on every run; the offsets are fixed at assembly time.

Memory Layout

The ORG 100h Directive

  • Tells the assembler the address where the program will be loaded; it is used when names are replaced with addresses.
  • Used for .COM files; most examples in this course use the emu8086 .COM template.
  • The operating system keeps data about the program in the first 256 bytes of the segment (the PSP); the command-line arguments live there.
  • .EXE files load at offset 0000h and use a separate segment for variables.
  • Directives are not translated into machine code.
PSP: DOS data, command line (80h–FFh)0000hcode: ORG 100h, first instruction0100hvariables (after RET)freestack: grows down from SP = FFFEhFFFEhFFFFhCS = DS = ES = SSone segment, at most 64 KBIP = 0100h

Memory Layout

Alignment: Where Does a Word Start?

even address: 1 bus cycle340200120201odd address: 2 bus cycles··0200340201120202··020316-bit data buscarries two bytes at oncecycle 1: 0201 · cycle 2: 0202+4 clocks per word
a  DB 5          ; 0200h
   EVEN          ; 0201h: pad
w  DW 1234h      ; 0202h (even)
  • The 8086 data bus is 16 bits: a word at an even address is read in one bus cycle.
  • A word at an odd address needs two cycles; the 8086 handles it, only slower (4 clocks per access).
  • MASM/TASM's EVEN rounds the location counter up to even, adding a pad byte if needed. Definitions are only shifted, not reordered.
  • On the 8088 (8-bit bus) every word takes two cycles anyway; alignment doesn't matter there.
Example: MOV AX, w takes 10 clocks on the 8086, 14 if w is odd-addressed. Read 1000 times in a loop: 4000 clocks, 0.8 ms at 5 MHz. Bytes need no alignment.

Memory Layout

Where Do Variables Go?

PlaceExampleWatch out for
After the codeRET · x DB 5Execution ends at RET and never reaches the data. The most common layout for .COM.
Before the codeJMP start · x DB 5 · start:The JMP skips the data; forget it and the data bytes are executed as instructions.
Separate data segment (.EXE).DATA · x DB 5The program must begin with MOV AX, @data · MOV DS, AX.
If DS is not loadedWhen an .EXE starts, DS points to the PSP. MOV AL, x uses the right offset but the wrong segment: a wrong value comes back with no error.
Why the segment mattersIn a .COM file CS = DS, so code and data can share a segment. In an .EXE the code is in CS and the data in DS; variable addresses are relative to DS.

Arrays

Arrays

  • Think of an array as a chain of variables: a group of values stored in consecutive memory locations.
  • Quoted text is converted automatically into a byte array (ASCII codes).
  • Elements are reached through memory addresses: the first element is at the array’s address, the rest follow.
a DB 48h, 65h, 6Ch, 6Ch, 6Fh, 00h
b DB 'Hello', 0
…a[0]48a[1]65a[2]6Ca[3]6Ca[4]6Fa[5]00b[0]48b[1]65b[2]6C…

a and b hold the same bytes: 48h 'H', 65h 'e', 6Ch 'l', 6Ch 'l', 6Fh 'o', 00h (end of string).

Arrays · Step through the code

Code Trace: Accessing Array Elements

AX006CAHALBX0000BHBLSI0000array aa[0]48a[1]65a[2]6Ca[3]6Ca[4]6Fa[5]00

a[3] means a + 3 bytes: AL = 6Ch ('l').

AX006CAHALBX0000BHBLSI0000array aa[0]48a[1]65a[2]6Ca[3]6Ca[4]6Fa[5]00

Index register SI = 0.

AX006CAHALBX0048BHBLSI0000array aa[0]48a[1]65a[2]6Ca[3]6Ca[4]6Fa[5]00

a[SI] = [a + SI]: BL = 48h ('H').

AX006CAHALBX0048BHBLSI0001array aa[0]48a[1]65a[2]6Ca[3]6Ca[4]6Fa[5]00

SI = 1: the next byte.

AX006CAHALBX6548BHBLSI0001array aa[0]48a[1]65a[2]6Ca[3]6Ca[4]6Fa[5]00

BH = a[1] = 65h ('e'). BX = 6548h.

AX006CAHALBX6548BHBLSI0001array aa[0]48a[1]45a[2]6Ca[3]6Ca[4]6Fa[5]00

The array is of type byte, so the size is known: a[1] ← 45h ('E'). The array now reads “HEllo”.

AX006CAHALBX6548BHBLSI0001array aa[0]48a[1]45a[2]6Ca[3]6Ca[4]6Fa[5]00

RET. The array definition sits after the code, where nothing is executed.

ORG 100h
MOV AL, a[3]   ; a + 3
MOV SI, 0
MOV BL, a[SI]  ; a + 0
INC SI
MOV BH, a[SI]  ; a + 1
MOV a[SI], 'E' ; a[1] ← 45h
RET
a DB 48h,65h,6Ch,6Ch,6Fh,0

Arrays

Access Patterns and DUP

Reaching an element
  • A constant index in brackets: MOV AL, a[3]
  • Through the memory index registers (BX, SI, DI, BP): MOV SI, 3 · MOV AL, a[SI]
  • Equivalent forms: a[SI] = [a + SI] = [SI + a]
  • The index counts bytes, not elements.
The DUP operator For large arrays: count DUP(value(s))
  • c DB 5 DUP(9) = c DB 9, 9, 9, 9, 9
  • d DB 5 DUP(1, 2) = d DB 1, 2, 1, 2, 1, 2, 1, 2, 1, 2
  • buffer DB 100 DUP(?): 100 uninitialized bytes
  • table DW 10 DUP(0): 10 words = 20 bytes
c DB 5 DUP(9)0909090909d DB 5 DUP(1, 2)010201020102010201025 bytes10 bytes
DW can define arrays of numbers (w DW 1000h, 2000h); what it cannot define is text strings (DW 'Hello' is invalid). Values above 255 or below −128 need DW.

Arrays · Step through the code

Code Trace: Indexing a DW Array

AX1111AHALBX0000BHBLCX0000CHCLDX0000DHDLSI0000array w (byte by byte)w+011w+111w+222w+322w+433w+533

w[0]: the first word → AX = 1111h.

AX1111AHALBX2222BHBLCX0000CHCLDX0000DHDLSI0000array w (byte by byte)w+011w+111w+222w+322w+433w+533

The second element is at w[2] (each element is 2 bytes): BX = 2222h.

AX1111AHALBX2222BHBLCX0000CHCLDX0000DHDLSI0004array w (byte by byte)w+011w+111w+222w+322w+433w+533

The index of element i = 2 × i: for the third element, SI = 4.

AX1111AHALBX2222BHBLCX3333CHCLDX0000DHDLSI0004array w (byte by byte)w+011w+111w+222w+322w+433w+533

CX = w[SI] = 3333h.

AX1111AHALBX2222BHBLCX3333CHCLDX2211DHDLSI0004array w (byte by byte)w+011w+111w+222w+322w+433w+533

w[1] reads from the middle of an element: bytes 11h, 22h → DX = 2211h. A common mistake!

ORG 100h
MOV AX, w[0]   ; element 1
MOV BX, w[2]   ; element 2
MOV SI, 4
MOV CX, w[SI]  ; element 3
MOV DX, w[1]   ; careful!
RET
w DW 1111h, 2222h, 3333h

Arrays

From Definition to Bytes

Type a DB / DW / DD line to see its bytes. Numbers (decimal, h, b, o), 'text' (DB only), ? and n DUP(…) are supported. Above each cell: the byte offset from the start of the variable.

Arrays

Strings and Terminators

s1 DB 'Welcome', 0             ; zero-terminated
s2 DB 'Welcome$'               ; '$'-terminated (INT 21h / AH = 09h)
s3 DB 13, 10, 'Line', 13, 10, '$'  ; CR LF: carriage return + line feed
s4 DB 7, 'Welcome'             ; length-prefixed: first byte = 7
s5 DB "Ali's", 0               ; single quote inside double quotes
FormatHow is the end found?Where is it used?
zero-terminatedwhen the byte read is 0emu8086.inc PRINT_STRING, the C language
'$'-terminatedwhen the byte read is 24hthe DOS print-string service
length-prefixedas many bytes as the first byte saysthe DOS buffered-input buffer (AH = 0Ah)
  • Space taken in memory = number of characters + terminator: 8 bytes for s1. Forget the terminator and printing runs on, treating the bytes after the string as characters.
  • A '$'-terminated string cannot print '$' itself. Codes for Turkish letters (ç, ğ, ş…) depend on the code page in use; they are not in ASCII’s 0–127 range.

Arrays

Character Constants and ASCII Arithmetic

CharactersASCII codes
'0' … '9'30h … 39h
'A' … 'Z'41h … 5Ah
'a' … 'z'61h … 7Ah
space · '$'20h · 24h
CR · LF0Dh · 0Ah
  • To the assembler a quoted character is just a number: MOV AL, 'A' = MOV AL, 41h.
  • Digit character to number: SUB AL, '0' ('7' = 37h → 7). The other way: ADD AL, '0'.
  • Upper and lower case differ by 20h, i.e. only bit 5: SUB AL, 20h or AND AL, 0DFh converts to upper case, OR AL, 20h to lower case.
AND AL, 0DFh is only correct for letters. Applied to '1' (31h) it gives 11h and ruins the digit, so first check that AL is a letter ('a' ≤ AL ≤ 'z').

Arrays · Step through the code

Code Trace: Converting a String to Upper Case

ORG 100h
LEA SI, s
again:
MOV AL, [SI]
CMP AL, 0          ; terminator?
JE done
CMP AL, 'a'
JB next
CMP AL, 'z'
JA next
SUB AL, 20h        ; 'a' - 'A'
MOV [SI], AL
next:
INC SI
JMP again
done:
RET
s DB 'Ab1c', 0
s41'A'62'b'31'1'63'c'0000SI = 0119hAL00h

LEA SI, s: SI = 0119h, the first byte of the string 'Ab1c' with terminator 0.

s41'A'62'b'31'1'63'c'0000SI = 011AhAL41h

'A' (41h): not 0; CMP AL, 'a' → 41h < 61h, JB jumps. Already upper case, so nothing is written; SI moves on.

s41'A'42'B'31'1'63'c'0000SI = 011BhAL42h

'b' (62h): 'a' ≤ 62h ≤ 'z' → SUB AL, 20h = 42h ('B'), and MOV [SI], AL writes it back.

s41'A'42'B'31'1'63'c'0000SI = 011ChAL31h

'1' (31h): below 'a', skipped. Without the letter check, 31h − 20h = 11h would have ruined the digit.

s41'A'42'B'31'1'43'C'0000SI = 011DhAL43h

'c' (63h) → 43h ('C'). The string is now 'AB1C'.

s41'A'42'B'31'1'43'C'0000AL00hterminator: ZF = 1

The byte read is 0: CMP AL, 0 → ZF = 1, JE done. The loop stopped at the terminator without knowing the length.

The string was changed in place: each byte is read and, if it is a letter, written back to the same address minus 20h. A terminator (0) took the place of a length.

Arrays

Two-Dimensional Arrays (Tables)

[0][0][0][1][0][2][0][3][1][0][1][1][1][2][1][3][2][0][2][1][2][2][2][3]logical viewrow-major order in memory01234567891011offset = row × 4 + col[2][1] → 2 × 4 + 1 = 9
mat DB 3 DUP(4 DUP(0)) ; 3 × 4
tab DW 3 DUP(4 DUP(0)) ; 24 bytes
  • Memory is one-dimensional; a table is laid out in row-major order: all columns of row 0, then row 1…
  • Offset = (row × number of columns + column) × element size.
  • In a word table, [1][2] is at (1 × 4 + 2) × 2 = 12 bytes.
  • Nested DUP reserves rows and columns together: 3 times (4 times 0).
If the number of columns is a power of 2, a shift replaces the multiply: two SHLs for row × 4. The number of rows does not affect the layout; it only bounds the row index.

Arrays · Step through the code

Code Trace: Reading mat[2][1]

NCOLS EQU 4
ORG 100h
MOV AL, row        ; AL = 2
MOV BL, NCOLS      ; BL = 4
MUL BL             ; AX = 2 × 4
ADD AL, col        ; + 1
MOV SI, AX         ; SI = 9
MOV DL, mat[SI]    ; mat[2][1]
RET
mat DB 11h, 12h, 13h, 14h
    DB 21h, 22h, 23h, 24h
    DB 31h, 32h, 33h, 34h
row DB 2
col DB 1
AX0002hBL00hSI0000hDL00h11012113214321422523624731832933103411

AL ← row = 2. Target: mat[2][1]. The table is at 0112h, 3 rows × 4 columns of bytes.

AX0002hBL04hSI0000hDL00h11012113214321422523624731832933103411

NCOLS EQU 4 is encoded in the instruction: BL = 04h (number of columns).

AX0008hBL04hSI0000hDL00h11012113214321422523624731832933103411

MUL BL: AX = AL × BL = 2 × 4 = 0008h. Row 2 starts at byte 8 of the table.

AX0009hBL04hSI0000hDL00h11012113214321422523624731832933103411

ADD AL, col: 8 + 1 = 9. AH is already 0, so AX = 0009h.

AX0009hBL04hSI0009hDL00h11012113214321422523624731832933103411

The offset goes into an index register: SI = 0009h. AL cannot be an index; only BX, SI, DI, BP can address memory.

AX0009hBL04hSI0009hDL32h11012113214321422523624731832933103411mat[2][1]

mat[SI] = [0112h + 9]: DL = 32h. Row 2, column 1.

AX0009hBL04hSI0009hDL32h11012113214321422523624731832933103411

RET. The constant is named NCOLS on purpose: COL EQU 4 would be the same name as col DB 1, since the assembler ignores case, and give a “duplicate definition” error.

Row and column were read from variables at run time; the same code works for every element. In a word table the result would also be multiplied by 2.

Addresses

Getting a Variable’s Address

LEA (Load Effective Address)Loads the address (offset) of a variable into a register: LEA BX, VAR1. It also works for indexed variables: LEA BX, a[SI].
OFFSETAn assembler operator; it supplies the variable’s offset value at assembly time: MOV BX, OFFSET VAR1.
VAR1 · 010Dh22hLEA BX, VAR1010DhaddressMOV AL, VAR122hvalue
A variable’s address is very handy for passing parameters to a procedure: instead of a large array, the procedure gets just its 16-bit address.
InstructionWhat goes into BX?
MOV BX, VAR1 (VAR1 DW)the value of VAR1 (its contents)
LEA BX, VAR1 · MOV BX, OFFSET VAR1the address of VAR1 (its offset)

Addresses · Step through the code

Code Trace: Updating a Variable Through LEA

AX0000AHALBX0000BHBLDS:010C … (VAR1 = 010Dh)010CC3010D22010E00

The code starts at 0100h; the assembler placed VAR1 at 010Dh (12 bytes of code + RET).

AX0022AHALBX0000BHBLDS:010C … (VAR1 = 010Dh)010CC3010D22010E00

AL = the value of VAR1 = 22h.

AX0022AHALBX010DBHBLDS:010C … (VAR1 = 010Dh)010CC3010D22010E00

BX = the address of VAR1 = 010Dh. BX is now a pointer to VAR1.

AX0022AHALBX010DBHBLDS:010C … (VAR1 = 010Dh)010CC3010D44010E00

VAR1 is updated through [BX]: [010Dh] = 44h.

AX0044AHALBX010DBHBLDS:010C … (VAR1 = 010Dh)010CC3010D44010E00

VAR1 is read again: AL = 44h. A variable can be reached by its name or through a pointer.

AX0044AHALBX010DBHBLDS:010C … (VAR1 = 010Dh)010CC3010D44010E00

RET. END is the directive that marks the end of the source file.

ORG 100h
MOV AL, VAR1          ; value
LEA BX, VAR1          ; address
MOV BYTE PTR [BX], 44h
MOV AL, VAR1          ; new value
RET
VAR1 DB 22h
END

Addresses

Are LEA and OFFSET the Same?

The same program with OFFSET

ORG 100h
MOV AL, VAR1
MOV BX, OFFSET VAR1
MOV BYTE PTR [BX], 44h
MOV AL, VAR1
RET
VAR1 DB 22h
END
  • In emu8086, LEA BX, VAR1 and MOV BX, OFFSET VAR1 assemble to the same machine code: BB 0D 01 (3 bytes, MOV BX, 010Dh). VAR1 = 010Dh in both programs.
  • A real 8086 assembler such as MASM encodes LEA as its own instruction: 8D 1E 0D 01 (4 bytes).
  • The difference: LEA BX, a[SI] computes the address at run time (a + SI); OFFSET gives only a constant address.
  • Only BX, SI, DI, BP may appear inside square brackets.

Addresses · Step through the code

Code Trace: LEA a[BX] vs. OFFSET a

ORG 100h
MOV BX, 2
LEA SI, a[BX]      ; SI = a + BX
MOV DI, OFFSET a   ; fixed address
ADD DI, BX         ; add by hand
MOV AL, [SI]
INC BX
LEA SI, a[BX]      ; new address
MOV AH, [SI]
RET
a DB 'ABCD'
a'A' 41h0116'B' 42h0117'C' 43h0118'D' 44h0119BX0002hSI0000hDI0000hAX0000h

BX = 2: the index. The array a starts at 0116h: 'A' 'B' 'C' 'D'.

a'A' 41h0116'B' 42h0117'C' 43h0118'D' 44h0119SIBX0002hSI0118hDI0000hAX0000h

LEA SI, a[BX]: the address is computed at run time: 0116h + 2 = 0118h. Memory is not read.

a'A' 41h0116'B' 42h0117'C' 43h0118'D' 44h0119SIDIBX0002hSI0118hDI0116hAX0000h

OFFSET a is a constant known at assembly time: DI = 0116h. It cannot include BX.

a'A' 41h0116'B' 42h0117'C' 43h0118'D' 44h0119SIDIBX0002hSI0118hDI0118hAX0000h

With OFFSET the same address needs an extra ADD: DI = 0118h = SI.

a'A' 41h0116'B' 42h0117'C' 43h0118'D' 44h0119SIDIBX0002hSI0118hDI0118hAX0043h

AL = [SI] = 43h ('C').

a'A' 41h0116'B' 42h0117'C' 43h0118'D' 44h0119SIDIBX0003hSI0118hDI0118hAX0043h

BX = 3.

a'A' 41h0116'B' 42h0117'C' 43h0118'D' 44h0119SIDIBX0003hSI0119hDI0118hAX0043h

LEA recomputes with the current BX: SI = 0119h.

a'A' 41h0116'B' 42h0117'C' 43h0118'D' 44h0119SIDIBX0003hSI0119hDI0118hAX4443h

AH = [SI] = 44h ('D'). AX = 4443h.

LEA computes the address from registers at run time; OFFSET gives only a constant address known at assembly time.

Addresses

Pointer Variables

ORG 100h
MOV BX, p          ; 010Bh
LEA SI, p          ; 010Fh
MOV DI, OFFSET arr
RET
arr   DB 'KAYA'    ; 010Bh
p     DW arr       ; 010Fh, holds 010Bh
far_p DD arr       ; offset + segment
  • If the value of a DW is a variable name, the address of that variable is stored: p is a pointer variable.
  • The pointer lives in memory, so it can change: ADD p, 2 moves it further into the array.
  • To use it, first load it into an address register: MOV BX, p · MOV AL, [BX].
  • A far pointer defined with DD holds offset and segment together; LDS SI, far_p or LES DI, far_p loads it in one instruction.
InstructionWhat goes into the register?Value
MOV BX, pthe address stored in p (the array’s address)010Bh
LEA SI, pp’s own address010Fh
MOV DI, OFFSET arrthe array’s address, fixed at assembly time010Bh

Addresses · Step through the code

Code Trace: A Pointer in Memory

ORG 100h
MOV BX, p          ; contents of p
MOV AL, [BX]       ; arr[0]
ADD p, 2           ; advance
MOV BX, p
MOV AH, [BX]       ; arr[2]
RET
arr  DB 'KAYA'
p    DW arr        ; address
arr4B0112410113590114410115p (0116)0112hBX0112hAX0000h

MOV BX, p reads the contents of p. Since p = DW arr, BX = 0112h, the array’s address.

arr4B0112410113590114410115p (0116)0112hBX0112hAX004Bh

MOV AL, [BX]: the byte the pointer points to: AL = 4Bh ('K').

arr4B0112410113590114410115p (0116)0114hBX0112hAX004Bhpointer moved

ADD p, 2: the pointer in memory now points to 0114h. BX is unchanged; it still holds the old address.

arr4B0112410113590114410115p (0116)0114hBX0114hAX004Bh

The pointer is reloaded: BX = 0114h.

arr4B0112410113590114410115p (0116)0114hBX0114hAX594Bh

MOV AH, [BX]: AH = 59h ('Y'). AX = 594Bh.

arr4B0112410113590114410115p (0116)0114hBX0114hAX594Bh

RET. The array’s address never appears in the code; the pointer in memory decided which element was read.

A pointer variable keeps an address in memory; with so few registers, it lets you track many arrays. Load it into BX, SI or DI before each use.

Constants

Constants (EQU)

  • Like variables, but they exist only at assembly time; they take no memory.
  • Once defined, a constant’s value cannot change.
  • They let different parts of a program use the same value, updated in one place.
  • Definition: name EQU <any expression>
  • The assembler replaces the name with its value at every use: MOV AX, k → MOV AX, 5 (B8 05 00).
k      EQU 5
ROWLEN EQU 80

MOV AX, k         ; AX = 5
MOV BX, ROWLEN*2  ; BX = 160
MOV CL, k+1       ; CL = 6
k EQU 5k DB 5
Memorynone1 byte
MOV AL, kAL ← 5 (immediate)AL ← [k] (from memory)
Can it change?noyes: MOV k, 7

Constants · Step through the code

Code Trace: EQU Is Built into the Instruction

k      EQU 5
ROWLEN EQU 80
ORG 100h
MOV AX, k         ; MOV AX, 5
MOV BX, ROWLEN*2  ; MOV BX, 160
MOV CL, k+1       ; MOV CL, 6
MOV DL, v         ; from memory
RET
v DB 5            ; takes 1 byte
symbols (assembly)k5memory (segment 0700h)0100B8 05 00MOV AX, 50103BB A0 00MOV BX, 1600106B1 06MOV CL, 601088A 16 0D 01MOV DL, [010Dh]010CC3RET010D05vAX0000hBX0000hCL00hDL00h

k EQU 5: the assembler records k = 5 in its symbol table. No memory is reserved and no machine code is produced.

symbols (assembly)k5ROWLEN80memory (segment 0700h)0100B8 05 00MOV AX, 50103BB A0 00MOV BX, 1600106B1 06MOV CL, 601088A 16 0D 01MOV DL, [010Dh]010CC3RET010D05vAX0000hBX0000hCL00hDL00h

ROWLEN = 80 also exists only at assembly time. The program starts at 0100h; its first byte is the code of MOV AX, k.

symbols (assembly)k5ROWLEN80v010Dh (address)memory (segment 0700h)0100B8 05 00MOV AX, 50103BB A0 00MOV BX, 1600106B1 06MOV CL, 601088A 16 0D 01MOV DL, [010Dh]010CC3RET010D05vAX0005hBX0000hCL00hDL00h

MOV AX, k → MOV AX, 5 (B8 05 00): 5 is inside the instruction, an immediate. AX = 0005h.

symbols (assembly)k5ROWLEN80v010Dh (address)memory (segment 0700h)0100B8 05 00MOV AX, 50103BB A0 00MOV BX, 1600106B1 06MOV CL, 601088A 16 0D 01MOV DL, [010Dh]010CC3RET010D05vAX0005hBX00A0hCL00hDL00h

ROWLEN*2 was computed at assembly time: 160 = 00A0h → BX = 00A0h.

symbols (assembly)k5ROWLEN80v010Dh (address)memory (segment 0700h)0100B8 05 00MOV AX, 50103BB A0 00MOV BX, 1600106B1 06MOV CL, 601088A 16 0D 01MOV DL, [010Dh]010CC3RET010D05vAX0005hBX00A0hCL06hDL00h

k+1 = 6 → CL = 06h. No addition happens at run time.

symbols (assembly)k5ROWLEN80v010Dh (address)memory (segment 0700h)0100B8 05 00MOV AX, 50103BB A0 00MOV BX, 1600106B1 06MOV CL, 601088A 16 0D 01MOV DL, [010Dh]010CC3RET010D05vAX0005hBX00A0hCL06hDL05h

v is a variable: the instruction carries its address (010Dh) and the value is read from memory: DL = 05h. v takes 1 byte; k takes none.

Machine codes follow the MASM/emu8086 encoding: B8 iw (MOV AX, imm), BB iw (MOV BX, imm), B1 ib (MOV CL, imm), 8A 16 addr (MOV DL, [addr]).

Constants

The Location Counter ($) and Size Operators

  • The assembler keeps the offset where the next byte will go in a counter: the location counter. In source code it is written $.
  • Right after an array, $ − arr is the array’s size in bytes. If the array grows or shrinks, the constant stays correct by itself.
  • The EQU line must come immediately after the array; any definition in between is counted too.
  • MASM’s TYPE, LENGTHOF and SIZEOF operators read the same information from the variable’s definition.
s DB 'Welcome', 0
n EQU $ - s        ; 8
w DW 10 DUP(0)
ExpressionValueMeaning
n8size of s in bytes
TYPE s1element size (DB)
LENGTHOF s8number of elements
TYPE w2element size (DW)
LENGTHOF w10number of elements
SIZEOF w20LENGTHOF × TYPE
All of these are assembly-time constants: MOV CX, LENGTHOF w builds 10 into the instruction and takes no memory. For a word array, take the loop count from LENGTHOF and the byte length from SIZEOF.

Constants · Step through the code

Code Trace: Size with $, Maximum with a Loop

ORG 100h
JMP start
arr DB 12, 47, 5, 31
n EQU $ - arr      ; 4
big DB ?
start:
MOV CX, n          ; CX = 4
MOV SI, 0
MOV AL, arr[SI]    ; first guess
check:
CMP AL, arr[SI]
JAE skip           ; AL >= element
MOV AL, arr[SI]    ; new guess
skip:
INC SI
LOOP check
MOV big, AL
RET
arr12010247010350104310105big00hCX0004hSI0000hAL0ChAL = 12

JMP start skips the data. n EQU $ − arr = 4 was computed at assembly time: CX = 0004h. The first element is the first guess: AL = 12.

arr12010247010350104310105big00hCX0003hSI0001hAL0ChAL = 12

Pass 1: 12 is compared with 12; JAE jumps (equal). SI = 1, LOOP: CX = 3.

arr12010247010350104310105big00hCX0002hSI0002hAL2FhAL = 47

Pass 2: 12 < 47 → CF = 1, JAE does not jump; AL = 47 (2Fh). SI = 2, CX = 2.

arr12010247010350104310105big00hCX0001hSI0003hAL2FhAL = 47

Pass 3: 47 ≥ 5 → skipped, AL unchanged. SI = 3, CX = 1.

arr12010247010350104310105big00hCX0000hSI0004hAL2FhAL = 47

Pass 4: 47 ≥ 31 → skipped. LOOP: CX = 0, the loop ends.

arr12010247010350104310105big2FhCX0000hSI0004hAL2FhAL = 47big = 47

MOV big, AL: the largest element, 2Fh = 47, is stored; RET.

The loop count is not hard-coded; add an element and n changes by itself. JAE compares unsigned; use JGE for signed arrays.

Example Programs · Step through the code

Code Trace: Moving Data Between Variables

AX0000AHALvariables (DS)value10Avalue1+100value200value2+100

jmp start makes the CPU jump over the data; otherwise the data bytes would be executed as instructions.

AX000AAHALvariables (DS)value10Avalue1+100value200value2+100

value1 → AX = 000Ah. There is no direct memory-to-memory MOV, so a register sits in between.

AX000AAHALvariables (DS)value10Avalue1+100value20Avalue2+100

AX → value2: 0A 00 in memory. Now value1 = value2 = 10.

AX000AAHALvariables (DS)value10Avalue1+100value20Avalue2+100

end start: the end of the source; start is the program’s entry point.

ORG 100h
jmp start
value1 DW 10    ; a 2-byte variable
value2 DW 0     ; a second one
start:
MOV AX, value1  ; AX ← value1
MOV value2, AX  ; value2 ← AX
RET
end start

Example Programs · Step through the code

Code Trace: Adding Variables

AX0000AHALFlagsCF=0ZF=0SF=0OF=0PF=0AF=0variables (DS)value105value1+101value203value2+101result00result+100

Start: result is defined with ?; emu8086 stores 00 00, but do not rely on that value.

AX0105AHALFlagsCF=0ZF=0SF=0OF=0PF=0AF=0variables (DS)value105value1+101value203value2+101result00result+100

AX = value1 = 0105h.

AX0208AHALFlagsCF=0ZF=0SF=0OF=0PF=0AF=0variables (DS)value105value1+101value203value2+101result00result+100

ADD can take a memory operand directly: 0105h + 0103h = 0208h. Flags: CF = 0, ZF = 0, PF = 0 (08h has a single 1 bit).

AX0208AHALFlagsCF=0ZF=0SF=0OF=0PF=0AF=0variables (DS)value105value1+101value203value2+101result08result+102

result ← AX: 08 02 in memory (little-endian).

AX0208AHALFlagsCF=0ZF=0SF=0OF=0PF=0AF=0variables (DS)value105value1+101value203value2+101result08result+102

Result: result = 0208h = 520 (261 + 259).

ORG 100h
jmp start
value1 DW 105h  ; initially 105h
value2 DW 103h  ; initially 103h
result DW ?     ; undefined
start:
MOV AX, value1  ; AX ← value1
ADD AX, value2  ; AX ← AX + value2
MOV result, AX  ; result ← AX
RET
end start

Example Programs · Step through the code

Code Trace: Find the Larger (10, 5)

AX000AAHALFlagsCF=0ZF=0SF=0OF=0variables (DS)value10Avalue1+100value205value2+100max_value00max_value+100

AX = value1 = 000Ah.

AX000AAHALFlagsCF=0ZF=0SF=0OF=0variables (DS)value10Avalue1+100value205value2+100max_value00max_value+100

CMP subtracts but does not store the result; it only sets the flags: 10 − 5.

AX000AAHALFlagsCF=0ZF=0SF=0OF=0variables (DS)value10Avalue1+100value205value2+100max_value00max_value+100

JGE (signed ≥): SF = OF, so it jumps → greater_or_equal.

AX000AAHALFlagsCF=0ZF=0SF=0OF=0variables (DS)value10Avalue1+100value205value2+100max_value00max_value+100

AX = value1 (it already was; the line is redundant but harmless).

AX000AAHALFlagsCF=0ZF=0SF=0OF=0variables (DS)value10Avalue1+100value205value2+100max_value0Amax_value+100

max_value ← AX = 000Ah.

AX000AAHALFlagsCF=0ZF=0SF=0OF=0variables (DS)value10Avalue1+100value205value2+100max_value0Amax_value+100

RET: back to DOS. Without RET the CPU would execute the bytes of value1 as instructions.

ORG 100h
MOV AX, value1     ; AX ← value1
CMP AX, value2     ; AX − value2
JGE greater_or_equal
MOV AX, value2     ; if less, value2
JMP store_max
greater_or_equal:
MOV AX, value1
store_max:
MOV max_value, AX  ; store result
RET
value1 DW 10
value2 DW 5
max_value DW ?

Example Programs · Step through the code

Code Trace: Find the Larger (3, 7)

AX0003AHALFlagsCF=0ZF=0SF=0OF=0variables (DS)value103value1+100value207value2+100max_value00max_value+100

AX = value1 = 0003h.

AX0003AHALFlagsCF=1ZF=0SF=1OF=0variables (DS)value103value1+100value207value2+100max_value00max_value+100

CMP subtracts but does not store the result; it only sets the flags: 3 − 7.

AX0003AHALFlagsCF=1ZF=0SF=1OF=0variables (DS)value103value1+100value207value2+100max_value00max_value+100

SF ≠ OF → the condition fails, no jump; execution continues with the next instruction.

AX0007AHALFlagsCF=1ZF=0SF=1OF=0variables (DS)value103value1+100value207value2+100max_value00max_value+100

AX = value2 = 0007h (the larger one).

AX0007AHALFlagsCF=1ZF=0SF=1OF=0variables (DS)value103value1+100value207value2+100max_value00max_value+100

JMP jumps unconditionally → store_max.

AX0007AHALFlagsCF=1ZF=0SF=1OF=0variables (DS)value103value1+100value207value2+100max_value07max_value+100

max_value ← AX = 0007h.

AX0007AHALFlagsCF=1ZF=0SF=1OF=0variables (DS)value103value1+100value207value2+100max_value07max_value+100

RET: back to DOS. Without RET the CPU would execute the bytes of value1 as instructions.

ORG 100h
MOV AX, value1     ; AX ← value1
CMP AX, value2     ; AX − value2
JGE greater_or_equal
MOV AX, value2     ; if less, value2
JMP store_max
greater_or_equal:
MOV AX, value1
store_max:
MOV max_value, AX  ; store result
RET
value1 DW 3
value2 DW 7
max_value DW ?

Example Programs

Notes on Comparison

CMPCMP a, b computes a − b and discards the result; only the flags (CF, ZF, SF, OF, PF, AF) are updated.
Signed / unsignedJGE, JG, JL: signed (SF, OF). JAE, JA, JB: unsigned (CF). With value1 = −1 (FFFFh) and value2 = 5, JGE does not jump but JAE does.
Why RET?The data definitions come right after the code; without RET the CPU would execute the bytes 0A 00 05 00… as instructions. Fix: RET (or a jmp start at the top).
ImprovementThe MOV AX, value1 under greater_or_equal is redundant; AX already holds value1. The jump can go straight to store_max.
JAE (unsigned)0565535 (FFFFh)JGE (signed)0−15−3276832767FFFFh ≥ 0005h ? unsigned yes, signed no

Example Programs

Doubleword Variables

org 100h
jmp start
mydouble dd 12345678h  ; double word
mywords  dw 5678h      ; 2 separate words
         dw 1234h
mybytes  db 78h        ; 4 separate bytes
         db 56h
         db 34h
         db 12h
data dd 00010010001101000101011001111000b
start:  ; load data into DX:AX
mov ax, word ptr data
mov dx, word ptr [data+2]
ret

All four definitions produce the same bytes in memory:

+078+156+234+312low word → AXhigh → DX
  • DD is 32 bits = 4 bytes; the lowest byte is at the lowest address.
  • The 8086 has no 32-bit register: the value is held in the pair DX:AX (DX the high word, AX the low word).

Example Programs · Step through the code

Code Trace: A 32-Bit Load into DX:AX

DX0000DHDLAX5678AHALdata (4 bytes)data+078data+156data+234data+312

The first two bytes of data (78 56) → AX = 5678h. word ptr states the size explicitly, since data is a DD.

DX1234DHDLAX5678AHALdata (4 bytes)data+078data+156data+234data+312

The two bytes at data+2 (34 12) → DX = 1234h. DX:AX = 1234:5678h = 12345678h.

mov ax, word ptr data      ; data+0
mov dx, word ptr [data+2]  ; data+2
ret
data dd 12345678h

Example Programs

Common Mistakes with Variables

Confusing value and addressMOV SI, arr loads the first word; for the address use LEA SI, arr.
Memory-to-memory movesThere is no MOV v1, v2; the value goes through a register.
Indexing a word arrayElement 3 is at w[4]: the index counts bytes.
Trusting ?total DW ? may not be zero; start with MOV total, 0.
Forgetting the suffixDW 10 is decimal 10 (000Ah); for hex write 10h.
Name clashesCOL EQU 4 and col DB 1 are the same name: “duplicate definition”.
Data in the path of the codeWithout RET or JMP, data bytes are taken for instructions.
Forgetting the terminatorWithout 0 or '$', printing runs on into the bytes that follow.

Example Programs

Data Definition Summary

SyntaxMeaningMemory
x DB 7a byte07
y DW 1234ha word34 12
z DD 12345678ha doubleword78 56 34 12
s DB 'Hi', 0string + terminator48 69 00
t DB 3 DUP(0)repetition00 00 00
u DW ?uninitialized?? ?? (emu8086: 00 00)
k EQU 10constanttakes no space

Address: LEA BX, x / MOV BX, OFFSET x. Value: MOV AL, x. Changing the size: word ptr, byte ptr.

Summary

Summary

VariableA name for a memory location; the assembler replaces the name with an offset.
DB / DW / DD1, 2, 4 bytes; little-endian; ? means uninitialized.
ORG 100h.COM code starts at 0100h; the first 256 bytes are the PSP. Data goes after RET or is skipped with jmp.
ArraysConsecutive bytes; a[i] is a byte offset; DUP repeats; in a DW array the index is 2 × i.
LEA / OFFSETGet an address; LEA also computes indexed addresses at run time.
EQUAn assembly-time constant; takes no memory and cannot change.

Summary

Exercises

  1. If x DB 5, 6 · y DW 0ABCDh · z DB 'OK' start at 0200h, give the address and value of every byte.
  2. How many bytes does table DW 10 DUP(?) take? What is the offset of the 7th element (index 6)?
  3. For y above, what is AL after MOV AL, BYTE PTR y and after MOV AL, BYTE PTR y+1?
  4. What is the difference between LEA SI, a[BX] and MOV SI, OFFSET a?
  5. Write code that stores the sum of two DW variables in a third and sets ovf DB 1 if the sum overflows (CF = 1).
  6. Which pair of instructions adds 1 to dd 0FFFFFFFFh held in DX:AX?
Answers
  1. 0200: 05, 0201: 06, 0202: CD, 0203: AB, 0204: 4F ('O'), 0205: 4B ('K').
  2. 20 bytes; offset 6 × 2 = 12 = 0Ch.
  3. CDh and ABh.
  4. LEA computes a + BX at run time; OFFSET is only the constant address of a.
  5. MOV AX,v1 · ADD AX,v2 · MOV v3,AX · JNC done · MOV ovf,1 · done:
  6. ADD AX,1 · ADC DX,0 (carries into the high word).

Summary

References

  • emu8086 documentation, 8086 assembler tutorial for beginners, part 3: variables (DB, DW, DUP, LEA, OFFSET, EQU).
  • Barry B. Brey, The Intel Microprocessors, 8th ed., Pearson.
  • Kip R. Irvine, Assembly Language for x86 Processors, Pearson (data definition directives).
  • Microsoft MASM 6.1 Programmer's Guide: DB/DW/DD, DUP, EQU, OFFSET.

The End

Chapter 4.2: Variables
Previous: Chapter 4.1: Memory Access · Next: Chapter 5.1: Interrupts
1 / 1 Sercan KÜLCÜ. All rights reserved.