Decimal, Hexadecimal and Binary Numbers Writing an assembly language program

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

HC12 Assembly Language Programming

M6800. Assembly Language Programming

The stack and the stack pointer

Programming the Motorola MC68HC11 Microcontroller

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

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

Oct: 50 8 = 6 (r = 2) 6 8 = 0 (r = 6) Writing the remainders in reverse order we get: (50) 10 = (62) 8

Today. Binary addition Representing negative numbers. Andrew H. Fagg: Embedded Real- Time Systems: Binary Arithmetic

Binary Numbers. Binary Octal Hexadecimal

The string of digits in the binary number system represents the quantity

CDA 3200 Digital Systems. Instructor: Dr. Janusz Zalewski Developed by: Dr. Dahai Guo Spring 2012

Solution for Homework 2

6800 Basics. By Ruben Gonzalez

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

Computer Science 281 Binary and Hexadecimal Review

Base Conversion written by Cathy Saxton

Lecture 2. Binary and Hexadecimal Numbers

HOMEWORK # 2 SOLUTIO

Number Representation

2011, The McGraw-Hill Companies, Inc. Chapter 3

Number and codes in digital systems

Goals. Unary Numbers. Decimal Numbers. 3,148 is s 100 s 10 s 1 s. Number Bases 1/12/2009. COMP370 Intro to Computer Architecture 1

EE 261 Introduction to Logic Circuits. Module #2 Number Systems

Z80 Instruction Set. Z80 Assembly Language

Introduction to Microcontrollers

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

CPEN Digital Logic Design Binary Systems

Microprocessor/Microcontroller. Introduction

NUMBER SYSTEMS. 1.1 Introduction

This 3-digit ASCII string could also be calculated as n = (Data[2]-0x30) +10*((Data[1]-0x30)+10*(Data[0]-0x30));

LSN 2 Number Systems. ECT 224 Digital Computer Fundamentals. Department of Engineering Technology

Binary Representation. Number Systems. Base 10, Base 2, Base 16. Positional Notation. Conversion of Any Base to Decimal.

Useful Number Systems

Systems I: Computer Organization and Architecture

Classless Subnetting Explained

Unsigned Conversions from Decimal or to Decimal and other Number Systems

3. Convert a number from one number system to another

plc numbers Encoded values; BCD and ASCII Error detection; parity, gray code and checksums

Digital System Design Prof. D Roychoudhry Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Chapter 1: Digital Systems and Binary Numbers

COMBINATIONAL CIRCUITS

Subnetting Examples. There are three types of subnetting examples I will show in this document:

1. Give the 16 bit signed (twos complement) representation of the following decimal numbers, and convert to hexadecimal:

Binary Representation

CSI 333 Lecture 1 Number Systems

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

MACHINE INSTRUCTIONS AND PROGRAMS

2010/9/19. Binary number system. Binary numbers. Outline. Binary to decimal

To convert an arbitrary power of 2 into its English equivalent, remember the rules of exponential arithmetic:

Numbering Systems. InThisAppendix...

Counting in base 10, 2 and 16

THE BINARY NUMBER SYSTEM

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

PROGRAMMABLE LOGIC CONTROLLERS Unit code: A/601/1625 QCF level: 4 Credit value: 15 TUTORIAL OUTCOME 2 Part 1

CS201: Architecture and Assembly Language

Levent EREN A-306 Office Phone: INTRODUCTION TO DIGITAL LOGIC

How To Understand All Instructions In The Power12 Program

Number Conversions Dr. Sarita Agarwal (Acharya Narendra Dev College,University of Delhi)

The Answer to the 14 Most Frequently Asked Modbus Questions

Chapter 4: Computer Codes

The programming language C. sws1 1

Digital Design. Assoc. Prof. Dr. Berna Örs Yalçın

Number Systems. Introduction / Number Systems

CHAPTER IX REGISTER BLOCKS COUNTERS, SHIFT, AND ROTATE REGISTERS

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

Embedded system programming: HCS12

Everything you wanted to know about using Hexadecimal and Octal Numbers in Visual Basic 6

How It All Works. Other M68000 Updates. Basic Control Signals. Basic Control Signals

PROBLEMS (Cap. 4 - Istruzioni macchina)

GUAGE. ^UcMttaL P. SizUuijeSi

Decimal Number (base 10) Binary Number (base 2)

MATH-0910 Review Concepts (Haugen)

COMPSCI 210. Binary Fractions. Agenda & Reading

Chapter 2. Binary Values and Number Systems

2.3 IPv4 Address Subnetting Part 2

Sistemas Digitais I LESI - 2º ano

Classful Subnetting Explained

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

Section 1.4 Place Value Systems of Numeration in Other Bases

CS101 Lecture 11: Number Systems and Binary Numbers. Aaron Stevens 14 February 2011

Activity 6.7.4: IPv4 Address Subnetting Part 2

Number of bits needed to address hosts 8

Informatica e Sistemi in Tempo Reale

(Cat. No L3) Product Data

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

B.Sc.(Computer Science) and. B.Sc.(IT) Effective From July 2011

ARM Cortex-M3 Assembly Language

Lecture 8: Binary Multiplication & Division

Appendix A Installation of Visual C++ and MASM

Decimal to Binary Conversion

Chapter Binary, Octal, Decimal, and Hexadecimal Calculations

1 Classical Universal Computer 3

Lecture 11: Number Systems

DEPARTMENT OF INFORMATION TECHNLOGY

THUMB Instruction Set

Today s topics. Digital Computers. More on binary. Binary Digits (Bits)

Chapter 2 Assemblers

APPENDIX B. Routers route based on the network number. The router that delivers the data packet to the correct destination host uses the host ID.

Memory is implemented as an array of electronic switches

Transcription:

Decimal, Hexadecimal and Binary Numbers Writing an assembly language program o Disassembly of MC9S12 op codes o Use flow charts to lay out structure of program o Use common flow structures if-then if-then-else do-while while o Do not use spaghetti code o Plan structure of data in memory o Plan overall structure of program o Work down to more detailed program structure o Implement structure with instructions o Optimize program to make use of instruction efficiencies o Do not sacrifice clarity for efficiency Binary, Hex and Decimal Numbers (4-bit representation) Binary Hex Decimal 0000 0001 0010 1010 1011 1100 1101 1110 1111 0 1 2 A B C D E F 0 1 2 10 11 12 13 14 15

What does a number represent? Binary numbers are a code, and represent what the programmer intends for the code. 0x72 Some possible meanings: r (ASCII) INC MEM (hh ll) (HC12 instruction) 2.26V (Input from A/D converter) 114 10 (Unsigned number) +114 10 (Signed number) Set temperature in room to 69 F Set cruise control speed to 120 mph Binary to Unsigned Decimal: Convert Binary to Unsigned Decimal 1111011 2 1 x 2 6 + 1 x 2 5 + 1 x 2 4 + 1 x 2 3 + 0 x 2 2 + 1 x 2 1 + 1 x 2 0 1 x 64 + 1 x 32 + 1 x 16 + 1 x 8 + 0 x 4 + 1 x 2 + 1 x 1 123 10 Hex to Unsigned Decimal Convert Hex to Unsigned Decimal 82D6 16 8 x 16 3 + 2 x 16 2 + 13 x 16 1 + 6 x 16 0 8 x 4096 + 2 x 256 + 13 x 16 + 6 x 1 33494 10

Unsigned Decimal to Hex Convert Unsigned Decimal to Hex Division Q R Decimal 721/16 45 1 45/16 2 13 2/16 0 2 Hex 1 D 2 721 10 = 2D1 16 Signed Number Representation in 2 s Complement Form: If the most significant bit (MSB) is 0 (most significant hex digit 0 7), then the number is positive. Get decimal equivalent by converting number to decimal, and use the + sign. Example for 8 bit number: 3A 16 > + ( 3 x 16 1 + 10 x 16 0 ) 10 + ( 3 x 16 + 10 x 1 ) 10 + 58 10

If the most significant bit is 1 (most significant hex digit 8 F), then the number is negative. Get decimal equivalent by taking 2 s complement of number, converting to decimal, and using sign. Example for 8 bit number: A3 16 > - (5C+1) 16 - ( 5 x 16 1 + 13 x 16 0 ) 10 - ( 5 x 16 + 13 x 1 ) 10-93 10 One s complement table makes it simple to finding 2 s complements One s complement One s complement

One s complement table makes it simple to finding 2 s complements To take two s complement, add one to one s complement. Take two s complement of D0C3: 2F3C + 1 = 2F3D

Addition and Subtraction of Binary and Hexadecimal Numbers Setting the C (Carry), V (Overflow), N (Negative) and Z (Zero) bits How the C, V, N and Z bits of the CCR are changed? N bit is set if result of operation is negative (MSB = 1) Z bit is set if result of operation is zero (All bits = 0) V bit is set if operation produced an overflow C bit is set if operation produced a carry (borrow on subtraction) Note: Not all instructions change these bits of the CCR

Addition of Hexadecimal Numbers ADDITION: C bit set when result does not fit in word V bit set when P + P = N or N + N = P N bit set when MSB of result is 1 Z bit set when result is 0 7A 2A AC AC +52 +52 +8A +72 ----- ----- ------ ------ CC 7C 36 1E C: 0 C: 0 C: 1 C: 1 V: 1 V: 0 V: 1 V: 0 N: 1 N: 0 N: 0 N: 0 Z: 0 Z: 0 Z: 0 Z: 0

SUBTRACTION: Subtraction of Hexadecimal Numbers C bit set on borrow (when the magnitude of the subtrahend is greater than the minuend V bit set when N - P = P or P - N = N N bit set when MSB is 1 Z bit set when result is 0 7A 8A 5C 2C -5C -5C -8A -72 ----- ----- ------ ------ 1E 2E D2 BA C: 0 C: 0 C: 1 C: 1 V: 0 V: 1 V: 1 V: 0 N: 0 N: 0 N: 1 N: 1 Z: 0 Z: 0 Z: 0 Z: 0

Writing Assembly Language Programs Use Flowcharts to Help Plan Program Structure Flow chart symbols:

IF-THEN Flow Structure if (C) { A; } EXAMPLE: if (A<10) { var = 5; } CMPA #10 ; if (A<10) BLT L1 ; signed numbers BRA L2 L1: LDAB #5 ; var=5 STAB var L2: next instruction OR: CMPA #10 ; if(a<10) BGE L2 ; signed numbers LDAB #5 ; var=5 STAB var L2: next instruction

IF-THEN-ELSE Flow Structure if (C) { A; } else { B; } if(a < 10) { var = 5; } else { var = 0; } CMPA #10 ; if(a<10) BLT L1 ; signed numbers CLR var ; var=0 BRA L2 L1: LDAB #5 ; var=5 STAB var L2: next instruction

DO WHILE Flow Structure do { A; } while ( C ); EXAMPLE: i = 0; do { table[i]=table[i]/2; i=i+1; } while (i <= LEN); LDX #table CLRA ; i=0 L1: ASR 1,X+ ; table[i] /=2 INCA ; i=i+1 CMPA #LEN ; while(i<=10) BLE L1 ; unsigned ; numbers

WHILE Flow Structure while ( C ) { A; } EXAMPLE: i = 0; while( i <= LEN) { table[i]=table[i]*2; i=i+1; } LDX #table CLRA L1: CMPA #LEN BLT L2 BRA L3 L2: ASL 1,X+ INCA BRA L1 L3: next instruction

Use Good Structure When Writing Programs Do Not Use Spaghetti Code

Example Program: Divide a table of data by 2 Problem: Start with a table of data. The table consists of 5 values. Each value is between 0 and 255. Create a new table whose contents are the original table divided by 2. 1. Determine where code and data will go in memory. Code at $2000, data at $1000. 2. Determine type of variables to use. Because data will be between 0 and 255, can use unsigned 8- bit numbers. 3. Draw a picture of the data structures in memory:

4. Strategy: Because we are using a table of data, we will need pointers to each table so we can keep track of which table element we are working on. Use the X and Y registers as pointers to the tables. 5. Use a simple flow chart to plan structure of program.

6. Need a way to determine when we reach the end of the table. One way: Use a counter (say, register A) to keep track of how many Elements we have processed. x+1 y+1

7. Add code to implement blocks: EE 308 Spring 2013

8. Write the program: ; Program to divide a table by two ; and store the results in memory prog: equ $2000 data: equ $1000 count: equ 5 org prog ; Set program counter to 0x2000 ldaa #count ; Use A as counter ldx #table1 ; Use X as data pointer to table1 ldy #table2 ; Use Y as data pointer to table2 l1: ldab 0,x ; Get entry from table1 lsrb ; Divide by two (unsigned) stab 0,y ; Save in table2 inx ; Increment table1 pointer iny ; Increment table2 pointer deca ; Decrement counter bne l1 ; Counter!= 0 => more entries ; to divide swi ; Done table1: table2: org data dc.b $07,$c2,$3a,$68,$f3 ds.b count

9. Advanced: Optimize program to make use of instructions set efficiencies: ; Program to divide a table by two ; and store the results in memory prog: equ $1000 data: equ $2000 count: equ 5 org prog ; Set program counter to 0x1000 ldaa #count ; Use A as counter ldx #table1 ; Use X as data pointer to table1 ldy #table2 ; Use Y as data pointer to table2 l1: ldab 1,x+ ; Get entry from table1; then inc ptr. lsrb ; Divide by two (unsigned) stab 1,y+ ; Save in table2; then inc ptr. dbne a,l1 ; Decrement counter; if not 0, ; more to do swi ; Done table1: table2: org data dc.b $07,$c2,$3a,$68,$f3 ds.b count

TOP-DOWN PROGRAM DESIGN PLAN DATA STRUCTURES IN MEMORY START WITH A LARGE PICTURE OF THE PROGRAM STRUCTURE WORK DOWN TO MORE DETAILED STRUCTURE TRANSLATE STRUCTURE INTO CODE OPTIMIZE FOR EFFICIENCY DO NOT SACRIFICE CLARITY FOR EFFICIENCY