0% found this document useful (0 votes)
4 views16 pages

Z80 Assembly Programming Challenges

The document outlines a series of assembly programming problems for the Z80 microprocessor, covering various tasks such as arithmetic calculations, memory manipulation, and user input handling. Each problem includes assembly code examples, expected results, and explanations of how the code works. The problems range from basic operations like addition and multiplication to more complex tasks like displaying messages on an LCD and handling keypad inputs.

Uploaded by

israaouss01
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views16 pages

Z80 Assembly Programming Challenges

The document outlines a series of assembly programming problems for the Z80 microprocessor, covering various tasks such as arithmetic calculations, memory manipulation, and user input handling. Each problem includes assembly code examples, expected results, and explanations of how the code works. The problems range from basic operations like addition and multiplication to more complex tasks like displaying messages on an LCD and handling keypad inputs.

Uploaded by

israaouss01
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

� Problem 1: Subroutine Calculation — G = 3(A + B) - 2C

Description:
Write a program that calculates the expression:
G = 3 × (A + B) - 2 × C
Values:

 A = 2AH, B = 14H, C = 05H


Store result G in memory location 2051H.
Use a subroutine called CALC. The stack starts at 3FBOH.

Assembly Code:

ORG 2000H
LD A, 2AH ; A = 2A
LD B, 14H ; B = 14
LD C, 05H ; C = 05
LD SP, 3FBOH ; Stack pointer
LD HL, 2051H ; HL points to result memory location
CALL CALC ; Call subroutine
HALT ; End program

; Subroutine CALC
CALC: PUSH AF
PUSH BC

LD A, B ; A = B
ADD A, C ; A = A + C -> now A = A + B
ADD A, A ; A = 2 × (A + B)
ADD A, B ; A = 3 × (A + B)
LD B, A ; Store in B temporarily

LD A, C ; A = C
ADD A, A ; A = 2 × C
LD C, A ; C = 2C

LD A, B ; A = 3(A + B)
SUB C ; A = A - 2C
LD (HL), A ; Store result in memory

POP BC
POP AF
RET

Explanation:

 Main code initializes A, B, and C and sets up the stack and HL.
 The subroutine CALC does the math:
o Computes 3 × (A + B) using repeated addition.
o Computes 2 × C.
o Subtracts 2C from the earlier result.
 Result stored at memory location 2051H.

Expected Result:

 Memory[2051H] = A2H (Assuming 3×(2A+14) - 2×05 = A2 in hex)


Problem 2: H = A × 5

Goal: Multiply A = 06H by 5 and store in memory at 2050H.

ORG 2000H
LD A, 06H
LD SP, 3FBOH
LD HL, 2050H
CALL MULT5
HALT

MULT5: PUSH AF
ADD A, A ; A × 2
ADD A, A ; A × 4
ADD A, 06H ; A × 4 + A = A × 5
LD (HL), A
POP AF
RET

Result: 06H × 5 = 1EH at 2050H.

Problem 3: Calculate Sum = A + B + C


ORG 2000H
LD A, 12H
LD B, 0FH
LD C, 03H
LD HL, 2050H
ADD A, B
ADD A, C
LD (HL), A
HALT

Result: 12H + 0FH + 03H = 24H

Problem 4: Store Keypad Value via SCAN

Reads keypad and stores pressed key in 2050H.

ORG 2000H
LD SP, 3FBOH
CALL SCAN
LD A, (3FFCH)
LD (2050H), A
HALT

SCAN EQU 03F1H

Result: Key press is stored in 2050H.

Problem 5: Show "Hi" on LCD


ORG 2000H
LD HL, MSG
CALL LCDCLR
CALL LCDSTRING
HALT

MSG: DB "Hi", 00H

LCDCLR EQU 0480H


LCDSTRING EQU 04B3H

Result: LCD shows: Hi

Problem 6: Light tone when key “3” is pressed


ORG 2000H
START: CALL SCAN
LD A, (BUF1)
CP 33H ; ASCII for '3'
JP NZ, START
LD HL, 7000H
CALL TONE
HALT

SCAN EQU 03F1H


BUF1 EQU 3FFCH
TONE EQU 04C1H

Result: Waits until '3' is pressed, plays tone.

Problem 7: Swap two numbers (X and Y)


ORG 2000H
LD A, 22H
LD B, 55H
EX AF, AF' ; Save A in alternate
LD A, B ; A = B
EX AF, AF' ; B = original A
HALT

Result: A and B swapped.

Problem 8: Delay Loop


ORG 2000H
LD BC, 0FFFFH
LOOP: DEC BC
LD A, B
OR C
JP NZ, LOOP
HALT

Result: Software delay using loop.

Problem 9: Compare two numbers


ORG 2000H
LD A, 3CH
CP 3CH
JP Z, EQUAL
HALT
EQUAL: LD (2050H), A
HALT

Result: Stores A at 2050H if equal.


Problem 10: Stack Trace
ORG 2000H
LD SP, 3FBOH
LD A, 22H
PUSH AF
LD A, 33H
PUSH AF
POP AF ; A = 33
POP AF ; A = 22
HALT

Result: Pushes and restores values using stack.

� 10 Hard Z80 Assembly Problems

Hard 1: Evaluate Expression — H = (A + B) × (C − D)

Objective:
Given A=05H, B=03H, C=09H, D=04H, compute (A + B) × (C − D) and store in 2050H.

ORG 2000H
LD A, 05H
LD B, 03H
LD C, 09H
LD D, 04H
LD SP, 3FBOH
LD HL, 2050H
CALL CALC
HALT

CALC: PUSH AF
PUSH BC
PUSH DE

LD A, B
ADD A, 05H ; A + B = 08H
LD B, A

LD A, C
SUB D ; C - D = 05H
LD C, A

; Multiply B × C
LD A, 00H
LD D, C
MUL: ADD A, B
DEC D
JP NZ, MUL

LD (HL), A

POP DE
POP BC
POP AF
RET

Result: 8 × 5 = 28H stored in 2050H

Hard 2: Stack Reverse (Push values then reverse them)


Push 4 values to the stack: 11H, 22H, 33H, 44H, then pop them in reverse to memory: 2050H-2053H.

ORG 2000H
LD SP, 3FB4H
LD A, 11H
PUSH AF
LD A, 22H
PUSH AF
LD A, 33H
PUSH AF
LD A, 44H
PUSH AF

LD HL, 2053H
POP AF
LD (HL), A
DEC HL
POP AF
LD (HL), A
DEC HL
POP AF
LD (HL), A
DEC HL
POP AF
LD (HL), A
HALT

Result:

 2050H = 11H
 2051H = 22H
 2052H = 33H
 2053H = 44H

Hard 3: Display Custom Message from User (via Keypad)

Type up to 5 characters on keypad, store in MEM, display on LCD.

ORG 2000H
LD SP, 3FBOH
LD HL, MEM
LD B, 05
LOOP: CALL SCAN
LD A, (BUF1)
LD (HL), A
INC HL
DJNZ LOOP
LD (HL), 00H ; null terminator
LD HL, MEM
CALL LCDCLR
CALL LCDSTRING
HALT

BUF1 EQU 3FFCH


MEM EQU 2060H
SCAN EQU 03F1H
LCDCLR EQU 0480H
LCDSTRING EQU 04B3H

Result: User types 5 chars → shown on LCD.

Hard 4: Tone Melody Sequencer


Play a simple melody using 3 tones with pauses.

ORG 2000H
CALL TONE1
CALL DELAY
CALL TONE2
CALL DELAY
CALL TONE3
HALT

TONE1: LD HL, 3000H


CALL TONE
RET
TONE2: LD HL, 4000H
CALL TONE
RET
TONE3: LD HL, 5000H
CALL TONE
RET

DELAY: LD BC, 0FFFFH


D1: DEC BC
LD A, B
OR C
JP NZ, D1
RET

TONE EQU 04C1H

Result: Melody of 3 tones with gaps.

Hard 5: Password Check via Keypad

Ask user to input 4-key password. Check if it matches 1234.

ORG 2000H

LD SP, 3FBOH
LD HL, INPUT
LD B, 04
INLOOP: CALL SCAN
LD A, (BUF1)
LD (HL), A
INC HL
DJNZ INLOOP
CALL CHECK
HALT

CHECK: LD HL, INPUT


LD DE, PASS
LD B, 04
CMP: LD A, (HL)
CP (DE)
JP NZ, FAIL
INC HL
INC DE
DJNZ CMP
CALL LCDCORRECT
RET

FAIL: CALL LCDWRONG


RET

INPUT: DS 4
PASS: DB '1','2','3','4'

LCDCORRECT: LD HL, OKMSG


CALL LCDCLR
CALL LCDSTRING
RET
LCDWRONG: LD HL, BADMSG
CALL LCDCLR
CALL LCDSTRING
RET

OKMSG: DB "Access", 00H


BADMSG: DB "Wrong!", 00H

SCAN EQU 03F1H


BUF1 EQU 3FFCH
LCDCLR EQU 0480H
LCDSTRING EQU 04B3H

Result: Accepts 4 digits, shows "Access" or "Wrong!" on LCD.

Hard 6: Recursive Tone Generator Using Stack

Simulate recursion (not truly recursive, but nested CALLs) for generating tones with different durations.

ORG 2000H
CALL TONE1
HALT

TONE1: LD HL, 2000H


CALL TONE
CALL TONE2
RET
TONE2: LD HL, 3000H
CALL TONE
CALL TONE3
RET
TONE3: LD HL, 4000H
CALL TONE
RET

TONE EQU 04C1H

Result: Simulates nested tones (like recursion).

Hard 7: Rotate 3 Bytes in Memory

Rotate values from 2050H, 2051H, 2052H → left one step.

asm
CopyEdit
ORG 2000H
LD HL, 2050H
LD A, (HL)
INC HL
LD B, (HL)
INC HL
LD C, (HL)
LD (2050H), B
LD (2051H), C
LD (2052H), A
HALT
Result:
If [2050] = A, [2051] = B, [2052] = C
→ becomes B, C, A

Hard 8: Conditional Tone Based on Key Press

If user presses key '7', tone plays longer than for '1'.

ORG 2000H
CALL SCAN
LD A, (BUF1)
CP '7'
JP Z, LONG
CP '1'
JP Z, SHORT
HALT

LONG: LD HL, 8000H


JP PLAY
SHORT: LD HL, 3000H

PLAY: CALL TONE


HALT

SCAN EQU 03F1H


BUF1 EQU 3FFCH
TONE EQU 04C1H

Result: Different tones depending on key.

Hard 9: Compare Two Inputs and Show Larger

Input 2 numbers from keypad, show the bigger one on LCD.

ORG 2000H
CALL SCAN
LD A, (BUF1)
LD (X), A
CALL SCAN
LD A, (BUF1)
LD (Y), A

LD A, (X)
CP (Y)
JP C, SHOWY
LD A, (X)
JP SHOW
SHOWY: LD A, (Y)
SHOW: LD (MSG), A
LD (MSG+1), 00H
LD HL, MSG
CALL LCDCLR
CALL LCDSTRING
HALT

X EQU 2060H
Y EQU 2061H
MSG EQU 2070H

SCAN EQU 03F1H


BUF1 EQU 3FFCH
LCDCLR EQU 0480H
LCDSTRING EQU 04B3H

Result: Shows the larger key on LCD.

Hard 10: LCD Counter from 0 to 9 with Tone Per Count


ORG 2000H
LD B, 0AH
LD C, 30H ; ASCII '0'
LOOP: LD (MSG), C
LD (MSG+1), 00H
LD HL, MSG
CALL LCDCLR
CALL LCDSTRING
LD HL, 2000H
CALL TONE
INC C
DJNZ LOOP
HALT

MSG EQU 2050H

LCDCLR EQU 0480H


LCDSTRING EQU 04B3H
TONE EQU 04C1H

Result: LCD shows 0→9, with tone after each count.

✅ Monitor ROM Subroutines (with address and purpose)

Address (Hex) Label Function

0000 RST0 Reset vector — jumps to cold start or restart

0008 RST8 Beep (tone generator), single tone

0010 RST10 Scan keypad

0018 RST18 Display character on LCD

0020 RST20 Clear LCD display

0028 RST28 Display string on LCD (null-terminated)

0030 RST30 Convert BCD to ASCII and display

0038 RST38 Maskable interrupt vector

� Z80 Monitor Routine-Based Problems (Inspired by the PDF)

Problem 1: Display "HELLO" on the LCD

Goal: Use RST 28 to display a null-terminated string.

ORG 2000H
LD HL, MSG
RST 20 ; Clear LCD
RST 28 ; Display string
HALT

MSG: DB "HELLO", 00H

Explanation:

 HL points to the message.


 RST 20 clears the screen.
 RST 28 prints the string starting at HL.

Result on LCD: HELLO

Problem 2: Scan a key and display it

Goal: Use RST 10 to read from keypad, then show it using RST 18.

ORG 2000H
RST 10 ; Read key
RST 18 ; Display character
HALT

Explanation:

 RST 10 puts key in A.


 RST 18 displays A on LCD.

Result:
If user presses 5, LCD shows 5.

Problem 3: Beep After Key Press

Goal: Wait for any key press, then beep using RST 8.

ORG 2000H
WAIT: RST 10 ; Read key
CP 00H
JP Z, WAIT ; Loop if no key
RST 8 ; Beep
HALT

Explanation:

 Keeps scanning until a key is pressed (non-zero).


 Then plays a tone.

Result:
User presses a key → system beeps.

Problem 4: Type 3 digits and show full string

Goal: Read 3 keys, store them, then display them as a string.

ORG 2000H
LD HL, BUF
LD B, 3
LOOP: RST 10
CP 00H
JP Z, LOOP
LD (HL), A
INC HL
DJNZ LOOP
LD (HL), 00H ; Null terminator
LD HL, BUF
RST 20
RST 28
HALT

BUF: DS 4

Explanation:

 Reads 3 key presses.


 Stores in memory as a null-terminated string.
 Displays full input on LCD.

Result:
User presses 1, 2, 3 → LCD shows 123

Problem 5: Display Number from BCD in B

Goal: Display 45 using RST 30 (assumes value is in BCD).

ORG 2000H
LD B, 45H ; BCD: 4 × 10 + 5 = 45
RST 30 ; Display 45
HALT

Explanation:

 Loads B with 45H (BCD for 45).


 RST 30 converts it to ASCII and prints.

Result: LCD shows 45

Problem 6: Show "START", then Beep


ORG 2000H
LD HL, MSG
RST 20
RST 28
RST 8
HALT

MSG: DB "START", 00H

Result: LCD shows START, and a beep is heard.

Problem 7: Echo Keypad Input Forever


ORG 2000H
LOOP: RST 10
CP 00H
JP Z, LOOP
RST 18
JP LOOP

Result: Any key pressed is immediately shown on LCD.

Problem 8: Clear LCD Every Time a Key is Pressed


ORG 2000H
AGAIN: RST 10
CP 00H
JP Z, AGAIN
RST 20
JP AGAIN

Result: Every key press clears the LCD screen.

Problem 9: Input 2 keys, show both


ORG 2000H
LD HL, BUF
LD B, 2
READ: RST 10
CP 00H
JP Z, READ
LD (HL), A
INC HL
DJNZ READ
LD (HL), 00H
LD HL, BUF
RST 20
RST 28
HALT

BUF: DS 3

Result:
User presses 4 and 7 → LCD shows 47

Problem 10: Simple Password Check: 123


ORG 2000H
LD HL, BUF
LD B, 3
INLOOP: RST 10
CP 00H
JP Z, INLOOP
LD (HL), A
INC HL
DJNZ INLOOP
LD HL, BUF
LD DE, PASS
LD B, 3
CHK: LD A, (HL)
CP (DE)
JP NZ, WRONG
INC HL
INC DE
DJNZ CHK
LD HL, OK
RST 20
RST 28
HALT
WRONG: LD HL, FAIL
RST 20
RST 28
HALT

BUF: DS 3
PASS: DB '1','2','3'
OK: DB "YES", 00H
FAIL: DB "NO", 00H

Result:

 If user types 123, LCD shows YES


 Else → NO

� General Monitor and System Functions

Subroutine Address Function

MONITOR $E000 Starts the monitor program.

WARM $E003 Warm start: resets vectors and returns to monitor.

COLD $E006 Cold start: resets vectors, clears memory, reinitializes hardware.

✅ LCD Display

Subroutine Address Function

LCDCMD $E00F Send command byte to LCD (e.g., clear, home, etc.).

LCDDAT $E012 Send data byte (character) to LCD.

LCDMSG $E015 Display string message pointed to by HL.

LCDCLR $E018 Clear LCD screen.

LCDCRLF $E01B Go to next line (like newline).

LCDCUR $E01E Set LCD cursor using coordinates in B (line) and C (column).

Keypad Input

Subroutine Address Function

KEYIN $E021 Waits for and returns a key press in A register.

KEYCHR $E024 Non-blocking key check (returns 0 if no key, else ASCII in A).

KEYHEX $E027 Waits for keypress and returns hexadecimal digit (0-9, A-F).

Tone / Sound Generation

Subroutine Address Function

TONE $E02A Generates tone using frequency in BC and duration in HL.

BEEP $E02D Short beep (standard sound).


Input / Output Routines

Subroutine Address Function

INHEX $E030 Inputs hexadecimal number string and converts to binary (result in HL).

OUTHEX $E033 Outputs HL as a hexadecimal number string.

OUTDEC $E036 Outputs HL as a decimal number string.

OUTSP $E039 Outputs a single space character.

OUTCRLF $E03C Outputs carriage return and line feed (newline).

� Problem 1: Display "WELCOME" on the LCD


ORG 2000H
LD HL, MSG
CALL $E018 ; LCDCLR
CALL $E015 ; LCDMSG
HALT

MSG: DB "WELCOME", 00H

Explanation:

 $E018 clears LCD.


 $E015 displays the null-terminated string at HL.

� LCD Output: WELCOME

� Problem 2: Wait for keypress and display it


ORG 2000H
CALL $E021 ; KEYIN → result in A
CALL $E012 ; LCDDAT → display A
HALT

� LCD Output: Shows the key the user presses.

� Problem 3: Show "READY", wait for key, then beep


ORG 2000H
LD HL, MSG
CALL $E018 ; Clear LCD
CALL $E015 ; Show "READY"
CALL $E021 ; Wait for key
CALL $E02D ; Beep
HALT

MSG: DB "READY", 00H

� LCD Output: Shows READY, then beeps on keypress.

� Problem 4: Read 2 keys, then display both


ORG 2000H
CALL $E021 ; First key → A
LD (BUF), A
CALL $E021 ; Second key
LD (BUF+1), A
LD (BUF+2), 00H
LD HL, BUF
CALL $E018 ; Clear
CALL $E015 ; Display string
HALT

BUF: DS 3

� LCD Output: Displays the two keypresses (e.g., 56)

� Problem 5: Input a hexadecimal number and show it


ORG 2000H
CALL $E030 ; INHEX → HL = value
CALL $E018 ; Clear LCD
CALL $E033 ; OUTHEX (HL as hex)
HALT

⌨ User types 3A
� LCD Output: 3A

� Problem 6: Count digits typed and display count


ORG 2000H
LD B, 0 ; Counter

LOOP: CALL $E024 ; KEYCHR (non-blocking)


CP 00H
JP Z, LOOP ; Nothing pressed
INC B
CALL $E012 ; Show key
CP 0DH ; Enter key (optional to exit)
JP Z, SHOW
JP LOOP

SHOW: CALL $E018 ; Clear LCD


LD A, B
LD L, A
LD H, 0
CALL $E036 ; OUTDEC
HALT

� LCD Output: shows number of keys pressed (e.g., 5)

� Problem 7: Cursor navigation test


ORG 2000H
LD B, 1 ; Line
LD C, 5 ; Column
CALL $E01E ; LCDCUR
LD A, 'X'
CALL $E012 ; LCDDAT
HALT

� LCD Output: shows X at line 1, column 5

� Problem 8: Display a hex number and beep


ORG 2000H
LD HL, $3F2A ; Arbitrary number
CALL $E033 ; OUTHEX
CALL $E02D ; Beep
HALT

� LCD Output: 3F2A, followed by a beep.


� Problem 9: Use TONE (set frequency and duration)
ORG 2000H
LD BC, 1000 ; Frequency
LD HL, 500 ; Duration
CALL $E02A ; TONE
HALT

� Sound Output: Custom frequency tone.

� Problem 10: Input password 321, compare, and say OK or FAIL


ORG 2000H FAIL: LD HL, NO
LD HL, BUF SHOW: CALL $E018
LD B, 3 CALL $E015
READ: CALL $E021 ; Wait key HALT
LD (HL), A
INC HL BUF: DS 3
DJNZ READ PASS: DB '3','2','1'
LD HL, BUF OK: DB "OK", 0
LD DE, PASS NO: DB "FAIL", 0
LD B, 3
CHK: LD A, (HL) � Input: 3 2 1 → Shows OK
CP (DE)
JP NZ, FAIL � Else: Shows FAIL
INC HL
INC DE
DJNZ CHK
LD HL, OK
JP SHOW

You might also like