### Machine Language - Binary

```1
Machine Language - Binary
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
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
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
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
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?
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?
0001 = 1 = STORE
0010 = 2 = CLEAR
ETC.,…
00
01
02
03
04
05
06
07
08
09
0A
0B
0C
0D
0E
0F
10
Contents
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?
0001 = 1 = STORE
0010 = 2 = CLEAR
ETC.,…
Machine Language with Mnemonics
00
01
02
03
04
05
06
07
08
09
0A
0B
0C
0D
0E
0F
10
00C
00D
STORE
00E
COMPARE 010
JUMPGT
007
STORE
00F
JUMP
00A
010
SUBTRACT 00E
STORE
00F
OUT
00F
HALT
000
0006
0007
0000
0000
0000
At what cost?
Machine Language with Mnemonics
00
01
02
03
04
05
06
07
08
09
0A
0B
0C
0D
0E
0F
10
00C
00D
STORE
00E
COMPARE 010
JUMPGT
007
STORE
00F
JUMP
00A
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
00C
00D
STORE
00E
COMPARE 010
JUMPGT
007
STORE
00F
JUMP
00A
010
SUBTRACT 00E
STORE
00F
OUT
00F
HALT
000
0006
0007
0000
0000
0000
Let’s write the remaining
constants in decimal form
Proto-Assembly Language
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
12
13
STORE
14
COMPARE
16
JUMPGT
7
STORE
15
JUMP
10
16
SUBTRACT 14
STORE
15
OUT
15
HALT
6
7
0
0
0
Let’s write the remaining
constants in decimal form
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
12
13
STORE
14
COMPARE
16
JUMPGT
7
STORE
15
JUMP
10
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
12
13
STORE
14
COMPARE
16
JUMPGT
7
STORE
15
JUMP
10
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
12
13
??
STORE
14
COMPARE
16
JUMPGT
7
STORE
15
JUMP
10
16
SUBTRACT 14
STORE
15
OUT
15
HALT
6
7
0
0
0
an instruction…
Assembly Language
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
12
13
STORE
14
COMPARE
16
JUMPGT
7
STORE
15
JUMP
10
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
A
13
STORE
14
COMPARE
16
JUMPGT
7
STORE
15
JUMP
10
16
SUBTRACT 14
STORE
15
OUT
15
HALT
6
7
0
0
0
Assembly Language
A
B
STORE
C
COMPARE
ZERO
JUMPGT
NEGATE
STORE
D
JUMP
OUTPUT
ZERO
SUBTRACT C
STORE
D
OUTPUT: OUT
D
HALT
A:
6
B:
7
C:
0
D:
0
ZERO:
0
Assembly Language
.BEGIN
A
B
STORE
C
COMPARE
ZERO
JUMPGT
NEGATE
STORE
D
JUMP
OUTPUT
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
A
B
STORE
C
COMPARE
ZERO
JUMPGT
NEGATE
STORE
D
JUMP
OUTPUT
ZERO
SUBTRACT C
Assembler Responsibilities
STORE
D
• Mnemonics  Binary
OUTPUT: OUT
D
HALT
• Decimal  Binary
A:
.DATA
6
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
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
COMPARE B
JUMPLT LOOP1END
.DATA 2
JUMPEQ LOOP1END
OUT A
SUBTRACT TWO
STORE A
JUMP LOOP1
LOOP1END: …
A: .DATA 0
B: .DATA 0
TWO:
26
More Examples
LOOP:
.BEGIN
CLEAR
RESULT
STORE
FIRST
COUNTER
LOOP:
FINAL
COMPARE COUNTER
JUMPGT
FINISH
STORE
RESULT
COUNTER
RESULT
.BEGIN
COMPARE
JUMPGT
JUMPEQ
LIMIT
RESULT
DONE
DONE
STORE
STORE
SECOND
FIRST
RESULT
SECOND
STORE
FIRST
SECOND
RESULT
JUMP
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
```