Before we begin to understand how to program in assembly it is best to try to understand how numbers are represented in computers. Numbers are stored in binary Numbers!. Let’s begin with the most commonly used numbering systems and see why computers use the binary numbers among all others numbering system.
Base is 10 0123456789
Base is 2 01
Base is 16 0123456789ABCDEF
In all numbering systems we can do arithmetic operations like addition subtraction and multiplication:
1-Decimal addition
1 + 2 = 3
1 + 3 = 4
1 + 9 =10
3 + 9 = 12
2- Binary addition
1 + 0 = 1
1 + 1 = 10
3- Hex addition
1 + 9 = A
1 + E = F
1 + F = 10
Binary/ Decimal/ Hex conversion table from 0 to 15
Binary(4 digits) |
Hex(1 digit) |
Decimal(2 digits) |
0000 |
0 |
0 |
0001 |
1 |
1 |
0010 |
2 |
2 |
0011 |
3 |
3 |
0100 |
4 |
4 |
0101 |
5 |
5 |
0110 |
6 |
6 |
0111 |
7 |
7 |
1000 |
8 |
8 |
1001 |
9 |
9 |
1010 |
A |
10 |
1011 |
B |
11 |
1100 |
C |
12 |
1101 |
D |
13 |
1110 |
E |
14 |
1111 |
F |
15 |
Examples
Given the following decimal numbers, Use binary and hex to do the following arithmetic operation:
0
1
1
1
1
30
15
7
3
1
0
34
17
8
4
2
1
0
0 L.O
1
0
0
0
1 H.O
0010 0010
0001 1110
--------------
0100 0000
12
6
3
1
0
19
9
4
2
1
0
1
1
0
0
1
0
0
1
1
0001 0011
0000 1100
--------------
0001 1111
(1) Hex
0010 0010 à 2 2
0001 1110 à 1 E
-------------- -----
0100 0000 4 0
(2) Hex
0001 0011 à 1 3
0000 1100 à 0 C
-------------- -----
0001 1111 1 F
(3) Binary
34-30 =34+(-30)
0010 0010 - 0001 1110 = 0010 0010 + (two’s complement of 0001 1110)
two’s complement of 0001 1110 = one’s complement +1
one’s complement == inversion of 1 to 0
(30) 0001 1110
(1’s comp) 1110 0001
(2’s comp ) 1110 0001+1= 1110 0010
(-30) 1110 0010
finally
(34-30) is
0010 0010
1110 0010
--------------
0000 0100
fig 1
Function (Assembly instruction) |
Controls value (Machine Language) |
||||||
A |
B |
C |
D |
E |
F |
G |
|
mix red |
1 |
0 |
0 |
0 |
0 |
0 |
0 |
mix yellow |
0 |
1 |
0 |
0 |
0 |
0 |
0 |
mix blue |
0 |
0 |
1 |
0 |
0 |
0 |
0 |
mix purple |
1 |
0 |
1 |
0 |
0 |
0 |
0 |
mix green |
0 |
1 |
1 |
0 |
0 |
0 |
0 |
mix orange |
1 |
1 |
0 |
0 |
0 |
0 |
0 |
mix black |
1 |
1 |
1 |
0 |
0 |
0 |
0 |
mix white |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
Paint |
0 |
0 |
0 |
1 |
0 |
0 |
0 |
move left |
0 |
0 |
0 |
0 |
1 |
0 |
0 |
move right |
0 |
0 |
0 |
0 |
1 |
0 |
1 |
move forwards |
0 |
0 |
0 |
0 |
0 |
1 |
0 |
move backwards |
0 |
0 |
0 |
0 |
0 |
1 |
1 |
halt |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
Table 1
Mix means dispense paint into the funnel, where it will mix itself.
Paint means snap open the valve on the bottom of the funnel, so it will paint the pixel on the piece of paper that is currently under the funnel.
Move means move the paper one pixel either left-right or forward-backward.
Assignments
[1] -Calculate the following operations:
1) 10011011b - 00011010b
2) 2374h + 56A3h
2) 13FFh + 0FA8h
4) 00110111b - 01001001b
4) 36 – 31 in binary
[2] – For figure #1, write a short program draw a horizontal line with orange color and length 5 pixels. The program should be in machine and assembly language shown in table1.
Bit 0 |
Binary Digit (BIT)
Byte
Byte
1 BYTE
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
2 NIBBLES
8 BITS
1 WORD
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
2 BYTES
4 NIBBLES
16 BITS
In general, with n bits we can represent the signed values in
the range -2n-1 to +2n-1-1 or unsigned values in the range 0 to 2n
For 16-bit numbers:
8000h is negative because the H.O. bit is one.
100h is positive because the H.O. bit is zero.
7FFFh is positive.
FFFFh is negative.
0FFFh is positive.
To convert a positive number to its negative, two’s complement form, you use
the following algorithm:
1) Invert all the bits in the number, i.e., apply the logical NOT function.
2) Add one to the inverted result.
For example, to compute the eight bit equivalent of -5:
0000 0101 Five (in binary).
1111 1010 Invert all the bits.
1111 1011
To convert the numbers above to their negative counterpart (i.e., to negate them), do the
following:
7FFFh: 0111 1111 1111 1111 +32,767
1000 0000 0000 0000 Invert all the bits (8000h)
1000 0000 0000 0001 Add one (8001h or -32,767t)
8000h: 1000 0000 0000 0000 -32,768
0111 1111 1111 1111 Invert all the bits (7FFFh)
1000 0000 0000 0000 Add one (8000h or -32768t)
4000h: 0100 0000 0000 0000 16,384
1011 1111 1111 1111 Invert all the bits (BFFFh)
1100 0000 0000 0000 Add one (0C000h or -16,384t)
Note: we don’t write the +ive equivalent in signed numbered. Instead we use the two’s complement to use the same H/W and algorithm for both signed and unsigned
A code number to represent each character in the keyboard
ASCII Code is 69
ASCII Code is 101
Char (shape) |
Dec |
Hex |
“0” |
48 |
30h |
“1” |
49 |
31h |
“2” |
50 |
32h |
“3” |
51 |
33h |
“4” |
52 |
34h |
“5” |
53 |
35h |
“6” |
54 |
36h |
“7” |
55 |
37h |
“8” |
56 |
38h |
“9” |
57 |
39h |
1- Examples of sign extension:
8 Bits |
16 Bits |
32 Bits |
80h |
FF80h |
FFFFFF80h |
28h |
0028h |
00000028h |
9Ah |
FF9Ah |
FFFFFF9Ah |
7Fh |
007Fh |
0000007Fh |
2- extend an unsigned number
8 Bits |
16 Bits |
32 Bits |
80h |
0080h |
00000080h |
28h |
0028h |
00000028h |
9Ah |
009Ah |
0000009Ah |
7Fh |
007Fh |
0000007Fh |
3- sign contraction
FF80h can be sign contracted to 80h
0040h can be sign contracted to 40h
FE40h cannot be sign contracted to 8 bits.
0100h cannot be sign contracted to 8 bits.
The rule : bytes you wish to remove must all contain either 00h or FFh.
True 1
False 0
0 and 0 = 0
0 and 1 = 0
1 and 0 = 0
1 and 1 = 1
2- OR
0 or 0 = 0
0 or 1 = 1
1 or 0 = 1
1 or 1 = 1
3- XOR
0 xor 0 = 0
0 xor 1 = 1
1 xor 0 = 1
1 xor 1 = 0
With “ • “ represents AND
With “ + “ represents OR
With “A’ “ represents NOT A
With “ + “ represents OR
Th1: |
A + A = A |
Th2: |
A • A = A |
Th3: |
A + 0 = A |
Th4: |
A • 1 = A |
Th5: |
A • 0 = 0 |
Th6: |
A + 1 = 1 |
Th7: |
(A + B)’ = A’ • B’ |
Th8: |
(A • B)’ = A’ + B’ |
Th15: |
A + A’ = 1 |
Th16: |
A • A’ = 0 |
W = AB |
A and B |
X = A + B |
A or B |
Y = A’B + AB’ |
A xor B |
Z = A’ |
Not A |
Example
ABC+A’BC+AB’C+A’B’C+ABC’ = BC(A+A’) + B’C(A+A’) + ABC’
= BC•1 +B’C•1 + ABC’
= C(B+B’) + ABC’
= C + ABC’
in C++ we can express the above Boolean expression as
if(C || (A && B && !C) )
{
...
}
Assignments
- Sign extend the following hex numbers to 16-bits.
|
|
|
|
|
|
- Sign contract the following hex numbers to 16-bits and indicate the invalid contraction.
|
|
|
|
|
|
1-Indicate which one of the following 16-bit numbers is positive and which one is negative.
|
|
|
|
|
- Perform the bitwise AND operation on the following pairs of hex numbers. Present the output in hex
a) 0FF00, 0FF0 b) 0F00F, 1234 d) 2F41, F241 c)FFFF,EDCB f) 1111, 5789
- Simplify the following Boolean expression
ABC+A’BC+AB’C+B’C+BC’+A’
The typical Von-Newman architecture is shown below indicating three main units CPU, Memory and I/O Devices. The connection between them is called System Bus
System bus
System Bus consists of
To convey data between CPU, memory and I/O devices
To identify the source and/or destination
|
Extra Segment Register
Stack Segment Register
Data Segment Register
Code Segment Register
16-bits registers
Segment on 80x86 can form 64K byte to 4 gigabyte. Offset is used to point inside the segment. The memory can be seen as 2-D array as shown in the figure below
The following figure shows how to calculate the physical address from the logical address.
Source: http://faculty.ksu.edu.sa/ohassan/Assembly%20language%20course/assemblyNotes.doc
Web site to visit: http://faculty.ksu.edu.sa/
Author of the text: indicated on the source document of the above text
If you are the author of the text above and you not agree to share your knowledge for teaching, research, scholarship (for fair use as indicated in the United States copyrigh low) please send us an e-mail and we will remove your text quickly. Fair use is a limitation and exception to the exclusive right granted by copyright law to the author of a creative work. In United States copyright law, fair use is a doctrine that permits limited use of copyrighted material without acquiring permission from the rights holders. Examples of fair use include commentary, search engines, criticism, news reporting, research, teaching, library archiving and scholarship. It provides for the legal, unlicensed citation or incorporation of copyrighted material in another author's work under a four-factor balancing test. (source: http://en.wikipedia.org/wiki/Fair_use)
The information of medicine and health contained in the site are of a general nature and purpose which is purely informative and for this reason may not replace in any case, the council of a doctor or a qualified entity legally to the profession.
The texts are the property of their respective authors and we thank them for giving us the opportunity to share for free to students, teachers and users of the Web their texts will used only for illustrative educational and scientific purposes only.
All the information in our site are given for nonprofit educational purposes