Machine Language - Binary
Download
Report
Transcript Machine Language - Binary
1
Machine Language - Binary
Address
Contents
00000000
00000001
00000010
00000011
00000100
00000101
00000110
00000111
00001000
00001001
00001010
00001011
00001100
00001101
00001110
00001111
00010000
0000000000001100
0011000000001101
0001000000001110
0111000000010000
1001000000000111
0001000000001111
1000000000001010
0000000000010000
0101000000001110
0001000000001111
1110000000001111
1111000000000000
0000000000000110
0000000000000111
0000000000000000
0000000000000000
0000000000000000
Machine Language - Binary
Address
Contents
00000000
00000001
00000010
00000011
00000100
00000101
00000110
00000111
00001000
00001001
00001010
00001011
00001100
00001101
00001110
00001111
00010000
0000000000001100
0011000000001101
0001000000001110
0111000000010000
1001000000000111
0001000000001111
1000000000001010
0000000000010000
0101000000001110
0001000000001111
1110000000001111
1111000000000000
0000000000000110
0000000000000111
0000000000000000
0000000000000000
0000000000000000
Do you know HEX?
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
0
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F
Machine Language - Binary
Address
Contents
00000000
00000001
00000010
00000011
00000100
00000101
00000110
00000111
00001000
00001001
00001010
00001011
00001100
00001101
00001110
00001111
00010000
0000000000001100
0011000000001101
0001000000001110
0111000000010000
1001000000000111
0001000000001111
1000000000001010
0000000000010000
0101000000001110
0001000000001111
1110000000001111
1111000000000000
0000000000000110
0000000000000111
0000000000000000
0000000000000000
0000000000000000
Do you know HEX?
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
0
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F
Machine Language - Binary
Address
Contents
00000000
00000001
00000010
00000011
00000100
00000101
06
00000111
00001000
00001001
00001010
00001011
00001100
00001101
00001110
00001111
00010000
0000000000001100
0011000000001101
0001000000001110
0111000000010000
1001000000000111
0001000000001111
800A
0000000000010000
0101000000001110
0001000000001111
1110000000001111
1111000000000000
0000000000000110
0000000000000111
0000000000000000
0000000000000000
0000000000000000
Do you know HEX?
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
0
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F
Machine Language - Hexadecimal
Address
00
01
02
03
04
05
06
07
08
09
0A
0B
0C
0D
0E
0F
10
Contents
000C
300D
100E
7010
9007
100F
800A
0010
500E
100F
E00F
F000
0006
0007
0000
0000
0000
Is this any better?
Machine Language - Hexadecimal
Address
00
01
02
03
04
05
06
07
08
09
0A
0B
0C
0D
0E
0F
10
Contents
000C
300D
100E
7010
9007
100F
800A
0010
500E
100F
E00F
F000
0006
0007
0000
0000
0000
Is this any better?
What if we use op-codes?
0000 = 0 = LOAD
0001 = 1 = STORE
0010 = 2 = CLEAR
0011 = 3 = ADD
ETC.,…
Machine Language - Hexadecimal
Address
00
01
02
03
04
05
06
07
08
09
0A
0B
0C
0D
0E
0F
10
Contents
ADD
STORE
000C
00D
100E
7010
9007
00F
800A
0010
500E
100F
E00F
F000
0006
0007
0000
0000
0000
Is this any better?
What if we use op-codes?
0000 = 0 = LOAD
0001 = 1 = STORE
0010 = 2 = CLEAR
0011 = 3 = ADD
ETC.,…
Machine Language with Mnemonics
00
01
02
03
04
05
06
07
08
09
0A
0B
0C
0D
0E
0F
10
LOAD
00C
ADD
00D
STORE
00E
COMPARE 010
JUMPGT
007
STORE
00F
JUMP
00A
LOAD
010
SUBTRACT 00E
STORE
00F
OUT
00F
HALT
000
0006
0007
0000
0000
0000
What does this buy us?
At what cost?
Machine Language with Mnemonics
00
01
02
03
04
05
06
07
08
09
0A
0B
0C
0D
0E
0F
10
LOAD
00C
ADD
00D
STORE
00E
COMPARE 010
JUMPGT
007
STORE
00F
JUMP
00A
LOAD
010
SUBTRACT 00E
STORE
00F
OUT
00F
HALT
000
0006
0007
0000
0000
0000
}
Why didn’t I
substitute
for these?
Machine Language with Mnemonics
00
01
02
03
04
05
06
07
08
09
0A
0B
0C
0D
0E
0F
10
LOAD
00C
ADD
00D
STORE
00E
COMPARE 010
JUMPGT
007
STORE
00F
JUMP
00A
LOAD
010
SUBTRACT 00E
STORE
00F
OUT
00F
HALT
000
0006
0007
0000
0000
0000
Let’s write the remaining
constants in decimal form
with no leading zeros.
Proto-Assembly Language
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
LOAD
12
ADD
13
STORE
14
COMPARE
16
JUMPGT
7
STORE
15
JUMP
10
LOAD
16
SUBTRACT 14
STORE
15
OUT
15
HALT
6
7
0
0
0
Let’s write the remaining
constants in decimal form
with no leading zeros.
Note: the assembler will then
have to translate decimal
numbers back into binary.
Proto-Assembly Language
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
LOAD
12
ADD
13
STORE
14
COMPARE
16
JUMPGT
7
STORE
15
JUMP
10
LOAD
16
SUBTRACT 14
STORE
15
OUT
15
HALT
6
7
0
0
0
Now, let’s try to read this
program and understand
what it does.
15
Proto-Assembly Language
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
LOAD
12
ADD
13
STORE
14
COMPARE
16
JUMPGT
7
STORE
15
JUMP
10
LOAD
16
SUBTRACT 14
STORE
15
OUT
15
HALT
6
7
0
0
0
Now, let’s try to read this
program and understand
what it does.
Proto-Assembly Language
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
LOAD
12
ADD
13
ADD
??
STORE
14
COMPARE
16
JUMPGT
7
STORE
15
JUMP
10
LOAD
16
SUBTRACT 14
STORE
15
OUT
15
HALT
6
7
0
0
0
Suppose we want to add
an instruction…
Assembly Language
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
LOAD
12
ADD
13
STORE
14
COMPARE
16
JUMPGT
7
STORE
15
JUMP
10
LOAD
16
SUBTRACT 14
STORE
15
OUT
15
HALT
6
7
0
0
0
Assembly Language
0
1
2
3
4
5
6
7
8
9
10
11
A:
13
14
15
16
LOAD
A
ADD
13
STORE
14
COMPARE
16
JUMPGT
7
STORE
15
JUMP
10
LOAD
16
SUBTRACT 14
STORE
15
OUT
15
HALT
6
7
0
0
0
Assembly Language
LOAD
A
ADD
B
STORE
C
COMPARE
ZERO
JUMPGT
NEGATE
STORE
D
JUMP
OUTPUT
NEGATE: LOAD
ZERO
SUBTRACT C
STORE
D
OUTPUT: OUT
D
HALT
A:
6
B:
7
C:
0
D:
0
ZERO:
0
Assembly Language
.BEGIN
LOAD
A
ADD
B
STORE
C
COMPARE
ZERO
JUMPGT
NEGATE
STORE
D
JUMP
OUTPUT
NEGATE: LOAD
ZERO
SUBTRACT C
STORE
D
OUTPUT: OUT
D
HALT
A:
.DATA
6
B:
.DATA
7
C:
.DATA
0
D:
.DATA
0
ZERO:
.DATA
0
.END
Assembly Language
.BEGIN
LOAD
A
ADD
B
STORE
C
COMPARE
ZERO
JUMPGT
NEGATE
STORE
D
JUMP
OUTPUT
NEGATE: LOAD
ZERO
SUBTRACT C
Assembler Responsibilities
STORE
D
• Mnemonics Binary
OUTPUT: OUT
D
HALT
• Decimal Binary
A:
.DATA
6
• Labels Addresses
B:
.DATA
7
• Pseudo-ops
C:
.DATA
0
D:
.DATA
0
ZERO:
.DATA
0
.END
Java Version
int a = 6;
int b = 7;
int c, d;
c = a + b;
if (c >= 0) {
d = c;
} else {
d = -c;
}
System.out.println(d);
Assembly Language Examples
Simple examples:
x=y+3
input a and b
while a > b do
print a
a=a–2
LOAD Y
ADD THREE
STORE X
…
-- Data comes after HALT
X:
.DATA 0 -- X is initially 0
Y:
.DATA 5 – Y is initially 5
THREE: .DATA 3 –The constant 3
IN A
IN B
LOOP1: LOAD A
COMPARE B
JUMPLT LOOP1END
.DATA 2
JUMPEQ LOOP1END
OUT A
LOAD A
SUBTRACT TWO
STORE A
JUMP LOOP1
LOOP1END: …
A: .DATA 0
B: .DATA 0
TWO:
26
More Examples
LOOP:
.BEGIN
CLEAR
RESULT
LOAD
STORE
FIRST
COUNTER
LOOP:
LOAD
FINAL
COMPARE COUNTER
JUMPGT
FINISH
LOAD
ADD
STORE
RESULT
COUNTER
RESULT
.BEGIN
LOAD
COMPARE
JUMPGT
JUMPEQ
LIMIT
RESULT
DONE
DONE
LOAD
STORE
LOAD
STORE
SECOND
FIRST
RESULT
SECOND
LOAD
ADD
STORE
FIRST
SECOND
RESULT
JUMP
LOAD
STORE
HALT
.DATA
.DATA
.DATA
.DATA
.END
LOOP
SECOND
RESULT
INCREMENTCOUNTER
FINISH:
FIRST:
FINAL:
COUNTER:
RESULT:
JUMP
HALT
LOOP
DONE:
.DATA
.DATA
.DATA
.DATA
.END
1
7
0
0
FIRST:
SECOND:
RESULT:
LIMIT:
1
1
2
20
The End