18.11.2014 Views

The Microcontroller Idea Book - Jan Axelson's Lakeview Research

The Microcontroller Idea Book - Jan Axelson's Lakeview Research

The Microcontroller Idea Book - Jan Axelson's Lakeview Research

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Calling Assembly-language Routines<br />

Avoiding Program Crashes<br />

It’s very easy to write an assembly-language program that crashes the system and forces you<br />

to reboot. To prevent this, you have to take care that your routines do not interfere with each<br />

other, or with BASIC-52. Remember that BASIC-52 is a program in itself, and it uses many<br />

of the registers and other memory locations, both inside and outside of the 8052, for its own<br />

purposes.<br />

For example, BASIC-52 uses locations 13h and 14h in internal RAM to store the starting<br />

address of the current BASIC program in external RAM. If you overwrite these values,<br />

BASIC-52 will no longer be able to find your program.<br />

<strong>The</strong> BASIC-52 programming manuals list the registers and other memory addresses used<br />

by BASIC. In general, you should avoid writing to these locations, unless you know what<br />

you’re doing and how to deal with the results.<br />

Often, an assembly-language routine will alter some of the 8052’s registers. You are<br />

responsible for seeing that all critical values are unchanged when the routine returns control<br />

to the program that called it, whether it’s BASIC-52 or another assembly-language program.<br />

<strong>The</strong> stack is a convenient way to preserve values on entering a routine, and to restore them<br />

on exiting. <strong>The</strong> stack is a special area of memory with a last-in, first-out structure, which<br />

means that you read values from the stack in the reverse order that you wrote them. Storing<br />

values in the stack area is called pushing, or placing, values on the stack. Retrieving values<br />

from the stack area is called popping them off the stack.<br />

Assembly language has push and pop instructions for accessing the stack. (BASIC-52’s<br />

PUSH and POP instructions access a separate area called the argument stack.)<br />

You can also preserve values by selecting a unique register bank for use by a routine. <strong>The</strong><br />

8052 has 32 registers arranged in four banks of eight, from 0 to 1Fh in internal data memory.<br />

You can access the registers by specifying the address, or by selecting a register bank and<br />

specifying a register from R0 to R7 within the bank. For example, if bank 0 is selected, R0<br />

is location 00h, but if bank 1 is selected, R0 is location 08h.<br />

BASIC-52 uses banks 0, 1, and 2, but uses bank 3 only with the PGM instruction, so this<br />

bank is usually free for other uses. Bits 3 and 4 of the 8052’s program status word (psw)<br />

select the register bank. When you call an assembly-language routine, BASIC-52 automatically<br />

selects register bank 0. To select bank 3, add this to the beginning of your routine:<br />

push psw ;save program status word<br />

orl psw,#18h ;select register bank 3<br />

<strong>The</strong> <strong>Microcontroller</strong> <strong>Idea</strong> <strong>Book</strong> 231

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!