Download presentation
Presentation is loading. Please wait.
1
Mon Sept. 11 Announcements
Lab should be open 24/7 starting tomorrow Lab/HW 4 will be posted tomorrow Code submission will be done on BB this year. Please submit Lab 2 code ASAP – Lab 3 (and future labs) are due by the end of the day for your lab period.
2
Assembly Parameter Passing
Module 1-F Assembly Parameter Passing Tim Rogers 2017
3
Learning Outcome #1 Architecture and Programming Model
“An ability to program a microcontroller to perform various tasks” Architecture and Programming Model Instruction Set Overview Assembly Control Structures Control Structure Applications Table Lookup Parameter Passing Macros and Structured Programming How?
4
Objective Why? Ultimately, they all turn into assembly
“Assembly Parameter Passing” Why? Almost any language you can name implements function calls Passing parameters to functions is universal N equ ? org $800 macme pshb ; (2) pshx ; (2) pshy ; (2) ldx #XA ; (2) ldy #YA ; (2) movw #0,ACM ; (5) movw #0,ACM+2 ; (5) ldab #N ; (1) loop emacs ACM ; (13) leax 2,x ; (2) leay 2,y ; (2) dbne b,loop ; (3) puly ; (3) pulx ; (3) pulb ; (3) rts ; (5) ACM rmb 4 XA fdb ?,?,? YA fdb ?,?,? Ultimately, they all turn into assembly
5
4 Basic Methods Call by value (aka via registers)
Fastest Call by value (aka via registers) Call by name (aka via pointers to global memory) Embed Parameters following the “call” Via the stack Weirdest Most General
6
Method 1: Call by Value (Registers)
Easiest to use used # parameters is small Input: (A) = <packed BCD number> bcdb Output: Unchanged = converted binary Example: Convert packed BCD to binary
7
Method 1: Call by Value (Registers)
Example calling code ldaa #99 jsr bcdb ; use b for something
8
Method 2: Call by Name (Global Area)
Uses a CPU index register as a pointer to the beginning of a global parameter area Where used: pass a character string pass a data structure (e.g., an array) Input: (X) = <pointer to start of array> sortem Output: (X) Unchanged Array in memory sorted Example: Sort an array in memory
9
Method 2: Call by Name (Global Area)
Global Memory Example calling code ldx #AA jsr sotem ; use AA for something
10
Method 3: Following “Call” Instruction
Use the “return address” as the pointer to the data. Effectively get a private parameter area Function MUST correct the return address before RTS Example: Print a string Input: ( ((SP)):((SP)+1) ) = <first byte of input string> pmsg Output: ((SP)):((SP)+1) = <correct return addr> Recall: JSR puts the address after the JSR instruction on the stack
11
Method 3: Following “Call” Instruction
desired return address return address placed on stack points here
12
Method 4: Using the Stack
The system stack is the technique most commonly used by high-level language compilers While this method has a significant amount of “overhead” associated with it (in terms of the complexity of the calling and exit sequences), it makes possible two important features associated with modern high-level languages: recursion (the ability of a subroutine to call itself) reentrancy (the ability of a code module to be shared among quasi-simultaneously executing tasks) Basically means you can take an interrupt, call the function in the interrupt and everything works
13
Method 4: Using the Stack
Input: (SP)+2 = <pointer to data structure> epadds Output: (SP) = <pointer to return data structure> Example: Extended precision binary add
14
Method 4: Using the Stack
Note: Here, data is stored in high order byte first format (also referred to as “big endian”)
15
Method 4: Using the Stack
16
Method 4: Using the Stack
17
Wed Sept. 13 Announcements
In-Class Quiz Friday Sept 15 Covers everything between last quiz and where we get today
18
A detailed example of each method
Convert packed-BCD to binary Call by value (aka via registers) Call by name (aka via pointers to global memory) Embed Parameters following the “call” Via the stack
19
Method 1: Call by Value (Registers) Example
Input in (A) Output to (B) conv binary = (10 x u.n.) + l.n. “BAB” (B) (A) + (B)
20
A detailed example of each method
Call by value (aka via registers) Call by name (aka via pointers to global memory) Embed Parameters following the “call” Via the stack Sort array
21
Array Sorting Example Method: “Bubble Sort”
use pairwise comparison and exchange use X register to point to AA[i] use Y register to point to AA[i+1]
22
Global Memory X initialize X and Y pointers Note: B J
23
A detailed example of each method
Call by value (aka via registers) Call by name (aka via pointers to global memory) Embed Parameters following the “call” Via the stack Print Message
24
Method 3: Following “Call” Instruction
25
A detailed example of each method
Call by value (aka via registers) Call by name (aka via pointers to global memory) Embed Parameters following the “call” Via the stack Extended Precision Add
26
Method 4: Using the Stack
27
Stack State at Entry to “epadds”
SP
28
Stack State at Entry to “epadds”
MSB augend MSB addend #N #N #N SP
29
Stack State at Entry to “epadds”
LSB augend LSB addend ••• MSB augend MSB addend #N SP #N
30
Stack State at Entry to “epadds”
LSB augend LSB addend ••• MSB augend MSB addend #N SP #N
31
Stack State at Entry to “epadds”
MSB return addr LSB return addr N LSB augend LSB addend ••• MSB augend MSB addend SP #N #N
32
Stack State as “epadds” Runs
MSB return addr LSB return addr N LSB augend LSB addend ••• MSB augend MSB addend SP X X* *after post-increment by 1
33
Stack State as “epadds” Runs
MSB return addr LSB return addr N LSB augend LSB addend ••• MSB augend MSB addend SP X
34
Stack State as “epadds” Runs
MSB return addr LSB return addr N LSB result LSB addend ••• MSB augend MSB addend SP X X** **after post-increment by 2
35
Stack State Just Before “epadds” Returns
MSB return addr LSB return addr N LSB result LSB addend ••• MSB result MSB addend SP X X** **after post-increment by 2
36
Exit Sequence Following Return From “epadds”
MSB return addr LSB return addr N LSB result LSB addend ••• MSB result MSB addend SP SP* *after post-increment by 1
37
Exit Sequence Following Return From “epadds”
MSB return addr LSB return addr N LSB result LSB addend ••• MSB result MSB addend SP SP** **after post-increment by 2
38
Exit Sequence Following Return From “epadds”
MSB return addr LSB return addr N LSB result LSB addend ••• MSB result MSB addend SP SP** **after post-increment by 2
39
Exit Sequence Following Return From “epadds”
MSB return addr LSB return addr N LSB result LSB addend ••• MSB result MSB addend SP SP**
40
Exit Sequence Following Return From “epadds”
MSB return addr LSB return addr N LSB result LSB addend ••• MSB result MSB addend SP
41
Interrupts: Basics Special return from interrupt instruction
Normally, CPU executes your program. PC is controlled by your program. N equ ? org $800 main pshb ; (2) pshx ; (2) pshy ; (2) ldx #XA ; (2) ldy #YA ; (2) movw #0,ACM ; (5) movw #0,ACM+2 ; (5) ldab #N ; (1) loop emacs ACM ; (13) leax 2,x ; (2) leay 2,y ; (2) dbne b,loop ; (3) puly ; (3) pulx ; (3) pulb ; (3) rts ; (5) ACM rmb 4 XA fdb ?,?,? YA fdb ?,?,? interrupt_handler ; does some interrupt stuff rti When done, comes back to your program BUT, you can actually get interrupted at ANY time! Examples of interrupts: Sensor firing, timer going off, etc…
42
Interrupts: Basics Like RTS, except does a bunch of extra stuff.
Why all the extra stuff?? Because you have no idea where in the program you will be when an interrupt happens. Need to save ALL register state! RTI puts all this state back. Description Assembly mnemonic operation Examples Return from interrupt RTI (CCR) ((SP)), (SP) (SP) + 1, (D) ((SP)), (SP) (SP) + 2, (X) ((SP)), (SP) (SP) + 2, (Y) ((SP)), (SP) (SP) + 2, (PC) ((SP)), (SP) (SP) + 2 Interrupt happens: ”stack registers” automatically RTI called: ”unstack registers” automatically
43
Reentrancy, interrupts and function calling
Is there a problem?: A: Yes B: No Is there a problem?: A: Yes B: No What is a problem?: A: (X) modified B: (Y) modified C: (B) modified D: (ACM) modified E: (XA) modified What is a problem?: A: (X) modified B: (Y) modified C: (B) modified D: (ACM) modified E: (XA) modified Reentrancy, interrupts and function calling A function (subroutine) is reentrant if it can be interrupted at any time and then called from the interrupt. N equ ? org $800 macme pshb ; (2) pshx ; (2) pshy ; (2) ldx #XA ; (2) ldy #YA ; (2) movw #0,ACM ; (5) movw #0,ACM+2 ; (5) ldab #N ; (1) loop emacs ACM ; (13) leax 2,x ; (2) leay 2,y ; (2) dbne b,loop ; (3) puly ; (3) pulx ; (3) pulb ; (3) rts ; (5) ACM rmb 4 XA fdb ?,?,? YA fdb ?,?,? interrupt_handler ; does some interrupt stuff jsr macme ; does some other stuff rti To be safe, the bottom line is: To be reentrant – do not read/write from global memory Output written to global memory
44
Other “software interrupt” instructions
Description Assembly mnemonic operation Examples Unimplemented op code TRAP (CCR) ((SP)), (SP) (SP) + 1, (D) ((SP)), (SP) (SP) + 2, (X) ((SP)), (SP) (SP) + 2, (Y) ((SP)), (SP) (SP) + 2, (PC) ((SP)), (SP) (SP) + 2 I bit of CCR 1 (PC) (Trap Vector) Software Interrupt SWI (PC) (SWI Vector) Also called “exceptions”. Exceptions = SW triggered Interrupts = HW triggered
45
Example Application Write a program that prompts a user for a five-digit access code (or “PIN”), checks it against a valid combination stored in memory, prompts the user to enter the code a second time if an error is made, and denies access if the PIN is entered incorrectly the second time. For security, the digits of the PIN should be echoed to the screen as “*” characters. Three possible scenarios are outlined below: Welcome to the MegaMoney ATM! Enter PIN: ***** Access granted Error in PIN – try again… Sorry – access denied Your money is our money until you can figure out how to withdraw it!! 1 valid code – first try invalid code – first try 2 valid code – second try invalid code – first try 3 invalid code – second try
46
STEP 1: Write a subroutine checkp that compares the 5-digit PIN entered by the user against a 5-digit PIN stored in memory. At entry to checkp, the PIN entered by the user is pushed onto the stack (in the order that it was entered), as five separate bytes each containing a single BCD digit (i.e., it is in “unpacked” format). Also at entry to checkp, the X register points to the valid 5-digit code stored in memory; it, too, is stored in “unpacked” format, in five consecutive bytes. At exit, checkp should return with the carry flag set (C=1) if the two combinations match, or with the carry flag clear (C=0) if the two combinations do not match. Also at exit, the combination tested should be removed from the stack and the X register should be restored to its original value (no other registers need to saved/restored).
47
checkp ; ; Subroutine checkp compares the 5-digit code stored in memory ; (pointed to by X) with the 5-digit code passed on the stack ; If the two codes match, checkp returns with C=1; else, C=0 ; The 5-byte code passed to this routine is de-allocated before exit ; The X register retains its original value puly ; save return address in Y ldab #NDIGS ; B used as loop counter and decb ; as pointer offset ; Comparison loop checkl pula ; get digit of combination entered cmpa b,x ; compare with valid code bne checkb ; if mismatch, know combo is bad decb bpl checkl ; B ranges from 4 to 0 ; If "fall through", all digits match sec ; return with CF = 1 pshy ; restore return address saved in Y rts ; note that X is unchanged ; If "mismatch" occurs, de-allocate remainder of combination ; that was passed on the stack and return with CF = 0 checkb leas b,sp ; de-allocate rest of combination clc ; return with CF = 0
48
STEP 2: Write a main program that prints a welcome message, prompts the user to enter a PIN, checks the PIN using the checkp routine, gives the user a second chance if necessary, checks the second PIN entered using checkp, and prints a sarcastic message if the user fails to enter a valid PIN the second time. The main program should also provide storage for the valid 5-digit combination (of your choice). The main program should simply terminate with a STOP instruction after the “access granted” or “access denied” message is printed.
49
; ATM access code verifier
; Tests 5-digit code entered by user ; and compares it with 5-digit code ; stored in memory CR equ $0d LF equ $0a NULL equ 0 NDIGS equ 5 ; number of digits in combination NTRYS equ 2 ; number of trys allowed ; Start of main program ; 1. Welcome user ; 2. Prompt for PIN ; 3. Input 5-digit PIN (echo "*" as code entered) ; 4. Call checkp to see if PIN is valid ; 5. If PIN is not valid then ; if this is the second try then ; print "access denied" and exit ; else ; print "try again" and goto 2 ; endif ; else print "access granted" and exit ; endif
50
movb #NTRYS,ntry ; initialize number of trys jsr pmsg fcb CR,LF
org $800 main movb #NTRYS,ntry ; initialize number of trys jsr pmsg fcb CR,LF fcc "Welcome to the MegaMoney ATM" fcb CR,LF,NULL prompt fcc "Enter your 5-digit PIN: ",NULL ldab #NDIGS pmptlp jsr inchar ; get character jsr atoh ; convert ASCII to HEX psha ; place on stack ldaa #'*' jsr outchar ; echo * dbne b,pmptlp ldx #combo ; X points to valid combination jsr checkp lbcs wasgood dec ntry beq goaway ; give user another chance ; if still has any trys left ; else, deny access to user fcc "Error in PIN - try again" bra prompt
51
fcc "Sorry - Access Denied"
; Uh oh...user ran out of chances goaway jsr pmsg fcb CR,LF fcc "Sorry - Access Denied" fcc "Your Money is Our Money Until You Can Figure Out Your PIN!" fcb CR,LF,NULL stop wasgood fcc "Access granted" jmp main ; start over ; Combination declaration combo fcb 7,6,5,9,4 ntry rmb 1 ; number of trys
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.