ECE3120: Computer Systems Chapter 4: Subroutines

Similar documents
The stack and the stack pointer

Programming the Motorola MC68HC11 Microcontroller

Microcontroller Basics A microcontroller is a small, low-cost computer-on-a-chip which usually includes:

M6800. Assembly Language Programming

HC12 Assembly Language Programming

6800 Basics. By Ruben Gonzalez

U:\montages\dominique\servo\moteur_AV_AR\program\moteur_AV_AR.asm jeudi 28 avril :32

Lecture 3 Addressing Modes, Instruction Samples, Machine Code, Instruction Execution Cycle

ASSEMBLY LANGUAGE PROGRAMMING (6800) (R. Horvath, Introduction to Microprocessors, Chapter 6)

Introduction to Microcontrollers

AN2183. Using FLASH as EEPROM on the MC68HC908GP32. Introduction. Semiconductor Products Sector Application Note

Baseball Pitch Speedometer

AN1752. Data Structures for 8-Bit Microcontrollers By Brad Bierschenk Consumer Systems Group Applications Engineering Austin, Texas.

Application Note AN_106. Interfacing to a USB Printer using Vinculum VNC1L Host Controller

How To Write A Microsoft Microsoft 8D (Droid) (Program) (Powerbook) (I386) (Microsoft) (Donga) (Opera) And (Dungeo) (Dugeo

Real-time system programs

Scrolling Marquee Display

How To Understand All Instructions In The Power12 Program

Programmer s Model = model of µc useful to view hardware during execution of software instructions

A Utility for Programming Single FLASH Array HCS12 MCUs, with Minimum RAM Overhead

Interrupts and the Timer Overflow Interrupts Huang Sections What Happens When You Reset the HCS12?

Parallel I/O Interface to the Neuron Chip

Application Note. General Description. AN991/D Rev. 1, 1/2002. Using the Serial Peripheral Interface to Communicate Between Multiple Microcomputers

Embedded system programming: HCS12

Introduction to Embedded Systems. Software Update Problem

EE282 Computer Architecture and Organization Midterm Exam February 13, (Total Time = 120 minutes, Total Points = 100)

Microcontrollers and the Freescale/Motorola HC11

C programming for embedded microcontroller systems.

8051 Serial Port. Crystal TXD. I/O Device RXD. Embedded Systems Peripherals

Stacks. Linear data structures

Pseudo code Tutorial and Exercises Teacher s Version

MACHINE ARCHITECTURE & LANGUAGE

THUMB Instruction Set

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

68HC11 Programmer s Reference Manual

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Question Bank Subject Name: EC Microprocessor & Microcontroller Year/Sem : II/IV

Translating C code to MIPS

PROBLEMS (Cap. 4 - Istruzioni macchina)

6. Standard Algorithms

Chapter 4: Computer Codes

IBM Emulation Mode Printer Commands

Microprocessor/Microcontroller. Introduction

Operating Systems. Virtual Memory

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

An Assembly Language Programming Style Guide. by Ken Lambert. Naming Conventions. Use of Whitespace

Stack Allocation. Run-Time Data Structures. Static Structures

1 Classical Universal Computer 3

First Bytes Programming Lab 2

DATA STRUCTURES USING C

COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms.

Typy danych. Data types: Literals:

Water Level Monitoring

Freescale Semiconductor, I

Storing Measurement Data

Solution for Homework 2

Data Structures. Level 6 C Module Descriptor

Symbol Tables. Introduction

Sorting Algorithms. Nelson Padua-Perez Bill Pugh. Department of Computer Science University of Maryland, College Park

Instruction Set Architecture

Freescale Semiconductors, Inc. Freescale HC12 Assembler

Debugging of Application Programs on Altera s DE-Series Boards. 1 Introduction

Simple sorting algorithms and their complexity. Bubble sort. Complexity of bubble sort. Complexity of bubble sort. Bubble sort of lists

Modbus RTU Communications RX/WX and MRX/MWX

Data Structure [Question Bank]

MIPS Assembly Code Layout

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

Query Processing C H A P T E R12. Practice Exercises

Instruction Set Architecture. or How to talk to computers if you aren t in Star Trek

M A S S A C H U S E T T S I N S T I T U T E O F T E C H N O L O G Y DEPARTMENT OF ELECTRICAL ENGINEERING AND COMPUTER SCIENCE

Apple II Technical Notes

2) Write in detail the issues in the design of code generator.

The goal is to program the PLC and HMI to count with the following behaviors:

A TOOL FOR DATA STRUCTURE VISUALIZATION AND USER-DEFINED ALGORITHM ANIMATION

Naming Conventions General Accounting

How To Write An Array In A Microsoft Zil

RS-485 Protocol Manual

How To Use A Computer With A Screen On It (For A Powerbook)

Organization of Programming Languages CS320/520N. Lecture 05. Razvan C. Bunescu School of Electrical Engineering and Computer Science

Numeral Systems. The number twenty-five can be represented in many ways: Decimal system (base 10): 25 Roman numerals:

GPIB Instrument Control

The Principle of Vaccination in Teaching

Introduction. What is an Operating System?

So far we have considered only numeric processing, i.e. processing of numeric data represented

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 20: Stack Frames 7 March 08

root node level: internal node edge leaf node Data Structures & Algorithms McQuain

CS106A, Stanford Handout #38. Strings and Chars

Systems Design & Programming Data Movement Instructions. Intel Assembly

COWLEY COLLEGE & Area Vocational Technical School

1. Define: (a) Variable, (b) Constant, (c) Type, (d) Enumerated Type, (e) Identifier.

8. MACROS, Modules, and Mouse

Introduction to Data Structures

AVR034: Mixing C and Assembly Code with IAR Embedded Workbench for AVR. 8-bit Microcontroller. Application Note. Features.

The 68000's Instruction Set

Passing 1D arrays to functions.

EARTH PEOPLE TECHNOLOGY SERIAL GRAPH TOOL FOR THE ARDUINO UNO USER MANUAL

Data Structure with C

Chapter 6: Physical Database Design and Performance. Database Development Process. Physical Design Process. Physical Database Design

4 Character 5x7 LED Matrix Display

Transcription:

ECE3120: Computer Systems Chapter 4: Subroutines Manjeera Jeedigunta http://blogs.cae.tntech.edu/msjeedigun21 Email: msjeedigun21@tntech.edu Tel: 931-372-6181, Prescott Hall 120

Stack Frame - The region in the stack that holds incoming parameters, the subroutine return address, local variables, and saved registers is referred to as stack frame. - The stack frame is also called activation record. Local variables SP Saved registers Return address Incoming parameters Figure 4.9 Structure of the 68HC12 stack frame

Example: Convert the character and word counting program into a subroutine. The starting address of the string is passed to this subroutine in the index register X, and the character and word count are returned to the caller in the register D and X respectively tab equ $09 sp equ $20 cr equ $0D lf equ $0A ch_cnt equ 0 ;offset wd_cnt equ 1 ;offset string_x fcc "this is a long and strange test string to count chars and words." fcb 0 org $1000 char_cnt rmb 2 word_cnt rmb 2 org $1500 ldx #string_x ;base address of the string jsr sub_cwcnt std char_cnt stx word_cnt swi ;subroutine for character and word count sub_cwcnt leas -2,sp ;allocation for the local variables clr ch_cnt,sp clr wd_cnt,sp string_lp ldab 1,x+ ; get one character and move string pointer lbeq done ; is this the end of the string? inc ch_cnt,sp

; the following 8 instructions skip white space characters between words cmpb #sp beq string_lp ;if it is any of these it just moves to the next cmpb #tab ; character beq string_lp cmpb #cr beq string_lp cmpb #lf beq string_lp ; a non-white character is the start of a new word inc wd_cnt,sp wd_loop ldab 1,x+ ; get one character and move pointer beq done inc ch_cnt,sp ; the following 8 instructions check the end of a word cmpb #sp lbeq string_lp cmpb #tab lbeq string_lp cmpb #cr lbeq string_lp cmpb #lf lbeq string_lp bra wd_loop done ldab wd_cnt,sp clra xgdx ldab ch_cnt,sp clra leas 2,sp rts end

Data Conversion: Lower to Upper case Lower case letters range from 97-122 (decimal). Upper case letters range from 65-90 (decimal). Notice that the lower case letter values are exactly 32 greater than their upper case counterparts. This is useful for upper and lower case conversions. The numbers 0-9 range from 48-57 (decimal).

Write a subroutine to convert the lower case letters in a string into uppercase letters. The string address is passed to the subroutine through the index register X org $1000 string fcc "This is a String to be converted. ;string to be converted db 0 org $1500 ldx #string jsr locase2hi swi locase2hi psha loopl2h ldaa 0,x beq done cmpa #$61 ; decimal 97 blo next cmpa #$7A ;decimal 122 bhi next suba #$20 ;decimal 32 staa 0,x next inx bra loopl2h done pula rts end Lower case 97-122 Upper case -65-90 Numbers 48-57 Lower>Upper by 32

Bubble Sort - Sorting is useful for improving the searching speed when an array or a file need to be searched many times. - Bubble sort is a simple but inefficient sorting method. All elements in positions > or equal to n-i are in right position after iteration I 0 1 157 13 35 9 98 810 120 54 10 30 13 35 9 98 157 120 54 10 30 810 Hence 2 13 9 35 98 120 54 10 30 157 810 1 st interation = n-1 comparisons 3 9 13 35 98 54 10 30 120 157 810 2 nd iteration = n-2 comparisons so on 4 9 13 35 54 10 30 98 120 157 810 N-1 iterations might not be required to sort the array An array is sorted if no swaps are made in an iteration 5 6 7 8 9 13 35 10 30 54 98 120 157 810 9 13 10 30 35 54 98 120 157 810 9 10 13 30 35 54 98 120 157 810 9 10 13 30 35 54 98 120 157 810 sorted 9 9 10 13 30 35 54 98 120 157 810

Example.4.13: Write a subroutine to implement the bubble sort algorithm and a sequence of instructions along with a set of test data for testing this subroutine. - Pass the base address of the array and the array count in the stack - Four bytes are needed for local variables. SP+1 SP+2 SP+3 SP+12 SP+13 iteration inner in_order buf X Y A B return address N array_x SP Figure 4.15 Stack frame for bubble sort

ploop Inner = no.of swaps cloop Not in order so set the flag to 0 looptes t Move to next iteration

arr equ 13 ; distance of the variable arr from stack top arcnt equ 12 ; distance of the variable arcnt from stack top buf equ 3 ; distance of local variable buf from stack top in_order equ 2 ; distance of local variable in_order from stack top inner equ 1 ; distance of local variable inner from stack top iteration equ 0 ; distance of local variable iteration from stack top true equ 1 ;used to set in_order flag false equ 0 n equ 30 ; array count local equ 4 ; number of bytes used by local variables SP+1 org $800 SP+2 array_x db 3,29,10,98,54,9,100,104,200,92,87,48,27,22,71 SP+3 db 1,62,67,83,89,101,190,187,167,134,121,20,31,34,54 org $1000 lds #$8000 ; initialize stack pointer ldx #array_x pshx ldaa #n SP+12 psha jsr bubble SP+13 leas 3,sp ; deallocate space used by outgoing parameters swi ; iteration inner in_order buf X Y A B return address N array_x SP Figure 4.15 Stack frame for bubble sort

bubble pshd pshy pshx leas -local,sp ; allocate space for local variables ldaa arcnt,sp ; compute the number of iterations to be performed deca ;A N-1 staa iteration,sp ;Iteration N-1 ploop ldaa #true ; set array in_order flag to true before any iteration staa in_order,sp ;in_order 1 ldx arr,sp ; use index register X as the array pointer ldaa iteration,sp ; initialize inner loop count for each iteration staa inner,sp ; inner N-1 (1 st iter) iteration cloop ldaa 0,x ; compare two adjacent elements SP+1 inner cmpa 1,x ; " SP+2 in_order bls looptest SP+3 buf ; the following five instructions swap the two adjacent elements staa buf,sp ; swap two adjacent elements X ldaa 1,x ; " Y staa 0,x ; " A ldaa buf,sp ; " B staa 1,x ; " ldaa #false ; reset the in-order flag SP+12 N staa in_order,sp ; " SP+13 array_x return address SP Figure 4.15 Stack frame for bubble sort

looptest inx dec inner,sp bne cloop tst in_order,sp ; test array in_order flag after each iteration bne done dec iteration,sp bne ploop done leas local,sp ; deallocate local variables pulx SP+1 puly SP+2 puld rts SP+3 end iteration inner in_order buf X SP Y A B SP+12 SP+13 return address N array_x Figure 4.15 Stack frame for bubble sort

Next Using the D-Bug 12 Functions to perform I/O Operations