24
M68K Assembly Language Programming Bob Britton Chapter 4 Number Systems

M68K Assembly Language Programming Bob Britton Chapter 4 Number Systems

  • View
    220

  • Download
    0

Embed Size (px)

Citation preview

Page 1: M68K Assembly Language Programming Bob Britton Chapter 4 Number Systems

M68K Assembly Language Programming

Bob Britton

Chapter 4

Number Systems

Page 2: M68K Assembly Language Programming Bob Britton Chapter 4 Number Systems

Number Systems• Introduction

• Polynomial Expansion

• Binary Numbers

• Hexadecimal Numbers

• Two’s Complement Number System

• Arithmetic & Overflow Detection

• American Standard Code for Information Interchange (ASCII)

Page 3: M68K Assembly Language Programming Bob Britton Chapter 4 Number Systems

Polynomial Expansion of a Decimal Number (Base 10)

496 = 4 x 10 + 9 x 10 + 6 x 10

Polynomial Expansion of a Binary Number (Base 2)Polynomial Expansion of a Binary Number (Base 2)

2 1 0

10

00101101 = 1 x 2 + 0 x 2 + 1 x 2 + 1 x 2 + 0 x 2 + 1x 2

5 4 3 2 1 0

2

A Faster Method ------ Double and AddA Faster Method ------ Double and Add

00101101 = 45 (1, 2, 5, 11, 22, 45)

Page 4: M68K Assembly Language Programming Bob Britton Chapter 4 Number Systems

Conversion of Decimal Numbers to Binary

Divide by 2 and extract the remainder

45 Remainder 1 0 1 1 0 1

22 1

11 0

5 1

2 1

1 0

0 1

Page 5: M68K Assembly Language Programming Bob Britton Chapter 4 Number Systems

Practice - Convert 25 to BinaryDivide by 2 and record the remainder

25 Remainder

12

Page 6: M68K Assembly Language Programming Bob Britton Chapter 4 Number Systems

To represent binary values in the positive and negative domains we use the

Two’s Complement Number SystemHere is the polynomial expansion of a two’s

complement 8-bit binary number N:

N = - d7x2 + d6x2 + d5x2 + d4x2 + d3x2 + d2x2 + d1x2 +d0x2

Notice the Minus sign

*** You need to memorize powers of 2 ***

7 56 4 3 2 1 0

Page 7: M68K Assembly Language Programming Bob Britton Chapter 4 Number Systems

The Two’s Complement Operation

When we take the two’s complement of a binary number, the result will be the negative of the value we started with.

For example, the binary value 00011010 is 26 in decimal.

To find the value negative 26 (-26) in binary we perform the two’s complement operation on 00011010.

Scan the binary number from right to left leaving all least significantzeros (0) and the first one (1) unchanged, and then complementing theremaining digits to the left: 11100110

The result is the value negative 26 (-26) in binary.

Page 8: M68K Assembly Language Programming Bob Britton Chapter 4 Number Systems

Binary Arithmetic & Overflow Detectionin the Two’s Complement Number System

Here is an addition example where we assume we are limited to 8 binary digits.

01000100 = 68

+ 00111100 = 6010000000 = -128 Overflow Occurred

Page 9: M68K Assembly Language Programming Bob Britton Chapter 4 Number Systems

Overflow0000

0010

0001

0011

0100

0101

0110

0111

10001001

1010

1011

1101

1110

1111

1

2

0

3

4

5

6

7-8

-7

-6

-5

-4

-3

-2

-1

1100

Page 10: M68K Assembly Language Programming Bob Britton Chapter 4 Number Systems

Binary Arithmeticin the Two’s Complement Number System

Here is a subtraction example where we assume we are limited to 8 binary digits. To subtract in binary we always add the two’s complement of the subtrahend.

01000100 = 01000100 68

-00111100 = +11000100 60

00001000 = 00001000 = 8

Page 11: M68K Assembly Language Programming Bob Britton Chapter 4 Number Systems

The Rule for Detection of Overflow

#################################################Adding numbers of opposite signs, overflow is impossible.

When adding numbers of the same sign, if the result is not the same as the operands then overflow occurred.

#################################################Here is an example:You are given the following two numbers in two’s complement representation.Perform the binary subtraction and indicate if there is signed overflow. ______Explain Why:

11101000-00010011

11101000 = -24+11101101 = -19 11010101 Correct

Result = -43

Page 12: M68K Assembly Language Programming Bob Britton Chapter 4 Number Systems

Sign Extension

The value – 43 as an 8-bit binary number is: 11010101The value – 43 as an 32-bit binary number is: 11111111111111111111111111010101In Hexadecimal – 43 appears as: 0xFFFFFFD5

###############################################

The value 68 as an 8-bit binary number is: 01000100 The value 68 as an 32-bit binary number is: 00000000000000000000000001000100In Hexadecimal 68 appears as: 0x00000044

Page 13: M68K Assembly Language Programming Bob Britton Chapter 4 Number Systems

The Hexadecimal Number SystemDecimal Hex

Binary0 0 00001 1 00012 2 00103 3 00114 4 01005 5 01016 6 01107 7 01118 8 10009 9 100110 A 101011 B 101112 C 110013 D 110114 E 111015 F 1111

Here is an example of how wecompactly represent binary numbers in hexadecimal:

| | | | | 001111001000111101111110

$ 3 C 8 F 7 E

Page 14: M68K Assembly Language Programming Bob Britton Chapter 4 Number Systems

Multiplication by constants that are a Power of 2 is accomplished much more efficiently using the Arithmetic Shift Left instruction*.

ASL.L #3, D0 * D0*8 --> D0bvs overflow

*See Appendix B for a description of the ASL instruction

Page 15: M68K Assembly Language Programming Bob Britton Chapter 4 Number Systems

Division by constants that are a Power of 2 is accomplished much more efficiently using the Arithmetic Shift Right instruction

ASR.L #1, D0 * D0/2 --> D0

*See Appendix B for a description of the ASR instruction

Page 16: M68K Assembly Language Programming Bob Britton Chapter 4 Number Systems

MIPS Assembly Language Programming

Bob Britton

Chapter 4 Exercises

Number Systems

Page 17: M68K Assembly Language Programming Bob Britton Chapter 4 Number Systems

Exercises4.1 Convert the decimal number 35 to an 8-bit binary number.

4.2 Convert the decimal number 32 to an 8-bit binary number.

4.3 Using the double and add method convert 00010101 to a decimal number.

4.4 Using the double and add method convert 00011001 to a decimal number.

4.5 Explain why the Least Significant digit of a binary number indicates if the number is odd or even.

4.6 Convert the binary number 00010101 to a hexadecimal number.

4.7 Convert the binary number 00011001 to a hexadecimal number.

4.8 Convert the hexadecimal number $15 to a decimal number.

4.9 Convert the hexadecimal number $19 to a decimal number.

4.10 Convert the decimal number -35 to an 8-bit two’s complement binary number.

00100011

00100000

21

25

LSD is a 1

$15

$19

21

25

11011101

Page 18: M68K Assembly Language Programming Bob Britton Chapter 4 Number Systems

Exercises4.11 Convert the decimal number -32 to an 8-bit two’s complement binary number.

4.12 Assuming the use of the two’s complement number system find the equivalent decimal values for the following 8-bit binary numbers:(a) 10000001(b) 11111111(c) 01010000(d) 11100000(e)            10000011

4.13 Convert the base 8 number 204 to decimal

4.14 Convert the base 7 number 204 to decimal

4.15 Convert the base 6 number 204 to decimal

4.16 Convert the base 5 number 204 to decimal

4.17 Convert the base 10 number 81 to a base 9 number.

11100000

-127 -1 80 -32-125

132

102

76

54

100

Page 19: M68K Assembly Language Programming Bob Britton Chapter 4 Number Systems

Exercises4.18 For each row of the table below convert the given 16 bit number to each of the other two bases, assuming the two’s complement number system is used.

 16 Bit Binary Hexadecimal Decimal

1111111100111100

$FF88

-128

1111111111111010

$0011

-25 4.19 You are given the following two numbers in two’s complement representation.

Perform the binary addition and indicate if there is signed overflow. __Explain Why:

0110111000011010

10001000 Yes overflow occurred – Sign of the result is different from the operands

Page 20: M68K Assembly Language Programming Bob Britton Chapter 4 Number Systems

Exercises4.21 You are given the following two numbers in two’s complement representation.

Perform the binary subtraction and indicate if there is signed overflow. ______Explain Why:

4.22 Sign extend the 8 bit hex number $88 to a 16 bit number. $_________ 

4.23 The first subtract instruction is located at address $00001234. What are the two possible values for the contents of the PC after the branch instruction is executed? $_____________ $ ____________

loop: sub #8, D4

sub D4, D2

bne loop

11101000-00010011

11101000+ 11101101 11010101 No Overflow

FF88

00001234 00001240

Page 21: M68K Assembly Language Programming Bob Britton Chapter 4 Number Systems

Exercises

4.24 You are given the following two 8-bit binary numbers in the two’s complement number system. What values do they represent in decimal?

X = 10010100 = __________ Y = 00101100 = __________ 2 10 2 10

Perform the following arithmetic operations on X and Y. Show your answers as 8-bit binary numbers in the two’s complement number system. To subtract Y from X, find the two’s complement of Y and add it to X. Indicate if overflow occurs in performing any of these operations.

X+Y X-Y Y-X

10010100 10010100 0010110000101100

-108 44

+11010100 +0110110011000000 01101000 10011000

Page 22: M68K Assembly Language Programming Bob Britton Chapter 4 Number Systems

Exercise 4.25

The following code segment is stored in memory starting at memory location $0001234. What are the two possible values for the contents of the PC after the branch instruction has executed? $__________ $ ____________Add in line pseudocode to describe each instruction.

loop: move (A0), D0 *adda #4, A0 *and #1, D0 *beq loop *

0001234 0001240

D0 = Mem[A0]A0 = A0 + 4D0 = D0 & 1 “Extract LSD”if D0 is an even # go to loop

Page 23: M68K Assembly Language Programming Bob Britton Chapter 4 Number Systems

Example QuizYou are given the following two 8 bit binary numbers in the two’s

complement number system. What values do they represent in decimal?

X= 10010100 = 2

Y= 00101100 = 2

Perform the following arithmetic operations on X and Y. Show your answers as 8 bit binary numbers in the two’s complement number system. To subtract Y from X find the two’s complement of Y and add it to X. Indicate if overflow occurs in performing any of these operations.

X+Y X-Y Y-X

10010100 10010100 0010110000101100

Page 24: M68K Assembly Language Programming Bob Britton Chapter 4 Number Systems

Beginning of the PrintHex Function

######################################### Algorithmic Description in Pseudo Code:##########################################

.globl PrintHex

.databuffer: .asciiz " 0x00000000"

.textPrintHex:

la $a1, bufferaddi $a1, $a1, 10 # Set pointer to end of buffer

......... # Body of the Algorithm...jr $ra