representation of numbers and characters number...represent 0 to r-1 e.g. decimal has 10 symbols, 0...

22
Representation of Numbers and Characters important to understand how numbers, characters, and the program are stored by a processor for assembly language, you need to understand this before we do any programming for high level languages, you should understand this so that your code doesn’t do stupid stuff like

Upload: others

Post on 27-Mar-2021

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Representation of Numbers and Characters Number...represent 0 to r-1 e.g. decimal has 10 symbols, 0 to 9 e.g. binary has 2 symbols, 0 to 1 e.g. hexadecimal has 16 symbols, 0–9 +

Representation of Numbers and Characters

important to understand how numbers, characters, and the program are stored by a processor

for assembly language, you need to understand this before we do any programming

for high level languages, you should understand this so that your code doesn’t do stupid stuff like …

Page 2: Representation of Numbers and Characters Number...represent 0 to r-1 e.g. decimal has 10 symbols, 0 to 9 e.g. binary has 2 symbols, 0 to 1 e.g. hexadecimal has 16 symbols, 0–9 +
Page 3: Representation of Numbers and Characters Number...represent 0 to r-1 e.g. decimal has 10 symbols, 0 to 9 e.g. binary has 2 symbols, 0 to 1 e.g. hexadecimal has 16 symbols, 0–9 +

Positional Number Systems

historical natural number system bases:3, 4, 5, 6, 8, 10, 12, 15, 20, 24, 27, 32, 60

computer system bases:2 (binary), 8 (octal), 16 (hexadecimal)

computer applications may use other bases:e.g. 58 (octoquinquagesimal) Bitcoin addresses: 123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz

Flicker short url: 123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ

Page 4: Representation of Numbers and Characters Number...represent 0 to r-1 e.g. decimal has 10 symbols, 0 to 9 e.g. binary has 2 symbols, 0 to 1 e.g. hexadecimal has 16 symbols, 0–9 +

Which base do you use? … binary? decimal? hexadecimal?

Use what is natural for the application

e.g. HTML 4.01 colour specification

06528010 =

Color Hex Color Hex Color Hex Color Hex

cyan 00FFFF gray 808080 navy 000080 silver C0C0C0

black 000000 green 008000 olive 808000 teal 008080

blue 0000FF lime 00FF00 purple 800080 white FFFFFF

fuchsia FF00FF maroon 800000 red FF0000 yellow FFFF00

Page 5: Representation of Numbers and Characters Number...represent 0 to r-1 e.g. decimal has 10 symbols, 0 to 9 e.g. binary has 2 symbols, 0 to 1 e.g. hexadecimal has 16 symbols, 0–9 +

Positional Number Systems

Positional system → “value” is based on the symbol and its position in the number

positional systems must have the concept of “zero” e.g. 26 or 206?

base r number system r unique symbols

represent 0 to r-1

e.g. decimal has 10 symbols, 0 to 9e.g. binary has 2 symbols, 0 to 1e.g. hexadecimal has 16 symbols, 0–9 + A-F

Page 6: Representation of Numbers and Characters Number...represent 0 to r-1 e.g. decimal has 10 symbols, 0 to 9 e.g. binary has 2 symbols, 0 to 1 e.g. hexadecimal has 16 symbols, 0–9 +

Conversions between number systems

a) any base to decimal→ expand the number into positional notation

and evaluate

e.g. 1102 = 1x22 + 1x21 + 0x20

e.g. A0116 = Ax162 + 0x161 + 1x160

Page 7: Representation of Numbers and Characters Number...represent 0 to r-1 e.g. decimal has 10 symbols, 0 to 9 e.g. binary has 2 symbols, 0 to 1 e.g. hexadecimal has 16 symbols, 0–9 +

Conversions between number systems

b) decimal to any base r→ integer portion – successive divide by r.

e.g. 1810 = ? 2 e.g. 256110 = ? 16

18/2=9 R= 0 2561/16 = 160 R= 1

9/2=4 R= 1 160/16 = 10 R= 0

4/2=2 R= 0 10/16 = 0 R=10

2/2=1 R= 0

1/2=0 R= 1

Page 8: Representation of Numbers and Characters Number...represent 0 to r-1 e.g. decimal has 10 symbols, 0 to 9 e.g. binary has 2 symbols, 0 to 1 e.g. hexadecimal has 16 symbols, 0–9 +

Conversions between number systems

c) binary ↔ hexadecimal

4 binary digits ≡ 1 hexadecimal digit

e.g. 11101100112 = ? 16

e.g. 46A116 = ? 2

Page 9: Representation of Numbers and Characters Number...represent 0 to r-1 e.g. decimal has 10 symbols, 0 to 9 e.g. binary has 2 symbols, 0 to 1 e.g. hexadecimal has 16 symbols, 0–9 +

Number Representation

a number is stored in a register or a memory location

the hardware and therefore the assembly language fix the length of the register and the memory location to a length “n”

for 68000, n = { 8, 16, 32 } bits≡ {byte, word, long word}

for ARM, n = {8, 16, 32} bits≡ {byte, half word, word}

Page 10: Representation of Numbers and Characters Number...represent 0 to r-1 e.g. decimal has 10 symbols, 0 to 9 e.g. binary has 2 symbols, 0 to 1 e.g. hexadecimal has 16 symbols, 0–9 +

Number Representation

(a)unsigned - number is assumed to be positive and no bits are used to represent the sign

e.g 000000012=

100000012=

for an n-bit register, smallest number largest number

00 … 00 11 … 11

Page 11: Representation of Numbers and Characters Number...represent 0 to r-1 e.g. decimal has 10 symbols, 0 to 9 e.g. binary has 2 symbols, 0 to 1 e.g. hexadecimal has 16 symbols, 0–9 +

Number Representation

(b) signed - number must have the following characteristics to be useful for calculations:

1. positive and negative numbers

2. sign test - easy way to determine if the number is positive or negative

3. zero test - easy way to identify zero

4. easy implementation of arithmetic operations. Operations, in order of importance, are:

add, subtract, multiply, divide

Page 12: Representation of Numbers and Characters Number...represent 0 to r-1 e.g. decimal has 10 symbols, 0 to 9 e.g. binary has 2 symbols, 0 to 1 e.g. hexadecimal has 16 symbols, 0–9 +

Number Representation

Ways to represent signed numbers:

1. sign-magnitude

in general: SXX … X e.g. 000000012 =100000012 =

both sign and magnitude are immediately obvious

two zeros

when adding numbers with opposite signs, magnitudes must be compared to determine the sign of the result

Page 13: Representation of Numbers and Characters Number...represent 0 to r-1 e.g. decimal has 10 symbols, 0 to 9 e.g. binary has 2 symbols, 0 to 1 e.g. hexadecimal has 16 symbols, 0–9 +

Number Representation

2. diminished-radix complement or "r-1" complement, e.g. if base 2, 1's complement; if base 10, 9's complement

in general:

positive # 0XXX…XX e.g. 000000012 =

negative # (r-1)YYY…YY 100000012 =

two zeros

sign immediately obvious

when adding numbers with opposite signs, the correct result can be produced with minor adjustment

Page 14: Representation of Numbers and Characters Number...represent 0 to r-1 e.g. decimal has 10 symbols, 0 to 9 e.g. binary has 2 symbols, 0 to 1 e.g. hexadecimal has 16 symbols, 0–9 +

Number Representation

3. radix complement = true complement = "r" complement, e.g. if base 2, 2's complement; if base 10, 10's complement

in general:

positive # 0XXX…XX e.g. 000000012=

negative # (r-1)YYY…YY 100000012=

one zero

sign immediately obvious

addition and subtraction result in

correct radix complement notation

Page 15: Representation of Numbers and Characters Number...represent 0 to r-1 e.g. decimal has 10 symbols, 0 to 9 e.g. binary has 2 symbols, 0 to 1 e.g. hexadecimal has 16 symbols, 0–9 +

Number Representation

recap, example using 8 bit register (or a byte in memory) and base 2

+6 -6unsignedsign-magnitude1’s complement2’s complement

Page 16: Representation of Numbers and Characters Number...represent 0 to r-1 e.g. decimal has 10 symbols, 0 to 9 e.g. binary has 2 symbols, 0 to 1 e.g. hexadecimal has 16 symbols, 0–9 +

Character Representation Characters are represented in ASCII **

[American Standard Code for Information Interchange]

e.g. character string '123' = 31 32 3316

the course will typically use the basic ASCII table with no extensions, i.e. the 7-bit encoding

in 68000, character strings are enclosed in single quotes, e.g. '123'

NOTE: character string '123' is NOT equal to the number 12310 which is stored as 7B16

** Most common current encoding systems are UTF-8 and ISO-8859-1which are designed to be backward compatible to ASCII

Page 17: Representation of Numbers and Characters Number...represent 0 to r-1 e.g. decimal has 10 symbols, 0 to 9 e.g. binary has 2 symbols, 0 to 1 e.g. hexadecimal has 16 symbols, 0–9 +

Assembler Directives … data definition

define constant initializes a memory location DC.B, DC.W, DC.L operand field = data

define constant block initializes a block of memory DCB.B, DCB.W, DCB.L operand fields = # of items, fillvalue

define storage reserves specified amount of memory actual contents of memory vary by situation DS.B, DS.W, DS.L operand field = # of items

1 byte = 8 bits .

= 2 hex digits

1 word = 16 bits

= 4 hex digits

1 longword = 32 bits .

= 8 hex digits

Page 18: Representation of Numbers and Characters Number...represent 0 to r-1 e.g. decimal has 10 symbols, 0 to 9 e.g. binary has 2 symbols, 0 to 1 e.g. hexadecimal has 16 symbols, 0–9 +

Assembler Directives … data definition

memory is “byte addressable” every byte has an address

memory is composed of 8 bit chunks

memory dumps are shown in hexadecimal

e.g.

ITEM DC.W $D0

LIST_L DC.W 4

LIST DC.B $C1,20,-1,%11010000

INDEX DS.L 1

ERR_M DC.B ꞌErrorꞌ,0

ONES DCB.B 5,$FF

Page 19: Representation of Numbers and Characters Number...represent 0 to r-1 e.g. decimal has 10 symbols, 0 to 9 e.g. binary has 2 symbols, 0 to 1 e.g. hexadecimal has 16 symbols, 0–9 +

Assembler Directives … data definition

ORG $1006 Symbol Table

ITEM DC.W $D0 ITEM

LIST_L DC.W 4 LIST_L

LIST DC.B $C1,20,-1,%11010000 LIST

INDEX DS.L 1 INDEX

ERR_M DC.B ‘Error’,0 ERR_M

ONES DCB.B 5,$FF ONES

Memory dump format:

00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

1000

Page 20: Representation of Numbers and Characters Number...represent 0 to r-1 e.g. decimal has 10 symbols, 0 to 9 e.g. binary has 2 symbols, 0 to 1 e.g. hexadecimal has 16 symbols, 0–9 +

Assembler Directives … data definition

ORG $1006 ** Word and Longword data

ITEM DC.W $D0 must start on an even boundary

LIST_L DC.W 3 (1) user forces even boundary

LIST DC.B $C1,$A3,$3A with DS.W 0

INDEX DS.L 1 (2) or handled by assembler

ERR_M DC.B ‘Error’,0

ONES DCB.W 3,$FF

00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

1000

Page 21: Representation of Numbers and Characters Number...represent 0 to r-1 e.g. decimal has 10 symbols, 0 to 9 e.g. binary has 2 symbols, 0 to 1 e.g. hexadecimal has 16 symbols, 0–9 +

Number Representation

You wrote:

list dc.b $20, ꞌ1ꞌ, 1, 12, -2, %1010

In the memory dump (hex), your list will look like:

20 31 01 0C FE 0A

For calculations, the computer will use the binary equivalent:

0010 0000 0011 0001 0000 0001

0000 1100 1111 1110 0000 1010

Page 22: Representation of Numbers and Characters Number...represent 0 to r-1 e.g. decimal has 10 symbols, 0 to 9 e.g. binary has 2 symbols, 0 to 1 e.g. hexadecimal has 16 symbols, 0–9 +

Reading & Expectations:Reading:

Both of the following references review this material: Digital Fundamentals (any version), Chapter on Number Systems [CP/PC220 text]

e.g. Digital fundamentals with VHDL, Floyd, Thomas L., TK7868.D5 F534, @Laurier Library

Data Representation [pdf 32p, chapter 3 Data Representation from Art of Assembly Language, Randall Hyde] section 3.2 – number systems section 3.3 – data organization section 3.4 – hexadecimal number system section 3.7 – logical operations section 3.9 – signed and unsigned numbers (omit code examples at end of section)

An ASCII table can be found at the end of M68000 Assembly Language [pdf, 92p; N. Znotinas]

For interest: Alternative Number Systems used in films, books, gamesList of numeral systems [Wikipedia]

Expectations: should be able to do all exercises in any of the above references should be comfortable working in hexadecimal and binary