What Registers are available? Programming in Assembler. Assembler Programming - like early Basic. Assembler Data Movement Instructions

Similar documents
Microcontroller Intel [Instruction Set]

Microprocessors 1. The 8051 Instruction Set. Microprocessors 1 1. Msc. Ivan A. Escobar Broitman

Microcontroller. Instruction set of 8051

8051 Microcontroller

8051 Overview and Instruction Set

SN8F5000 Family Instruction Set


Q. Classify the instruction set of 8051 and list out the instructions in each type.

Programming of 8085 microprocessor and 8051 micro controller Study material

DR bit RISC Microcontroller. Instructions set details ver 3.10

Memory organization Programming model - Program status word - register banks - Addressing modes - instruction set Programming examples.

Dragonchip. Instruction Set Manual

Module Contents of the Module Hours COs

TUTORIAL Assembly Language programming (2)

8051 Microcontrollers

MASSEY UNIVERSITY PALMERSTON NORTH CAMPUS

Architecture & Instruction set of 8085 Microprocessor and 8051 Micro Controller

Instruction Set Of 8051

Embedded Controller Programming

Dodatak. Skup instrukcija

EEE3410 Microcontroller Applications Department of Electrical Engineering Lecture 4 The 8051 Architecture

Digital Blocks Semiconductor IP

Assembly Language programming (2)

UNIT 2 THE 8051 INSTRUCTION SET AND PROGRAMMING

UNIT-III ASSEMBLY LANGUAGE PROGRAMMING. The CPU can access data in various ways, which are called addressing modes

Digital Blocks Semiconductor IP

Digital Blocks Semiconductor IP

C51 Family. Architectural Overview of the C51 Family. Summary

CHAPTER ASSEMBLY LANGUAGE PROGRAMMING

8051 Programming using Assembly

UNIT THE 8051 INSTRUCTION SET AND PROGRAMMING

Principle and Interface Techniques of Microcontroller

Chapter Family Microcontrollers Instruction Set

Principle and Interface Techniques of Microcontroller

80C51 family programmer s guide and instruction set. 80C51 Family. PROGRAMMER S GUIDE AND INSTRUCTION SET Memory Organization. Philips Semiconductors

INSTRUCCIONES ARITMETICAS ERROR! MARCADOR NO DEFINIDO.

8051 Core Specification

ET355 Microprocessors Thursday 6:00 pm 10:20 pm

Assembly Language programming (3)

Chapter 9. Programming Framework

TUTORIAL. Donal Heffernan University of Limerick May Tutorial D.Heffernan 2000,

MCS -51 Programmer s Guide and Instruction Set

Programming Book Microcontroller Kit. Rev 3.0 January, Wichit Sirichote

8051 Microcontroller Assembly Programming

MICROPROCESSOR LABORATORY MANUAL

ELEG3924 Microprocessor

Control Transfer Instructions Jump, Loop, and Call. ECE473/573 Microprocessor System Design, Dr. Shiue

Arithmetic and Logic

8051 Programming: Arithmetic and Logic

CSIS1120A. 10. Instruction Set & Addressing Mode. CSIS1120A 10. Instruction Set & Addressing Mode 1

ELEG3923 Microprocessor Ch.2 Assembly Language Programming

ELEG3923 Microprocessor Ch.6 Arithmetic and Logics

ELEG3924 Microprocessor

ENE 334 Microprocessors

Introduction To MCS-51

~: Simple Programs in 8051 assembly language :~

Application Brief D-005

8051 Microcontroller Logical Operations. Prepared by : A. B. Modi Target Audience : 5 th Semester Students

C51 Family. C51 Family Programmer s Guide and Instruction Set. Summary

ELEG3923 Microprocessor Ch.3 Jump, Loop, and Call

8051 Instruction Set

CPEG300 Embedded System Design. Lecture 6 Interrupt System

AL8051S 8-BIT MICROCONTROLLER Application Notes

NAME as31 - An Intel 8031/8051 assembler. SYNOPSIS as31 [-h] [-l] [-s] [-v] [-Aarg] [-Ffmt] [-Ofile] infile.asm

Y51 Microcontroller. Technical Manual

8051 Single Board Monitor Programming. Minmon - Yeralan & Ahluwalia. PaulMon1 & PaulMon2 - Paul Stoffregen

ET2640 Microprocessors

Highlights. FP51 (FPGA based 1T 8051 core)

Chapter 09. Programming in Assembly

CHAPTER 3 JUMP, LOOP, AND CALL INSTRUCTIONS

CS 320. Computer Architecture Core Architecture

Assembly Language Programming of 8085

MASSEY UNIVERSITY PALMERSTON NORTH CAMPUS

8051 Microcontrollers

System & Program Developments of 8051

Introduction to uc & Embedded Systems

Lecture 5. EEE3410 Microcontroller Applications Department of Electrical Engineering Assembly Language Programming (1)

MICROPROCESSOR & MICROCONTROLLER

Assembly Language Programming of 8085

CPEG300 Embedded System Design. Lecture 3 Memory

Assembly Language programming (1)

Chapter 3. Bit Addressable Area. By DeccanRobots

(2) Explain the addressing mode of OR What do you mean by addressing mode? Explain diff. addressing mode for 8085 with examples.

The 8051 Microcontroller and Embedded Systems

CPU: SOFTWARE ARCHITECTURE INSTRUCTION SET (PART

Microcontroller and Applications

EXPERIMENT NO. 1 THE MKT 8085 MICROPROCESSOR TRAINER

JUMP, LOOP AND CALL INSTRUCTIONS

SOEN228, Winter Revision 1.2 Date: October 25,

Legacy documentation refer to the Altium Wiki for current information. TSK52x MCU

Legacy documentation refer to the Altium Wiki for current information. TSK51x MCU

UNIT MICROCONTROLLER AND ITS PROGRAMMING

Instruction Sets: Characteristics and Functions Addressing Modes

TYPES OF INTERRUPTS: -

MAHALAKSHMI ENGINEERING COLLEGE TIRUCHIRAPALLI

Stored Program Concept. Instructions: Characteristics of Instruction Set. Architecture Specification. Example of multiple operands

VARDHAMAN COLLEGE OF ENGINEERING (AUTONOMOUS) Shamshabad, Hyderabad

Application Brief D-002

MODULE-1. Short Answer Questions

ET355 Microprocessors Friday 6:00 pm 10:20 pm

Transcription:

Programming in Assembler Need knowledge of CPU 8051 Programmers model what registers are available? what memory is available? code memory (for programs) data memory (for variables and the stack) what instructions are available Programming in assembler involves mapping problem onto these resources select instructions that will have desired effect not all desired instructions may be available not all addressing modes may not be available Assembler Programming not so much difficult as detailed complexity is caused by the multiplicity of choice you have to worry about much more than with HLLs What Registers are available? Name Bits Comments Acc 7-0 All arithmetic, comparisons, logic B 7-0 temporary, used for Multiply & Divide R0 7-0 scratchpad, can be used as pointer R1 7-0 scratchpad, can be used as pointer R2 7-0 scratchpad R3 7-0 scratchpad R4 7-0 scratchpad R5 7-0 scratchpad R6 7-0 scratchpad R7 7-0 scratchpad Sp 7-0 Stack pointer to Internal data memory PSW 7-0 Processor Status Word DPTR 15-0 Data Pointer Register, only 16 bit register, used as pointer to code memory C 1 Carry bit, used for multi-yte add/sub Constraints Accumulator (Acc the A register) must be used for arithmetic & logic operations: add a,#100 is OK (acc:= acc + 100) add r1,#100 is NOT (can't add to r1) - only r0 & r1 can be used as pointers to internal memory - only B can be used as second argument for MUL & DIV - r0 - r7, are the same as Memory locations 0 to 7 Familiarity with the Instruction Set is Critical! Assembler Programming 1 Copyright 2006 Giovanni Moretti Assembler Programming 2 Copyright 2006 Giovanni Moretti Assembler Programming - like early Basic The early versions of the Basic programming language had the following constructs: LET variable = expression GOTO line # IF condition GOTO line # INPUT variable PRINT variable string GOSUB line # RETURN Where: <condition> ::= <expression> <rel-op> <expression> <rel-op> ::= < <= = # >= > <expression>::= <value> <operator> <value> <value> ::= variable number <operator> ::= + - * / What's Missing? No support for subroutines with parameters, while/repeat loops, grouped statements. these higher level constructs must be synthesised. Assembler Data Movement Instructions Let write an assembler program to mimic: var X, Y : byte; Y := Y + X; Decide on Memory Locations for Variables variables we need to store them somewhere their actual memory location doesn't matter 8051 only has 128 bytes of memory Arbitrarily, let's use location 20 for X and 30 for Y mov A,30 ; Move Y to the accumulator adda 20 ; Acc' now contains Y+X mov 30,A ; Put total back into Y Notice: the MOV instruction move Right to Left the arguments are simple memory addresses Using Symbolic Names This is hard to read, So define constants for the memory adr X equ $20 Y equ $30 mov a,y ; Move Y to the accumulator adda X ; Acc' now contains Y+X mov Y,a ; Put total back into Y Assembler Programming 3 Copyright 2006 Giovanni Moretti Assembler Programming 4 Copyright 2006 Giovanni Moretti

Assembler Flow-of-Control Instructions Execution order of instructions Instructions are normally executed in order the Program Counter (PC) is incremented after each instruction is fetched. Altering the execution order can be done: using an UNCONDITIONAL JUMP (sjmp/ljmp) these simply load a new value into the PC ljmp 200 ; Set PC to 200 using a CONDITIONAL JUMP these test a value and depending on the result, MAY ALTER the PC if condition fails, the conditional jump has no effect jz 200 ; JumpIf Zero to 200 ; Jump to 200 if Acc = Zero There are many different conditional jumps (sometimes called Branches) eg, jump-if-zero, jump-if-not-zero, jump-if-carry-set General form of Assembler Source ; Fill locations 50-70h with zero ORG 20h Start: mov sp,#127 mov r1,#50h mov A,#0 ; Setup Stack lup: mov @r1,a ; mem[r1] = 0 inc r1 ; r0++ cjne r1,#71h,lup ; again? Wait: sjmp wait Points to Note labels start in column 1 instructions NEVER start in Column 1 ; Hang here instructions have varying number of parameters ; indicates a comment remainder of line is ignored some number are hexadecimal if followed by h (eg 30h) or preceded with $ (eg $30) ORG defines where next byte is placed in memory but doesn t generate any code EQU is a constant definition (can t be redefined) zero EQU 0 MOV A,#zero Assembler Programming 5 Copyright 2006 Giovanni Moretti Assembler Programming 6 Copyright 2006 Giovanni Moretti Well Commented Assembler ; PUTCH() - Send the character in acc to the Serial Port ;--------------------------------------------------------------------- putch: clr ti ; Clear "Char Been Sent" Bit mov sbuf,a ; Store character to Output gone: jnb ti,gone ; Wait until it's gone ret ; return ;**************************************************** ; NEWLINE() - send a RETURN & LINEFEED to the display ; newline: mov a,#13 ; RETURN character in Acc lcall putch mov a,#10 ; Linefeed character lcall putch ret ;**************************************************** ; PRINT a string ;------------------------------------------------------------------------------ ; Print a string ending in a zero byte to the display (serial port). ; ; Parameters : r1 - the address of the first byte of the string ; Return Values : none ; Regs changed : acc, R1 ;------------------------------------------------------------------------------ ; repeat print: mov a,@r1 ; acc = *r1 // Get a byte jz done ; if acc == 0 then break; lcall putch ; putch(acc) - Send it to serial port inc r1 ; r1:= r1+1 // point r1 at next char sjmp print ; until false // do next char done: ret Input Syntax (Structure) A grammar is needed to discuss the form (the structure) of a language, such as assembler. This is itself a language being used to describe a language. The grammar rules below are modelled on Extended BNF (Backus-Naur Form): a Grammar consists of a series of Productions each Production defines a possible combination of Productions and Terminal Symbols Productions are surrounded by < > and are defined elsewhere. Terminal Symbols indicate actual symbols/characters. The following symbols have a special meaning: <XXX> this a placeholder for a production called XXX which is defined elsewhere. Whatever matches the production <XXX> maybe inserted instead of <XXX>. [ OBJ ] OBJ is optional it may occur 0 or one time { OBJ } OBJ may be present zero or more times ::= is read as is defined as means OR Recursion in the Productions is permitted Assembler Programming 7 Copyright 2006 Giovanni Moretti Assembler Programming 8 Copyright 2006 Giovanni Moretti

Lexical and Syntactic Structure The following rules specify structure of an assembler source file but don t specific the exact makeup of the symbols that may be used for identifiers, operators inside expressions. The definition of the actual legal (permitted symbols) are defined elsewhere is the Lexical Grammar. <Assembler file> ::= { <assembler line> } <assembler line> ::= [ <label> ] [<mnemonic> [<arg-list>]] [<comment>] <label> <mnemonic> ::= <identifier> ::= one of defined opcodes <arg-list> ::= <argument> {, <argument> } <argument> ::= #<expression> <expression> Rn A <expression> <comment> ::= something returning a numeric value ::= ; rest of the line Assembler Programming 9 Copyright 2006 Giovanni Moretti Assembler vs High Level Languages High Level Languages Abstraction is easy Multiple Data types char, int, double, string Structured data types (arrays, structs) Algorithmic Constructs IF - THEN ELSE WHILE - DO Switch statements Functions with parameters/return values Object-oriented programming Not machine-specific can move (relatively) easily Control & Responsibility No control over allocation of variables No control over assembler constructs used Summary Good for rapid development of larger systems when program size and execution speed aren t constraints Assembler No Abstractions - initially Only those supported on underlying Hardware None must by synthesized usually with pointers None, only: JUMP Conditional Jumps Only Subroutine calls - no parameters - no return values --- Move to different architecture is very timeconsuming Programmer is responsible for all variable allocation Programmer must select specific instructions Good when max speed or min size is required Not good for large systems -too much detail Assembler Programming 10 Copyright 2006 Giovanni Moretti Assembler Overview Processors need: instructions data Both of these are located at a specific address in memory instructions are decoded from bit patterns (patterns of bits have predefined meaning) meaning of the bits is arbitrary but defined by the CPU designer To generate an instruction, three pieces of information are needed: the actual instruction to be executed the values of any arguments where in memory to place the instruction All of these must be made available to the assembler Differing types of Operands The data being moved by a MOV opcode may be: a constant value currently in a memory location in a memory location whose address can be calculated in a named register (eg A (accumulator) B, r0-r7, SP which must be indicated unambiguously Assembler Programming 11 Copyright 2006 Giovanni Moretti Addressing Mode Indicators The differing types of operands must be uniquely identifiable as: variables aren't typed a variable can be either data or an address differing arguments are encoded differently mov a,r1 instruction can be one byte long mov a,217 needs an extra byte for the address Indicating the Types of Arguments The different types of arguments must be distinctive. Symbol A Rn a number @Ri #data #data16 Meaning the Accumulator register one of R0 R7 Direct - Internal Memory 8 bit address Internal ram 0-255 via R0/ R1 a 8 bit value a 16 bit value relative signed offset (one byte) 128 to +127 bit Directly addressable bit in Data Memory addr11 11 bit destination ( acall & ajmp ) addr16 16 bit destination (lcall & ljmp) can be any byte in 64K program memory Assembler Programming 12 Copyright 2006 Giovanni Moretti

Assembler Instructions LAYOUT MATTERS - instructions are written one per line e.g: MOV <dest-byte>,<src-byte> Address Contents 30h 47h or $47 47h 15h or $15 After Instruction Completes mov R0,30 ; R0 = 47h mov R0,#30h ; R0 = 30h mov A,@r0 ; A = 47h mov $47,#255 ; 47H = 255 mov $47,#$FF ; 47H = 255 Parameter Types value Direct address value is memory address mov 30,40 ; mem[30] = mem[40] #value R0 or A @R0 Immediate data value is used (no reference to mem) mov 30,#0 ; mem[30] = 0 Register Register is used as source/destination mov r0,#15 Indirect contents of register are used as adr mov @r0, A ; mem[15] = A What s needed to Assemble a Program? ==> A list of all the possible instructions variants This is NOT a list like: mov, sjmp, cjne, inc but a list that showing: each possible address data type variation (bit, byte, 16 bits) applicable opcode mov direct,a 1 st byte 2 nd byte 3 rd byte 1111 0101 direct address mov direct,direct 1 st byte 2 nd byte 3 rd byte 1000 0101 Direct-adr (src) direct-adr (dest) mov direct,@ri 1st byte 2 nd byte 3 rd byte 1000 011i direct address mov direct,#data 1 st byte 2 nd byte 3 rd byte 0111 0101 direct address data-value Eg mov 100,#22 would be encoded as 75 64 16 Assembler Programming 13 Copyright 2006 Giovanni Moretti Assembler Programming 14 Copyright 2006 Giovanni Moretti Building Instructions e.g. MOV opcode Opcodes need to encode all the information for one instruction, such as: which Operation (mov, incr, jmp ) arguments (address of source and/or destination) any constant value The Size of the Instructions Vary Instructions are designed to be as compact as possible Why? mov direct,direct - at least three bytes long why? Rn Ri dir is one of R0 - R7 is one of R0 or R1 is a direct (one byte) address F4 mov, A, Rn, 1 1110 1RRR F5 mov, A, direct, 2 1110 0101,dir F6 mov, A, @Ri, 1 1110 011i Opcode Size The sequence of bytes for one opcode may be a merging of an opcode template with information from the arguments: Hardware-supported Instructions Data Manipulation Instructions Move primitive data types - bits, bytes, words - move reg-to-reg, reg to/from memory Clear Registers or memory Set (make all ones) Registers or memory Increment & Decrement Regs/memory Arithmetic and Logical Instructions These work on bytes, words (& sometimes bit) Add, subtract, sometimes multiply, less frequently AND, OR, NOT (called Complement), XOR Flow of Control Instructions JUMP like a GOTO JUMP if a condition is TRUE Call a subroutine (& remember how to get back) Looping Control Fixed count - Decrement & Jump if not zero DJNZ Compare and Jump if not equal CJNE eg RRR is replaced with 000-111 representing R0-R7, i is replaced with 0 or 1 representing R0 or R1 Assembler Programming 15 Copyright 2006 Giovanni Moretti Assembler Programming 16 Copyright 2006 Giovanni Moretti

Hardware-supported Instructions Status Manipulation Instructions Allow / disallow interrupts Set up Stack Pointer Comparisons on primitive data types & set flags Negative, Zero, Carry Advanced Features String support move, comparisons Floating point arithmetic These aren't supported at all if the processor is to work on more complex data types, (eg strings or floating point numbers), subroutines must be written to implement the operations. Programmer s Model Detail The processor has several sets of: Programmer registers I/O control registers Status registers I/O ports ALL of which as accessed as though they were memory Program Memory (Code) Address 07FFFH (8051) Serial Port Timer 1 Ext Interrupt 1 Timer 0 Ext Interrupt 0 Reset 07FFH (89C2051) 0023H 001BH 0013H 000BH 0003H 0000H Interrupt Vectors (location jumped to on Interrupt) Bank Select Bits in PSW 11 { 10 { 01 { 00 { Internal Memory (Data) Directly Addressable Bits 0-7F RB3* RB2* RB1* RB0* * Four banks of Registers addressable as R0-R7 7FH 2FH 20H 1FH 18H 17H 10H 0FH 08H 07H 00H Reset value of Stack Pointer Program (CODE) memory Data Memory (R/W) Many memory locations have predefined alternate uses Assembler Programming 17 Copyright 2006 Giovanni Moretti Assembler Programming 18 Copyright 2006 Giovanni Moretti Programmer-accessible Registers Accumulator Acc at E0H This is used for: Arithmetic ops - ADD, ADDC, SUBB, MUL, DIV logical operations - ANL, ORL, XRL, CPL Rotates - RL, RLC, RR, RRC SWAP - Swap top/lower four bits B Register at F0H The is an auxilary register used primarily in MUL & DIV instructions. Can be used at temp location otherwise Register Bank R0 to R7 These are (usually) the first 8 locations in internal memory (0H 7H): Treated as special only need 3 bits to specify them Many instructions have one of R0-R7 as source or dest R0 and R1 are special Have an INDIRECT addressing mode can be used as pointer registers DPTR Data Pointer Register the only 16 bit register used as pointer to CODE memory can be accessed as two 8 bit regs DPL & DPH Assembler Programming 19 Copyright 2006 Giovanni Moretti Processor Stack The 8051 supports a stack by having a register (SP) that auto-increments when used in a PUSH instruction auto-decrements when used in a POP instruction Where's the Stack Stored? The stack itself is stored in data memory wherever SP location happens to be pointing make sure that the memory used for the stack is free! Stack Pointer SP at 81H A processor manipulated register used to point to last byte stacked it's incremented BEFORE a new byte is stacked. Stack-oriented Instructions PUSH Acc SP = SP + 1 Data Mem(SP) = Acc POP Acc Acc = Data Mem [SP] SP = SP 1 Push the acc onto the stack Pop the top-of-stack into Acc Assembler Programming 20 Copyright 2006 Giovanni Moretti

Support for Subroutine Calls The stack is used to store the return address during a subroutine call. before jumping to a subroutine the address to return to is stacked (done by LCALL) at the end of the subroutine, the return opcode (RET) pops this address and puts it back into PC LCALL sub-adr Call a subroutine PC = PC + 3 ; point at next instr' SP = SP + 1 DataMem[SP] = PC (bits 0-7) ; stack LO part of PC SP = SP + 1 DataMem[SP] = PC (bits 8-15) ; stack HI part of PC PC = sub-adr Status Register PSW at D0H The Processor Status Word register (PSW) is a register that holds: status information from the result of previous instructions (eg did a carry occur?) some configuration bits (RS1 & RS0) see next slide CY AC F0 RS1 RS0 OV PSW1 P Carry Aux Flag Register Overflow Spare Parity Bit Carry - Bank Select Set if a carry from Acc.7 free for user 0=> RB0 1=> RB1 2=> RB2 3=> RB3 Set by arithmetic Ops if SIGNED Integer Overflow free for user Set to 1 if acc contains ODD no of 1 Bits RET Return from a subroutine causes the address to return to be popped from stack into the program counter PC(bit 8-15) = DataMem[SP] ; Pop return adr HI SP = SP 1 ; cut stack back PC(bit 0-7) = DataMem[SP] ; Pop return adr LO SP = SP 1 ; cut stack back Assembler Programming 21 Copyright 2006 Giovanni Moretti Assembler Programming 22 Copyright 2006 Giovanni Moretti Register Bank Location By default, R0 is located in memory location 0, R1 in 1 This can be altered by the user to three alternate locations RB1 Register Bank 0 RB1 Register Bank 2 PSW RS1&0 = 00 PSW RS1&0 = 10 R0 Data RAM 0 R0 Data RAM 10H 16 R1 Data RAM 1 R1 Data RAM 11H 17 R2 Data RAM 2 R2 Data RAM 12H 18 R7 Data RAM 7 R7 Data RAM 17H 23 RB1 Register Bank 1 RB1 Register Bank 3 PSW RS1&0 = 01 PSW RS1&0 = 11 R0 Data RAM 8 R0 Data RAM 18H 24 R1 Data RAM 9 R1 Data RAM 19H 25 R2 Data RAM AH (10) R2 Data RAM 1AH 26 R7 Data RAM FH (15) R7 Data RAM 1FH 31 Changing Register Banks Need to alter RS1 and RS0 in the Processor Status Word e.g. to move to register Bank 3 OR mov acc,psw orl #18h mov psw,acc Setb PSW.4 Setb PSW.3 Get PSW merge in RS1 & RS0 Put result back Set RS1 and RS0 In this paper: there will be no need to use alternate register banks but you should be familiar with the underlying idea. The use or not of the register bank is the programmers responsibility: If the memory locations aren t used for RB1-3 they can be used for other purposes The primary reason for Register Banks is a fast Context Switch a complete swap and therefore preservation of the contents of R0 R7 with two instructions (can be useful with interrupts) Assembler Programming 23 Copyright 2006 Giovanni Moretti Assembler Programming 24 Copyright 2006 Giovanni Moretti

Special Function Registers The registers for controlling the I/O devices are mapped so as to appear as locations in internal (data) memory they appear between locations 80H and FFH Bank Select Bits in PSW 11 { umped rupt) 10 { 01 { 00 { Internal Memory (Data) Special Function Registers Directly Addressable Bits 0-7F RB3* RB2* RB1* RB0* * Four banks of Registers addressable as R0-R7 Several common registers are: Acc E0H B register F0H PSW D0H IE E8H 7FH 2FH 20H 1FH 18H 17H 10H 0FH 08H 07H 00H Reset value of Stack Pointer Port 0 Port 1 Port 2 Port 3 80H 90H A0H B0H These registers are BIT Addressable: you can use SetB, ClrB, JB, JNB, CPL their bits e.g: cpl p3.7 FFH Program Memory Map Bank Select Bits in PSW 11 { umped rupt) 10 { 01 { 00 { Internal Memory (Data) Special Function Registers Directly Addressable Bits 0-7F RB3* RB2* RB1* RB0* * Four banks of Registers addressable as R0-R7 7FH 2FH 20H 1FH 18H 17H 10H 0FH 08H 07H 00H Reset value of Stack Pointer The internal data memory can be used in different ways: FFH as register banks as stack space as directly addressable bits for user variables It s necessary to decide: which of these is actually necessary where in memory it s going to be located Assembler Programming 25 Copyright 2006 Giovanni Moretti Assembler Programming 26 Copyright 2006 Giovanni Moretti What goes where in Memory? There are few constraints with assembler programming but some of them are absolute: the first instruction to be executed is that at address ZERO of PROGRAM memory this is called the Reset Address Your program must start at address zero there is ONLY 128 bytes of Data Memory for: Sample Memory Layout #1 few subroutine calls will be made little stack space required (2 bytes/call) bit addressable registers not required don t use memory locations 0-7 (used for r0 to r7) Data Memory SP 127 110 Stack 110 registers r0 r7 (usually in locations 0 to 7) the stack any variables you need Locations 8-127 must be shared between variables and the stack. The Stack is used by subroutine calls so SP MUST NOT point at data memory that contains variables. How much Stack Space should be allocated? the primary use of the stack is to store subroutine return addresses two bytes per NESTED subroutine call (call from inside a subroutine) For this paper, a 16-24 bytes of stack space will be plenty! ; Can use locations 8 to 110 for variables ; Define Data memory locations to be used for variables X equ 8 Y equ 9 Registers R0 R7 org 0 ; Start program at 0 Start mov sp, #110 ; setup Stack at 110 your instructions goes here! stop sjmp stop ; make program stops 7 0 Assembler Programming 27 Copyright 2006 Giovanni Moretti Assembler Programming 28 Copyright 2006 Giovanni Moretti

Sample Memory Layout #2 few subroutine calls will be made little stack space required (2 bytes/call) bit addressable registers not required don t use memory locations 0-7 (used for r0 to r7) SP 110 Data Memory Variables 127 Stack Registers R0 R7 32 31 8 7 0 ; Stack starts at 8, can use up to location 31 ; Can use locations 32 to 127 for variables ; Define Data memory locations to be used for variables X equ 32 Y equ 33 org 0 ; Start program at 0 start mov sp, #7 ; Setup Stack at 7 your instructions go in here! stop sjmp stop ; make program stops Assembler Programming 29 Copyright 2006 Giovanni Moretti