Arrays, Stacks and the LC-3

Similar documents
PROBLEMS (Cap. 4 - Istruzioni macchina)

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

DATA STRUCTURES USING C

Module 2 Stacks and Queues: Abstract Data Types

Common Data Structures

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Chapter 7 Assembly Language

Data Structures and Data Manipulation

PROGRAMMING CONCEPTS AND EMBEDDED PROGRAMMING IN C, C++ and JAVA: Lesson-4: Data Structures: Stacks

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

LC-3 Assembly Language

Algorithms and Data Structures

10CS35: Data Structures Using C

1 Description of The Simpletron

Stack machines The MIPS assembly language A simple source language Stack-machine implementation of the simple language Readings:

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

DATA STRUCTURE - STACK

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

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

MACHINE ARCHITECTURE & LANGUAGE

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Chapter 3: Restricted Structures Page 1

Data Structures and Algorithms V Otávio Braga

Stacks. Linear data structures

The stack and the stack pointer

1 Classical Universal Computer 3

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

MICROPROCESSOR AND MICROCOMPUTER BASICS

Lecture 2. Binary and Hexadecimal Numbers

Adapting the PowerPC 403 ROM Monitor Software for a 512Kb Flash Device

Stack Allocation. Run-Time Data Structures. Static Structures

Application of Stacks: Postfix Expressions Calculator (cont d.)

Compilers I - Chapter 4: Generating Better Code

Microprocessor & Assembly Language

Advanced Computer Architecture-CS501. Computer Systems Design and Architecture 2.1, 2.2, 3.2

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

Computer Systems Architecture

A single register, called the accumulator, stores the. operand before the operation, and stores the result. Add y # add y from memory to the acc

Glossary of Object Oriented Terms

Data Structures and Algorithms Stacks and Queues

Lecture 27 C and Assembly

Atmiya Infotech Pvt. Ltd. Data Structure. By Ajay Raiyani. Yogidham, Kalawad Road, Rajkot. Ph : ,

Chapter 7D The Java Virtual Machine

DATA STRUCTURE - QUEUE

PES Institute of Technology-BSC QUESTION BANK

7.1 Our Current Model

Comp 255Q - 1M: Computer Organization Lab #3 - Machine Language Programs for the PDP-8

Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

Data Structure with C

Z80 Instruction Set. Z80 Assembly Language

An Introduction to the ARM 7 Architecture

Queues and Stacks. Atul Prakash Downey: Chapter 15 and 16

Last not not Last Last Next! Next! Line Line Forms Forms Here Here Last In, First Out Last In, First Out not Last Next! Call stack: Worst line ever!

A3 Computer Architecture

Instruction Set Architecture

MACHINE INSTRUCTIONS AND PROGRAMS

MICROPROCESSOR. Exclusive for IACE Students iacehyd.blogspot.in Ph: /422 Page 1

A Tiny Guide to Programming in 32-bit x86 Assembly Language

Exceptions in MIPS. know the exception mechanism in MIPS be able to write a simple exception handler for a MIPS machine

Stack & Queue. Darshan Institute of Engineering & Technology. Explain Array in detail. Row major matrix No of Columns = m = u2 b2 + 1

Instruction Set Architecture (ISA)

QUEUES. Primitive Queue operations. enqueue (q, x): inserts item x at the rear of the queue q

Chapter 1 Computer System Overview

Traditional IBM Mainframe Operating Principles

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

CSC 2405: Computer Systems II

Lecture Outline. Stack machines The MIPS assembly language. Code Generation (I)

Overview. CISC Developments. RISC Designs. CISC Designs. VAX: Addressing Modes. Digital VAX

X86-64 Architecture Guide

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.

İSTANBUL AYDIN UNIVERSITY

More MIPS: Recursion. Computer Science 104 Lecture 9

Evolving Data Structures with Genetic Programming

Chapter 4 Lecture 5 The Microarchitecture Level Integer JAVA Virtual Machine

An Overview of Stack Architecture and the PSC 1000 Microprocessor

STACKS,QUEUES, AND LINKED LISTS

Stacks. Data Structures and Data Types. Collections

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

1 The Java Virtual Machine

Introduction to Stacks

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

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

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

PART B QUESTIONS AND ANSWERS UNIT I

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Administrative Issues

CHAPTER 7: The CPU and Memory

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

LSN 2 Computer Processors

Intel 8086 architecture

ALLIED PAPER : DISCRETE MATHEMATICS (for B.Sc. Computer Technology & B.Sc. Multimedia and Web Technology)

EC 362 Problem Set #2

HOMEWORK # 2 SOLUTIO

Keil C51 Cross Compiler

Return-oriented programming without returns

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Welcome to Basic Math Skills!

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

Queues Outline and Required Reading: Queues ( 4.2 except 4.2.4) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

Transcription:

Arrays, Stacks and the LC-3 (Textbook chapter 10.1 -- 10.4.1)

Summary 1. The array data structure 2. The stack data structure 3. Arithmetic using a stack 11-2

Arrays Definition: A list of values arranged sequentially in memory and grouped under a single name Example: a list of telephone numbers In C, the expression a[4] refers to the 5th element of the array a Most assembly languages have only basic concept of arrays (.BLKW in LC-3) 11-3

Properties of arrays: Properties of Arrays Each element is the same size (i.e. same type) Elements are stored contiguously First element is located at the lowest memory address In assembly language we must Allocate correct amount of space for an array Map array addresses to memory addresses Ex: myarray.blkw 5 11-4

Pointer Address of a variable in memory Allows us to indirectly access variables Base of an array = pointer to the first element myarray.blkw 5 LEA R2, myarray LDR R0, R2, #0 STR R0, R2, #2 11-5

Multi-dimensional arrays 2 rows 4 columns A 0 1 2 3 0 A00 A01 A02 A03 1 A10 A11 A12 A13 2 major memory mappings: column-major (fortran, matlab) and row-major (about everyone else). x3100 A 00 A 00 x3101 A 01 A 10 x3102 A 02 A 01 x3103 x3104 x3105 x3106 x3107 A 13 A 13 Row-major Column-major 11-6

Abstract Data Structures LIFO and FIFO defined by the rules for inserting and extracting data LIFO (Last In-First Out) FIFO (First In-First Out) stack queue A, B, C A, B, C 11-7

Stack Data Structure A stack is a LIFO structure Operations: PUSH (enter item at top of stack) POP (remove item from top of stack) TOS = Top Of Stack 11-8

Stack overflow and underflow Error conditions: Overflow (trying to push onto full stack) Underflow (trying to pop from empty stack) 11-9

A physical stack A coin holder as a stack 11-10

Implemented in memory A software stack The Top Of Stack moves as new data is entered NOTE that the stack grows towards address 0000! Here R6 is the TOS register, a pointer to the Top Of Stack 11-11

Stack in LC-3 1. Declare an array to be your stack 2. Use a register to point to the TOS (R6 is the convention) 3. Implement PUSH and POP 11-12

Push and Pop in LC-3 Assume data to push is in a specific register (R0) Decrement TOS pointer (our stack grows downward) Then write data in R0 to new TOS PUSH ADD R6, R6, #-1 STR R0, R6, #0 NOTE: the TOS points to the last element pushed Assume data to pop will go to a specific register (R0) Read data at current TOS into R0 Then increment TOS pointer POP LDR R0, R6, #0 ADD R6, R6, #1 11-13

Checking for over/underflow Before pushing, we have to test for overflow Before popping, we have to test for underflow In both cases, use R5 to report success or failure POP Same for PUSH and overflow 11-14

When is the stack empty or full? First way: TOS always points to top valid element Stack Overflow: TOS = Stack Underflow: TOS = x3ffa x3ffb x3ffc x3ffd x3ffe x3fff x4000 MAX BASE 11-15

When is the stack empty or full? Second way: TOS always points to next available location to push x3ffa x3ffb x3ffc MAX Stack Overflow: TOS = Stack Underflow: TOS = x3ffd x3ffe x3fff x4000 BASE 11-16

Stack protocol on LC-3 - Example Use standard conventions: PUSH pushes R0, returns success in R5 POP pops into R0, returns success in R5 Stack pointer is R6 All other used registers need to be callee-saved The TOS points to the top valid element The stack goes from x3fff to x3ffb 11-17

Stack protocol in LC-3: POP (1/3) POP ST R2, Save2 ; save, needed by POP ST R1, Save1 ; save, needed by POP LD R1, nbase ; nbase contains -x3fff ADD R1, R1, #-1 ; R1 now has -x4000 ADD R2, R6, R1 ; compare SP to BASE BRz fail_exit ; branch if stack is empty LDR R0, R6, #0 ; the actual pop ADD R6, R6, #1 ; adjust stack pointer BRnzp success_exit 11-18

Stack protocol in LC-3: PUSH (2/3) PUSH ST R2, Save2 ; needed by PUSH ST R1, Save1 ; needed by PUSH LD R1, nmax ; nmax has -x3ffb ADD R2, R6, R1 ; compare SP to x3ffb BRz fail_exit ; branch is stack is full ADD R6, R6, #-1 ; adjust Stack Pointer STR R0, R6, #0 ; the actual push 11-19

Stack prot. in LC-3: Return values (3/3) success_exit LD R1, Save1 ; restore registers LD R2, Save2 AND R5, R5, #0 ; R5 <-- success RET fail_exit LD R1, Save1 ; restore registers LD R2, Save2 AND R5, R5, #0 ADD R5, R5, #1 ; R5 <-- fail RET nbase.fill xc001 ; nbase has -x3fff nmax.fill xc005 ; nmax has x3ffb Save1.FILL x0000 Save2.FILL x0000 11-20

Stack as an alternative to Registers Three-address vs zero-address The LC-3 explicitly specifies the location of each operand: it is a three-address machine e.g. ADD R0, R1, R2 Some machines use a stack data structure for all temporary data storage: these are zero-address machines the instruction ADD would simply pop the top two values from the stack, add them, and push the result back on the stack some calculators use a stack to do arithmetic, most general purpose microprocessors use a register bank 11-21

Interesting Routines in Book 10.4.2 shows how to convert ASCII to Binary with a stack. 10.4.3 shows how to convert from Binary to ASCII with a stack. 11-22

Recommended exercises Read and implement the examples in textbook sections 10.3, 10.4, and 10.5 Ex 10.3, 10.4, 10.5 ok but tedious Ex 10.6 (and 10.7), 10.8, 10.9 11-23