Chapter 3: Program Analysis

In this chapter you will learn about the ASM syntax and a few cpu instructions. It is important to be able to break down someone else's code in order to know how to use it in your project.

3.1 What a program looks like

This is a simple code example:

vector reset = 1000h
org 1000h

	jp main


vector reset = 1000h
Set the reset vector to address 1000h. The reset vector is located in rom at address 02h. It is two bytes large. The cpu will jump to address 1000h on reset because we set the reset vector.

org 1000h
Tell the assembler to put the program at rom location 1000h. This is exactely where the reset vector points to!

This is a label. When you put a label somewhere the assembler knows it should remember the address it is at.

nop (4x)
NOP is a 2 cycle instruction. It is useful for delays in cycle accurate coding, but here I just use it as dummy code.

This tells the on-chip debugger to make the cpu stop executing code. The program counter stops counting and you can read out register values using one of the debug windows in the IDE. Notice that the program counter is at the next instruction already, and not at the break instruction.

jp main
Jump back to label main.

This program will execute NOP instructions until it reaches the BRK instruction. Continue from here and execute two more NOP instructions. When it gets to the JP instruction it branches to label main, thus it will loop forever.

3.2 How to run this code

If you did not create a new project yet read tutorial 1 again.

Now you run the code on the target device which is a simulator in this case. Go to the Debug menu and click Go (or hit F5.)

When you are in debug mode there is an extra set of buttons in the menu. These are for various debug features. debug menu

You will notice the program stopped at the BRK instruction. That is normal behavior. It is an instruction that stops the cpu and puts it in debug mode so you can read from registers and more.
To resume the program you press the Go button in the debug menu.
When in debug mode you can also step trough the program instruction by instruction by using the Step Into debug feature. The button looks like two curly brackets { }. This feature is helpful when you are trying to find out how the program works or if you want to find out what is going wrong if you encountered a bug.

When you are confident your code runs fine in the simulator you can head over to the project settings and set the target device to your debug cable which connects to your eZ8 microcontroller.
Go to the debug menu and Connect to Target. Then press Download Code. This will send the assembled code to the target device.
You can still use the debug features just like with the simulator because of the on-chip debugger.

If you are still experiencing technical difficulties you should check out one of the following links.
Chapter 1: Getting started. - Learn where to get the IDE and how to set up your first project.
Reference - for most features of the eZ8 encore XP mcu's (work in progress)

"But, but, but... This does nothing interesting!" True, but it is the basic structure for a program.

3.3 LED blink example

This code example will put a binary number on GPIO port B. Which means: when you connect an LED to pin B1 for example you should see it blinking. Before you try it on your precious microcontroller test it with the Simulator to see if it works.
You have to wire up the LED like this:
3.3V --[resistor]--[led>|-- [ I/O pin on eZ8
This means that the LED lights up whenever the pin is in its low state.

include ""

vector reset = 1000h
org 1000h

	di			;Disable interrupts
	srp #00h		;Set working registers base address (Working registers are R0-R15)
	ldx SPH,#00h		;Set stack pointer base address
	ldx SPL,#FFh
	ldx PAADDR, #01h	;Access the data direction subregister
	ldx PACTL,#00h		;Set data direction of port B to all outputs. 0 = output, 1 = input
	ld  R4,	#00h		;Load register R0 with 00. This will be our counter.
	ldx PAOUT, R4		;Load the output register of port B with the value of our counter.
	inc R4			;Increase our counter by 1.	
	call delay		;Call our delay function.
	jp loop			;Jump back to loop
	ld R0,#0Fh		;Load value 0FFFh in the 16 bit register pair RR0 (consists of R0 and R1)
	ld R1,#FFh
	ld R2,#FFh		;Load value 0FFh in register R2

	djnz R2,$		;Decrement counter and jump back if it is not zero
	decw RR0
	jp nz, outer		;While the register pair has not reached 0 (nz) yet jump back to outer
	ret			;Return from routine


Try reading some of the comments (preferably all of them) and you will understand how this code works.
- I cannot see the led blink.
A: Checked the pinout and did you wire it correctly? Also: Try different pins on the same port such as pin B0. It might blink super slow on the high bits.

- Why do you push R0 and R1 on the stack and what is a stack anyway?
A: A stack is a pile of numbers. If you want to use a register for something else you push the current value on the stack so when you are done with the register you can restore its original value by popping it off the stack back into the register. Make sure to do it in order. The last thing you pushed on the stack is on top of the stack, so you pop it off first.

- What is the difference between ld and ldx?
A: LDX is for extended addressing. The address of the port registers are 12 bit so you need to use LDX instead of LD. For register R0 and R1 in this example I can just use LD since they are within range of 4 bit addressing (working registers r0-r15).

- How does that delay function work?
A: There is a loop inside of a loop. Each iteration of the outer loop contains many iterations of the inner loop.

- Why do you use DECW instead of DEC?
A: DECW is the 16 bit equivalent of DEC

For more information go to the reference: Port Access →

Please submit corrections, suggestions, and new documentation here: Submit a ticket
← Return to Tutorial Index
This is not an official Zilog website. Zilog nor 8times8 are liable for any damage done to equipment. Tutorial and reference copyright ©8times8 Creative Commons CC-0 License (public domain.) This reference uses content from Zilog's technical datasheets. Fair use only. Datasheets copyright © 2013 Zilog®, Inc. Z8, Z8 Encore!, Z8 Encore! XP and Z8 Encore! MC are trademarks or registered trademarks of Zilog, Inc. Read Zilog's terms at

Website design by Koen van Vliet. Hosted by