Presentation is loading. Please wait.

Presentation is loading. Please wait.

Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Chapter 3 – Digital Logic and Binary Numbers These are lecture notes to accompany.

Similar presentations


Presentation on theme: "Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Chapter 3 – Digital Logic and Binary Numbers These are lecture notes to accompany."— Presentation transcript:

1 Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Chapter 3 – Digital Logic and Binary Numbers These are lecture notes to accompany the book SPARC Architecture, Assembly Language Programming, and C, by Richard P. Paul, 2 nd edition, 2000. By Michael Weeks

2 Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Binary A computer is a “bistable” device A bistable device: –Easy to design and build –Has 2 states: 0 and 1 One Binary digit (bit) represents 2 possible states (0, 1)

3 Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C With 2 bits, 4 states are possible (2 2 = 4) With n bits, 2 n states are possible With 3 bits, 8 states are possible (2 3 = 8) Bit 1 Bit 0 State 00 1 01 2 10 3 11 4 Bit 2 Bit 1 Bit 0 State 0001 0012 0103 0114 1005 1016 1107 1118

4 Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Binary Coded Decimal (BCD) Why not use 4 bits to represent decimal? Let 0000 represent 0 Let 0001 represent 1 Let 0010 represent 2 Let 0011 represent 3, etc. –This is called BCD –Only uses 10 of the 16 possibilities

5 Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Binary Number System From left to right, the position of the digit indicates its magnitude (in decreasing order) –E.g. in decimal, 123 is less than 321 –In binary, 011 is less than 100 A subscript indicates the number’s base –E.g. is 100 decimal or binary? We don’t know! –But 14 10 = 1110 2 is clear

6 Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Bytes A group of 8 bits is a byte A byte can represent 2 8 = 256 possible states Registers are usually a multiple of bytes SPARC registers have 32 bits (4 bytes) 2 32 = 4,294,967,296

7 Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Memory Addresses Memory addresses are in binary –often 32 bits, these days –if each memory address maps to 1 byte: 2 32 bytes = 4 GB K = kilo = thousand, but 1KB actually means 1024 bytes 1MB = 1024 x 1024 bytes 1GB = 1024 x 1024 x 1024 bytes

8 Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Octal and Hexadecimal It is difficult for a human to work with long strings of 0’s and 1’s Octal and Hexadecimal are ways to group bits together Octal: base 8 Hexadecimal: base 16

9 Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Hexadecimal With 4 bits, there are 16 possibilities Use 0, 1, 2, 3, …9 for the first 10 symbols Use a, b, c, d, e, and f for the last 6 Bit 3 Bit 2 Bit 1 Bit 0 Symbol 000 00 000 11 001 02 001 13 010 04 010 15 011 06 011 17 100 08 100 19 101 0a 101 1b 110 0c 110 1d 111 0e 111 1f

10 Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Binary to Hexadecimal 0101011010110011 2 = ? in hex Group into 4 bits, from the right: 0101, 0110, 1011, 0011 2 Now translate each (see previous table): 0101 2 => 5, 0110 2 => 6, 1011 2 => b, 0011 2 => 3 So this is 56b3 16 What if there are not enough bits? –Pad with 0’s on the left

11 Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Hexadecimal to Binary f0e5 16 = ? in binary Translate each into a group of 4 bits: f 16 => 1111 2, 0 16 => 0000 2, e 16 => 1110 2, 5 16 => 0101 2 So this is 1111000011100101 2

12 Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Decimal to Any Number Base Take the decimal number, and divide by the new number base Keep track of the quotient and remainder Repeat until quotient = 0 Read number from the bottom to the top

13 Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Decimal to Binary Binary is base 2 Example: convert 35 (decimal) to binary QuotientRemainder 35 / 2 = 171 17 / 2 = 81 8 / 2 = 40 4 / 2 = 20 2 / 2 = 10 1 / 2 = 01 So 35 10 = 100011 2

14 Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Any Number Base to Decimal From right to left, multiply the digit of the number-to-convert by its base position Sum all results

15 Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Binary to Decimal Binary is base 2 Example: convert 10110 (binary) to decimal 10110 2 = 1x2 4 + 0x2 3 + 1x2 2 + 1x2 1 + 0x2 0 = 1x16 + 0x8 + 1x4 + 1x2 + 0x1 = 16 + 0 + 4 + 2 + 0 =22 So 10110 2 = 22 10

16 Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Hexadecimal to Decimal Hexadecimal is base 16 Example: convert 16 (hex) to decimal 16 16 = 1x16 1 + 6x16 0 = 1x16 + 6x1 = 16 + 6 =22 So 16 16 = 22 10 Not surprising, since 16 16 = 0001, 0110 2 –If one of the hex digits had been > 9, say c, then we would have used 12 in its place.

17 Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C ASCII American Standard Code for Information Interchange Use byte values to represent characters The assembler allows double-quotes mov0x4d, %r3! Moves capital M to register 3 mov“M”, %r3! This command does the same

18 Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C ASCII chart

19 Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Bitwise Logical Operations There are several binary operations: –NOT –AND –OR –XOR –NAND –NOR

20 Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C NOT The NOT operation simply complements a binary value –not (a) –a’ anot(a) 01 10

21 Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C AND The AND operation uses 2 binary values –a and b aba and b 000 010 100 111

22 Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C OR The OR operation uses 2 binary values –a or b aba or b 000 011 101 111

23 Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C XOR The XOR (exclusive-or) operation uses 2 binary values True when only one input is true. –a xor b aba xor b 000 011 101 110

24 Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C NAND The NAND (Not-AND) operation uses 2 binary values Take the AND function, and complement it. –a nand b aba nand b 001 011 101 110

25 Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C NOR The NOR (Not-OR) operation uses 2 binary values Take the OR function, and complement it. NAND and NOR are easy to make on a chip. Why? Take CSc 4250 and find out! aba nor b 001 010 100 110

26 Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Possible Logic Functions Suppose you have 2 binary digits: a, b Imagine that some function operates on them to create c. What could this function be? –There are only 16 possibilities –And some of these are not useful! abab c some function

27 Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Logic Operations A0011LogicalSparc B0101 00000 false 10001 a and band 20010 a and (not b)andn 30011 a 40100 b and (not a) 50101 b 60110 a xor bxor 70111 a or bor 81000 a nor b 91001 a xor (not b)xnor A1010 not b B1011 a or (not b)orn C1100 not a D1101 b or (not a) E1110 a nand b F1111 true

28 Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Bitwise Each of these logic functions is a bitwise operation, meaning that the result is independent of the bits to the left or right e.g. 1 0 1 or 0 1 1 1 1 1 compare this with addition

29 Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Logic Instruction Examples mov 0x21, %l0 and %l0, 0x3c, %l1 mov 0x21, %l0 or %l0, 0x3c, %l1 mov 0x55, %l0 xnor %l0, 0x3c, %l1 mov 0x55, %l0 xor %l0, 0x3c, %l1 mov 0x47, %l0 and %l0, 0xca, %l1 mov 0x47, %l0 andn %l0, 0xca, %l1 mov 0x47, %l0 or %l0, 0xca, %l1 mov 0x47, %l0 orn %l0, 0xca, %l1 mov 0x55, %l0 not %l0 20 3d ffffff96 69 42 5 cf ffffff77 ffffffaa

30 Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C A Few More Logic Examples and %l0, %l1, %l1 or %l0, %l1, %l1 123456780 9abcdef8 88888888 edcba987 xor %l0, %l1, %l1 not %l0, %l1 In all the examples below, these registers have the following initial values: %l0 = 0x12345678 %l1 = 0x9abcdef0 What are the values for %l1 after the instruction?

31 Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C SPARC Instruction Format These commands are in the form: commandsource register 1, source register 2, destination register commandsource register 1, immediate value, destination register command can be any of the following: and, andn, xor, or, xnor, orn andcc, andncc, xorcc, orcc, xnorcc, orncc the cc means “set condition codes” andn means a and (not b) orn means a or (not b)

32 Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C SPARC Logical Instruction Example cmp%a_r, 0 blenext nop add%b_r, 1, %b_r next: This is equivalent to: if (a > 0) b++; %a_r and %b_r will be replaced by the actual registers, such as %r2 and %r3

33 Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Synthetic Instructions The cmp command is a synthetic one. It is a macro that uses %g0. The above cmp command will be expanded to: subcc%a_r, %g0, %g0 Also, the tst command compares a register to 0: tst%a_r which the assembler turns into: orcc%a_r, %g0, %g0 Since %g0 ignores any updates, only the condition codes are affected.

34 Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Flags Since individual bits are used to represent boolean flags, a word may contain 32 flags. Common flag operations and mnemonics –set:bset( done with or ) –clear:bclr( done with andn ) –toggle:btog( done with xor )

35 Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Testing Flags This command will see if one or more flags is set btst reg_or_imm, reg rs1 it expands to: andccreg rs1, reg_or_imm, %g0 (notice how the operands are switched) example: test if flag 0x02 is set btst0x02, %a_r be clear nop set: clear:


Download ppt "Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Chapter 3 – Digital Logic and Binary Numbers These are lecture notes to accompany."

Similar presentations


Ads by Google