Presentation is loading. Please wait.

Presentation is loading. Please wait.

M68K Assembly Language Programming Bob Britton Chapter 4 Number Systems.

Similar presentations


Presentation on theme: "M68K Assembly Language Programming Bob Britton Chapter 4 Number Systems."— Presentation transcript:

1

2 M68K Assembly Language Programming Bob Britton Chapter 4 Number Systems

3 Introduction Polynomial Expansion Binary Numbers Hexadecimal Numbers Two’s Complement Number System Arithmetic & Overflow Detection American Standard Code for Information Interchange (ASCII)

4 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) 210 10 00101101 = 1 x 2 + 0 x 2 + 1 x 2 + 1 x 2 + 0 x 2 + 1x 2 543210 2 A Faster Method ------ Double and Add 00101101 = 45 (1, 2, 5, 11, 22, 45)

5 Conversion of Decimal Numbers to Binary Divide by 2 and extract the remainder 45 Remainder1 0 1 1 0 1 221 110 51 21 10 01

6 Practice - Convert 25 to Binary Divide by 2 and record the remainder 25 Remainder 12

7 To represent binary values in the positive and negative domains we use the Two’s Complement Number System Here is the polynomial expansion of a two’s complement 8-bit binary number N: N = - d 7 x2 + d 6 x2 + d 5 x2 + d 4 x2 + d 3 x2 + d 2 x2 + d 1 x2 +d 0 x2 Notice the Minus sign *** You need to memorize powers of 2 *** 7 564321 0

8 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 significant zeros (0) and the first one (1) unchanged, and then complementing the remaining digits to the left: 11100110 The result is the value negative 26 (-26) in binary.

9 Binary Arithmetic & Overflow Detection in the Two’s Complement Number System Here is an addition example where we assume we are limited to 8 binary digits. 01000100= 68 +00111100= 60 10000000 = -128 Overflow Occurred

10 Overflow 0000 0010 0001 0011 0100 0101 0110 0111 1000 1001 1010 1011 1101 1110 1111 1 2 0 3 4 5 6 7 -8 -7 -6 -5 -4 -3 -2 1100

11 Binary Arithmetic in 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

12 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

13 Sign Extension The value – 43 as an 8-bit binary number is: 11010101 The value – 43 as an 32-bit binary number is: 11111111111111111111111111010101 In 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: 00000000000000000000000001000100 In Hexadecimal 68 appears as: 0x00000044

14 The Hexadecimal Number System DecimalHexBinary 000000 110001 220010 330011 440100 550101 660110 770111 881000 991001 10A1010 11B1011 12C1100 13D1101 14E1110 15F1111 Here is an example of how we compactly represent binary numbers in hexadecimal: | | | | | 001111001000111101111110 $ 3 C 8 F 7 E

15 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 --> D0 bvsoverflow *See Appendix B for a description of the ASL instruction

16 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

17 MIPS Assembly Language Programming Bob Britton Chapter 4 Exercises Number Systems

18 Exercises 4.1Convert the decimal number 35 to an 8-bit binary number. 4.2Convert the decimal number 32 to an 8-bit binary number. 4.3Using the double and add method convert 00010101 to a decimal number. 4.4Using the double and add method convert 00011001 to a decimal number. 4.5Explain why the Least Significant digit of a binary number indicates if the number is odd or even. 4.6Convert the binary number 00010101 to a hexadecimal number. 4.7Convert the binary number 00011001 to a hexadecimal number. 4.8Convert the hexadecimal number $15 to a decimal number. 4.9Convert the hexadecimal number $19 to a decimal number. 4.10Convert 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

19 Exercises 4.11Convert the decimal number -32 to an 8-bit two’s complement binary number. 4.12Assuming 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.13Convert the base 8 number 204 to decimal 4.14Convert the base 7 number 204 to decimal 4.15Convert the base 6 number 204 to decimal 4.16Convert the base 5 number 204 to decimal 4.17Convert the base 10 number 81 to a base 9 number. 11100000 -127 80 -32 -125 132 102 76 54 100

20 Exercises 4.18For 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 BinaryHexadecimalDecimal 1111111100111100 $FF88 -128 1111111111111010 $0011 -25 4.19You are given the following two numbers in two’s complement representation. Perform the binary addition and indicate if there is signed overflow. __ Explain Why: 01101110 00011010 10001000 Yes overflow occurred – Sign of the result is different from the operands

21 Exercises 4.21You 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.22Sign extend the 8 bit hex number $88 to a 16 bit number. $_________ 4.23The 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 subD4, D2 bneloop 11101000 -00010011 11101000 + 11101101 11010101No Overflow FF88 0000123400001240

22 Exercises 4.24You 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+YX-YY-X 1001010010010100 00101100 00101100 -10844 +11010100 +01101100 110000000110100010011000

23 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* beqloop * 0001234 0001240 D0 = Mem[A0] A0 = A0 + 4 D0 = D0 & 1 “Extract LSD” if D0 is an even # go to loop

24 Example Quiz 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 = 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+YX-YY-X 1001010010010100 00101100 00101100

25 Beginning of the PrintHex Function ######################################## # Algorithmic Description in Pseudo Code: # #########################################.globlPrintHex.data buffer:.asciiz" 0x00000000".text PrintHex: la$a1, buffer addi$a1, $a1, 10# Set pointer to end of buffer......# Body of the Algorithm... jr$ra


Download ppt "M68K Assembly Language Programming Bob Britton Chapter 4 Number Systems."

Similar presentations


Ads by Google