Lecture 5 Assembly Programming: Arithmetic

Similar documents
ECE 3120 Computer Systems Arithmetic Programming

Chapter 2: HCS12 Assembly Programming. EE383: Introduction to Embedded Systems University of Kentucky. Samir Rawashdeh

Lecture 6 Assembly Programming: Branch & Iteration

Lecture 9 Subroutines

Lecture 7 Assembly Programming: Shift & Logical

Lecture 11: Advanced Arithmetic Instructions

Menu Computer Organization Programming Model for the an example microprocessors (the G-CPU & Motorola 68HC11) Assembly Programming Look into my...

EE4390 Microprocessors

LECTURE #21: G-CPU & Assembly Code EEL 3701: Digital Logic and Computer Systems Based on lecture notes by Dr. Eric M. Schwartz

Chapter 2 HCS12 Assembly Language

MC9S12 Assembler Directives A Summary of MC9S12 Instructions Disassembly of MC9S12 op codes. Summary of HCS12 addressing modes ADDRESSING MODES

CodeWarrior. Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff

Introduction to the 9S12 Microcontroller

Getting Started with the HCS12 IDE

It translates (converts) assembly language to machine code.

EE 3170 Microcontroller Applications

ECE331 Handout 3- ASM Instructions, Address Modes and Directives

ELECTRICAL AND COMPUTER ENGINEERING DEPARTMENT, OAKLAND UNIVERSITY ECE-470/570: Microprocessor-Based System Design Fall 2014.

EE 5340/7340 Motorola 68HC11 Microcontroler Lecture 1. Carlos E. Davila, Electrical Engineering Dept. Southern Methodist University

538 Lecture Notes Week 2

Addition and Subtraction of Hexadecimal Numbers Simple assembly language programming

Mark II Aiken Relay Calculator

2) [ 2 marks] Both of the following statements cause the value $0300 to be stored in location $1000, but at different times. Explain the difference.

EC 333 Microprocessor and Interfacing Techniques (3+1)

Addition and Subtraction of Hexadecimal Numbers Simple assembly language programming

MC9S12 Address Space

Programming the Motorola MC68HC11 Microcontroller

Example 2: Simplify each of the following. Round your answer to the nearest hundredth. a

CHAPTER ASSEMBLY LANGUAGE PROGRAMMING

Decimal, Hexadecimal and Binary Numbers Writing an assembly language program

EE 308 Spring The HCS12 has 6 addressing modes

EE319 K Lecture 7. Address mode review Assembler, Debugging Psuedo ops 16 bit timer finite state machines. University of Texas ECE

538 Lecture Notes Week 5

538 Lecture Notes Week 5

2. Arithmetic Instructions addition, subtraction, multiplication, divison (HCS12 Core Users Guide, Sections 4.3.4, and ).

Using the stack and the stack pointer

ECE 3120 Lab 1 Code Entry, Assembly, and Execution

Disassembly of MC9S12 op codes Decimal, Hexadecimal and Binary Numbers

Disassembly of MC9S12 op codes Decimal, Hexadecimal and Binary Numbers

ECE3120: Computer Systems Hardware & Software Development Tools

Divide: Paper & Pencil

ECET Chapter 2, Part 2 of 3

Assembly Language. Instructor: Dmitri A. Gusev. Spring Lecture 10, February 27, CSC : Introduction to Computer Science

ECE 372 Microcontroller Design Basic Assembly Programming. ECE 372 Microcontroller Design Basic Assembly Programming

ME4447/6405. Microprocessor Control of Manufacturing Systems and Introduction to Mechatronics. Instructor: Professor Charles Ume LECTURE 7

Chapter 1. Microprocessor architecture ECE Dr. Mohamed Mahmoud.

EEM336 Microprocessors I. Arithmetic and Logic Instructions

Multiplies the word length <ea> times the least significant word in Dn. The result is a long word.

Module 2: Computer Arithmetic

Microcontrollers. Microcontroller

EE 3170 Microcontroller Applications

1. Memory Mapped Systems 2. Adding Unsigned Numbers

8088/8086 Programming Integer Instructions and Computations

HC11 Instruction Set Architecture

Microcontrollers. 2IN60: Real-time Architectures (for automotive systems) Mike Holenderski,

HC11 Instruction Set Architecture

Digital Fundamentals. CHAPTER 2 Number Systems, Operations, and Codes

Introduction to Embedded Systems and Chapter 1: Introduction to HCS12/MC9S12. EE383: Introduction to Embedded Systems University of Kentucky

Cross Assembly and Program Development

Number Systems and Conversions UNIT 1 NUMBER SYSTEMS & CONVERSIONS. Number Systems (2/2) Number Systems (1/2) Iris Hui-Ru Jiang Spring 2010

ORG ; TWO. Assembly Language Programming

N bit is set if result of operation in negative (MSB = 1) Z bit is set if result of operation is zero (All bits = 0)

COE538 Lecture Notes Week 3 (Week of Sept 17, 2012)

538 Lecture Notes Week 3

ENE 334 Microprocessors

68000 Assembler by Paul McKee. User's Manual

Mnem. Meaning Format Operation Flags affected ADD Addition ADD D,S (D) (S)+(D) (CF) Carry ADC Add with ADC D,C (D) (S)+(D)+(CF) O,S,Z,A,P,C

Exam I Review February 2017

Arithmetic Instructions

ELEG3924 Microprocessor

Introduction to Computers and Programming. Numeric Values

Manipulating Integers

ELEG3923 Microprocessor Ch.2 Assembly Language Programming

Computer Architecture and System Programming Laboratory. TA Session 3

Computer Organisation CS303

Reading Assignment. 68HC12 Instruction Set. M68HC12 Instruction Set Categories. Some Tips. Endianness (Byte Order) Load and Store Instructions

A Simple MC9S12 Program

BINARY SYSTEM. Binary system is used in digital systems because it is:

Chapter 2 Number System

Computer Organization I. Lecture 28: Architecture of M68HC11

Integer Multiplication and Division

C Programming Language. Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff

COSC345 Software Engineering. Basic Computer Architecture and The Stack

HC11 Instruction Set

CHAPTER 8. Solutions for Exercises

Coe538 Final Study Guide 2016 (Questions & Answers)

Signed number Arithmetic. Negative number is represented as

ECE 372 Microcontroller Design Assembly Programming Arrays. ECE 372 Microcontroller Design Assembly Programming Arrays

DLD VIDYA SAGAR P. potharajuvidyasagar.wordpress.com. Vignana Bharathi Institute of Technology UNIT 1 DLD P VIDYA SAGAR

EE319K Final Fall 2005 Solution C. (3) Question 1. (3) Question 2. short function(const short in){ return in+5; } const

Chapter 5: Computer Arithmetic. In this chapter you will learn about:

Introduction to Computers - Chapter 4

Module 1-G. Marcos and Structured Programming

COMP 303 Computer Architecture Lecture 6

HC 11 Instructions! From Alex Hollowayʼs notes with! many thanks!

CHW 261: Logic Design

Adding Binary Integers. Part 4. Negative Binary. Integers. Adding Base 10 Numbers. Adding Binary Example = 10. Arithmetic Logic Unit

Computer Organization and Assembly Language. Lab Session 01

ECE L A B 1 Introduction ASSEMBLY PROGRAMMING WITH MINIIDE

CS/COE 0447 Example Problems for Exam 2 Spring 2011

Transcription:

CPE 390: Microprocessor Systems Spring 2018 Lecture 5 Assembly Programming: Arithmetic Bryan Ackland Department of Electrical and Computer Engineering Stevens Institute of Technology Hoboken, NJ 07030 Adapted from HCS12/9S12 An Introduction to Software and Hardware Interfacing Han-Way Huang, 2010 1

Try These 1. What is 27 10 in 8-bit binary? 2. What is -27 10 in 8-bit binary? 3. What is %10011010 (unsigned) in decimal? 4. What is %10011010 (signed) in decimal? 5. What is %10011010 in hex? 6. What is %10101101 + %00100111 in binary (unsigned) 7. What is %10101101 + %00100111 in binary (signed) 8. What is 299 10 in 16-bit hex? 9. What is $1A3F in decimal? 10.What is $39C2 + $A175 in hex? 2

What is Assembly Language? Assembly Language (assembly code) allows a programmer to specify machine code instructions and data that should be loaded into microprocessor memory prior to program execution. Machine code instructions are specified using mnemonics and address labels Data represents initial values of program variables Assembly Language Program Assembler Object Code (Binary data to be loaded into specific memory locations) Assembler translates assembly code mnemonics & symbols into raw binary data to be loaded into microprocessor memory 3

Structure of a HCS12 Assembly Program You will find three kinds of statements in assembly program: Assembler Directives Define data and symbols Reserve and initialize memory locations Set assembler and linking conditions Specify output format Specify end of program Assembly Language Instructions mnemonic representation of HCS12 machine code instructions Comments Explanation and documentation 4

Program Structure: Example Code label ORG $800 assembler directive results: DS.B 4 ;reserve 4 bytes for result inc_value: EQU $30 ;symbol to represent data din: DC.B $1122 ;label & initialize data assembly language instruction opcode ORG $900 ;program begins ldd din ;load $1122 into D subd #10 std results operand adda #inc_value std results+2 END ;program ends comment 5

Fields of an HCS12 Instruction loop: adda #$40 ;add $40 to accumulator A Label Field optional: usually starts from first column start with a letter followed by letters, digits or ( _ or.) can start any column if ended with a colon : Operation (Opcode) Field mnemonic machine code instructions or assembler directive is separated from label or beginning of line by at least one space Operand Field operands for instructions or arguments for assembler directives separated from operation field by at least one space Comment Field optional: starts with ; separated from operation/operand field by at least one space a line that starts with * or ; is a comment 6

Some Assembler Directives END Ends program to be processed by assembler Any statement after END is ignored ORG Assembler uses a location counter to keep track of current memory location where next machine code byte or data byte should be placed ORG directive sets a new value into the location counter for example: ORG $1000 opcode $C6 ldab #$FF operand $FF $1000 $1001 will place the opcode byte for the ldab instruction at memory address $1000 7

Initialize Memory Directives DC.B (define constant byte) define value of byte (or bytes) at current memory location location counter is updated to point to next byte address value can be specified by expression DC.W (define constant word) define value of 2-byte word(s) ORG $4000 array: DC.B $A3 DC.B $11, $22, $33 DC.W $1234, array-2 $A3 $11 $22 $33 $12 $34 $3F $FE $4000 $4001 $4002 $4003 $4004 $4005 $4006 $4007 8

Initialize Text String DC.B can also be used to define and load a string of ascii characters string specified using quotes ( ) each character represented by one-byte ascii code ORG $1000 msg: DC.B enter y/n: $1000 $1001 $1002 $1003 $1004 $1005 $1006 $1007 $1008 $1009 $65 $6E $74 $65 $72 $20 $79 $2F $6E $3A e n t e r <sp> y / n : 9

Initialize Memory Directives DCB.B (define constant block of bytes) fill a block of memory locations with same one-byte value syntax is: DCB.B count,value value is optional default value is $00 DCB.W (define constant block of words) fill a block of memory with same two-byte value ORG $3000 space_line: DCB.B 64, $20 64 locations $20 $20 $20 $20 $3000 $3001 $303E $303F 10

Reserve Memory Directives DS.B (define storage byte) reserves (and optionally labels) number of bytes at current memory location location counter is updated to point to next byte address following the reserved space reserved locations are not initialized DS.W (define storage word) reserves, and optionally labels: (# words X 2) bytes ORG $1400 buffer: DS.B $100 ;reserves 256 bytes of memory from $1400 ;to $14FF with buffer labeling first byte wbuf: DS.W 20 ;reserves 20 words (40 bytes) of memory from ;$1500 to $1527 with wbuf labeling first byte 11

EQU (equate) Equate Directive assigns a value (rather than a memory address) to a label does not affect memory contents loop_count: EQU 16 ldaa #loop_count ;load 16 into accumulator A 12

Assembler Directive Examples Show the contents of memory resulting from the following assembler directives: ORG $4800 xyz: EQU 24 abc: DC.B $20, 16 DC.W $21, $1ACD res: DS.B 3 DC.B bcd DC.W abc+xyz label address data Show results as a table: ascii code for b is $62, ascii code for c is $63, ascii code for d is $64 13

Software Development Process Problem definition: Identify precisely what needs to be done Develop a plan or algorithm computational procedure that takes a set of inputs and produces required outputs may be expressed as a set of steps that need to be performed may include iteration and sub-procedures or subroutines need to specify data structures that may be required algorithm may be expressed in pseudo-code (e.g. A A+1) algorithm code may be expressed in flow-chart Programming convert computational (or flowchart) steps into executable statements and data structures in target language Program testing & debugging Program maintenance 14

Flow-Chart Symbols Terminal A Process Subroutine Input or Output B off-page connector Decision? no yes A on-page connector 15

Programs to do Simple Arithmetic Write a program starting at memory location $1500 to subtract the contents of memory location $1005 from the sum of memory locations $1000 and $1002 and store the difference at $1010. Start Solution: Step1: Load contents of memory loc. $1000 into A Step2: Add contents of memory loc. $1002 to A Step3: Subtract contents of memory loc. $1005 from A Step4: Store contents of A to memory loc. $1010 A [$1000] A [A] + [$1002] A [A] - [$1005] $1010 [A] End 16

Algorithm to Assembly Code Start A [$1000] A [A] + [$1002] A [A] - [$1005] $1010 [A] ORG $1500 ; starting address ldaa $1000 ; A [$1000] adda $1002 ; A [A] + [$1002] suba $1005 ; A [A] - [$1005] staa $1010 ; $1010 [A] bgnd ; break to debugger END ;end of program End 17

More on Arithmetic (Add/Sub) We know how to add 8-bit quantities using A or B accumulator: ldaa $1000 ; add 8-bit data in $1000 adda $1001 ; to 8-bit data in $1001 staa $2000 ; and store 8-bit result in $2000 and we can add 16-bit quantities using D accumulator: ldd $1000 ; add 16-bit data in $1000:$1001 addd $1002 ; to 16-bit data in $1002:$1003 std $2000 ; and store 16-bit result in $2000:$2001 How can we add quantities of greater precision (e.g. 24-bit)? 18

Carry/Borrow Flag Carry is LSB of CCR S X H I N Z V C Carry acts like the n th result bit when doing n-bit add/sub Carry set to 1 whenever addition generates carry-out Carry set to 1 whenever subtraction requires borrow-out ldaa #$20 adda #$30 ldaa #$93 adda #$8B ldaa #$96 suba #$3A ldaa #$27 suba #$54 20 +30 0 50 93 +8B 1 1E 96 3A 0 5C 27 54 1 D3 also 16-bit: ldd #$A200 addd #$7000 A200 +7000 1 1200 19

Multi-precision Addition Carry bit allows us to do multi-precision arithmetic i.e. arithmetic on numbers whose precision is greater than that of the ALU For example how do we add $59A183 to $5482DB? Solution: Step1: Add $A183 to $82DB and remember carry Step2: Store 16-bit result as least significant two bytes of answer Step3: Add $59 to $54 with carry from step 1 Step4: Store 8-bit result as MSbyte of answer 20

32-bit Arithmetic Write a program at memory location $4000 to add 4-byte numbers that are stored at $1000~$1003 and $1004~$1007 and store the sum at locations $1010~1013 21

Multiplication Mnemonic Function Operation emul Unsigned 16 x 16 multiply Y:D [D] x [Y] emuls Signed 16 x 16 multiply Y:D [D] x [Y] mul Unsigned 8 x 8 multiply D [A] x [B] Write an instruction sequence to multiply (unsigned) register X by register Y and store result in $1000~$1003 tfr x,d ;transfer X operand into D emul ;perform multiplication sty $1000 ;save upper 16-bits of product std $1002 ;save lower 16-bits of product 22

Multiplication Mnemonic Function Operation emul Unsigned 16 x 16 multiply Y:D [D] x [Y] emuls Signed 16 x 16 multiply Y:D [D] x [Y] mul Unsigned 8 x 8 multiply D [A] x [B] n1 and n2 are signed 16-bit integers and n3 is a 32-bit signed integer. Use assembler directives to reserve space for n1, n2 and n3 at memory locations $1000, $1002 and $1004 respectively. Write code starting at $4000 to perform: n3 = n1 * n2 23

Division Mnemonic Function Operation ediv edivs idiv idivs Unsigned 32 by 16 divide Signed 32 by 16 divide Unsigned 16 by 16 divide Signed 16 by 16 divide YY YY : DD [XX] DD rrrrrrrrrrrrrrrrrr YY YY : DD [XX] DD rrrrrrrrrrrrrrrrrr XX DD XX DD rrrrrrrrrrrrrrrrrr XX DD XX DD rrrrrrrrrrrrrrrrrr Remember that if divisor > dividend, then quotient = 0 24

Integers Math and Precision When performing integer arithmetic (especially multiplication and division), important to keep track of potential size of results to avoid overflow and/or loss of precision 1200 2500 Suppose we want to calculate: 1150 Does the order of the operations matter? Correct answer is 2608.6956 (but we can only do integer arithmetic) If I do multiply first (emul followed by ediv), I get 2608 If I do divide first ( ediv followed by emul), I get 2500 25

Rounding If we take the quotient as being the answer to a divide operation (and ignore the remainder), the result is truncated to the closest integer that is less than the correct answer ( 2608 instead of 2608.6956) A better result would be to round to the nearest integer (2609). This can be achieved by adding half of the divisor to the dividend before executing the divide operation: rrrrrrrrrrrrrr qqqqqqqqqqqqqqqq = dddddddddddddddd + dddddddddddddd 2 dddddddddddddd This effectively adds 0.5 to the answer before truncation. 26

Integer Precision: Example Multiply the unsigned 16-bit number in locations $1000:$1001 by 1.414 (approx. to 2), truncating result to nearest integer. Multiply the unsigned 16-bit number in locations $1000:$1001 by 1.414 (approx. to 2), rounding result to nearest integer. 27