Machine Language Machine Language: Bunch of Bytes
Machine Language Machine Language: Bunch of Bytes
Bunch of Bytes
• Machine language is binary codes that the
computer executes.
Machine Language • The computer fetches the instructions from
memory and executes them.
COMP375 Machine language for a square root program
Computer Architecture and Organization 8b 45 e0 89 45 f8 89 45 ec 8b 45 ec
89 45 f8 8b 45 e0 ba 00 00 00 00 f7
7d f8 03 45 f8 d1 f8 89 45 ec 3b 45
f8 75 e2 8b f4
Assembler and Machine Instruction Format
• Assembler language is the easy way to write • The general format for a machine language
machine language.
hi l i t ti i
instruction is
• Each line of an assembler program generates
one machine language instruction. Op code Operands
• The assembler allows you to use variable
names instead of numerical addresses and
names instead of numerical addresses and The operands can be a memory address,
address a
instruction mnemonics instead of numerical register or a value.
operation codes.
1
9/16/2008
Op codes Data Location
• Each assembler instruction represents a
• Register – The data is in a CPU register.
numerical machine language opcode.
numerical machine language opcode.
• Memory – The data is in a location in RAM
add 05 • Immediate – The data is part of the instruction.
cmp 3B Immediate data items are read‐only.
dec FF
idiv 7F
jmp 39
push 68
sar D0
Intel Assembler Mnemonic to Op Code Mapping
• The Intel assembler
allows you to use Opcode Instruction Description • Intel assembler uses the same mnemonic for
04 ib ADD AL,imm8 ADD imm8 to AL
one mnemonic for
one mnemonic for 05 iw ADD AX,imm16 ADD imm16 to AX
th
the machine language instruction to:
hi l i t ti t
05 id ADD EAX,imm32 ADD imm32 to EAX
different op codes. 80 /0 ib ADD r/m8,imm8
81 /0 iwADD r/m16,imm16
ADD imm8 to r/m8
ADD imm16 to r/m16 – Move a byte from memory to a register
81 /0 id ADD r/m32,imm32 ADD imm32 to r/m32
• There are several 83 /0 ib ADD r/m16,imm8 ADD sign-extended – Move a byte from a register to memory
83 /0 ib ADD r/m32,imm8 ADD sign-extended i
versions of the add 00 /r ADD r/m8,r8
01 /r ADD r/m16,r16
ADD r8 to r/m8
ADD r16 to r/m16 • The Intel mov instruction generates different
instruction based on 01 /r ADD r/m32,r32
02 /r ADD r8,r/m8
,
ADD r32 to r/m32
ADD r/m8 to r8 machine language op codes depending upon
machine language op codes depending upon
the size of the 03 /r ADD r16,r/m16
03 /r ADD r32,r/m32
ADD r/m16 to r16
ADD r/m32 to r32
the size of the operands.
operands. The
assembler picks the
correct op code.
2
9/16/2008
Number of Operands No operand instructions
• Some instructions do not require any operands.
• In addition to the op code, the instruction The data affected is implied in the instruction.
might contain zero, one, two or three
i ht t i t th
operands. • RET — Return from function
• Different architectures use different number of • HLT — Halt
operands. • CPUID — Get details about the CPU
• A single architecture may have instructions
A single architecture may have instructions • LAHF — Load Status Flags into AH Reg
Load Status Flags into AH Reg
with differing number of operands.
Op code
One Operand Instructions Two Operand Instructions
• Many instructions act on two operands
• Some unary operations require only one • Most math instructions use two operands and
operand
d return the results in one of them.
inc al ‐ increment the al register • add al,varname
jmp address – jump to the address • add bl,al
• imul eax,
, varname
Op code register
Op code register address
Op code address Op code reg1 reg2
3
9/16/2008
Three Operand Instructions Additional Instruction Fields
• Some machines support three operands (Intel • Most architectures support an addressing mode
Pentium does not). that combines an address field in the instruction
• Most MIPS instructions use three operands and the contents of a register
and the contents of a register
Index
Op code reg1 address
reg
Assembled Code
Variable or Fixed Length addr machine assembler
004a 8b 45 e0 mov eax, number[ebp]
004d 89 45 f8 mov good[ebp], eax
• Some architectures use variable length 0050 89 45 ec mov better[ebp], eax
i t ti
instructions. Instructions with more operands
I t ti ith d 0053 8b 45 ec mov eax, better[ebp]
or memory addresses are longer. again:
0056 89 45 f8 mov good[ebp], eax
– saves memory 0059 8b 45 e0 mov eax, number[ebp]
• Some architectures always use the same 005c ba 00 00 00 00 mov edx, 0
0061 f7 7d f8 idiv good[ebp]
length instruction.
length instruction. 0064 03 45 f8 add
dd eax, good[ebp]
d[ b ]
– easier to find the beginning of instructions 0067 d1 f8 sar eax, 1
– instructions are in aligned words 0069 89 45 ec mov better[ebp], eax
006c 3b 45 f8 cmp eax, good[ebp]
006f 75 e2 jne SHORT again
0071 8b f4 mov esi, esp
4
9/16/2008
Questions about the code Example Machine Language
• Assume each instruction of this imaginary
• What does SHORT mean after the jne ?
computer is 32 bits in length
computer is 32 bits in length
• What is the displacement of the jne ?
8 4 4 16 bits
opcode reg index address
reg
label: mnemonic reg, address[index reg]
Assembler language format
Machine Language Program World of Numbers
00 01100018 LOAD R1, y • The opcodes are numbers
04 2110001C DIV R1, z • The address is a number
The address is a number
08 05100020 ADD R1, five • The register field is a number
0C 02100014 STORE R1, x 8 4 4 16 bits
10 47000000 RET opcode reg index address
14 x res reg
g
18 y res 10100101 0011 0000 000000001011010
1C z res
20 00000005 five +5
Add R3, [no indexing] xyz
5
9/16/2008
What are the Instructions?
Disassembling Opcodes
Add Subtract Multiply Divide Load Store JumpEql Jump
0 1 2 3 4 5 6 7
• Any bunch of bits in memory can be considered
Instruction Formats
a program. opcode register index memory address
Load and Store
• Most random bits may not produce a logical register
3 4 4 21
program and may generate errors due to bad
unused
opcode reg 1 reg 2 reg 3 Add, Sub, Mult and Divide
opcodes or addressing errors.
3 1 4 4 4
• A disassembler is a program that interprets the
A disassembler is a program that interprets the 1 2 3 4 5 6 7 8 9
1
0
1
1
1
2
1
3
1
4
1
5
1
6
1
7
1
8
1
9
2
0
2
1
2
2
2
3
2
4
2
5
2
6
2
7
2
8
2
9
3
0
3
1
3
2
values in memory as instructions. 1 0 0 0 0 0 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
0 1 0 0 0 0 1 0 0 0 0 1 0 1 1 0
• Some software asks not to be disassembled 0 1 1 0 1 0 0 1 0 1 0 0 0 0 0 0
0 0 0 0 0 1 1 1 0 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
What are the Instructions?
Opcodes Notes on the Example Architecture
Add Subtract Multiply Divide Load Store JumpEql Jump
0 1 2 3 4 5 6 7
• This is an example of a “Load/Store” architecture.
Instruction Formats
opcode register index memory address
O l th l d d t
Only the load and store instructions access
i t ti
Load and Store
register memory.
3 4 4 21
• Why is there a one bit unused field in the
unused
opcode reg 1 reg 2 reg 3 Add, Sub, Mult and Divide
arithmetic instructions?
3 1 4 4 4
1 2 3 4 5 6 7 8 9
1
0
1
1
1
2
1
3
1
4
1
5
1
6
1
7
1
8
1
9
2
0
2
1
2
2
2
3
2
4
2
5
2
6
2
7
2
8
2
9
3
0
3
1
3
2 • Why is the opcode always the left most bits?
Why is the opcode always the left most bits?
1 0 0 0 0 0 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
0 1 0 0 0 0 1 0 0 0 0 1 0 1 1 0 • The format of the jump instructions was not
0 1 1 0 1 0 0 1 0 1 0 0 0 0 0 0 shown. What might be a good format?
0 0 0 0 0 1 1 1 0 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
6
9/16/2008
Instruction Cycle Basic Processor Components
• Fetch the instruction from the memory • Program Counter – contains the address of
address in the Program Counter register
dd i th P C t it th
the next instruction to execute.
t i t ti t t
• Increment the Program Counter • Arithmetic Logic Unit – logic to perform
• Decode the type of instruction arithmetic and logical functions
• Fetch the operands • User registers – hold data
• Execute the instruction • Memory Address Register
Memory Address Register – contains the
contains the
• Store the results address to be copied to or from RAM
• Memory Buffer Register – contains data
copied to or from RAM.
Instruction Fetch Instruction Fetch
R1 R1
Program Counter Instruction Register Program Counter Instruction Register
R2 R2
ALU . . . ALU . . .
R16 R16
Memory Read Read Result
7
9/16/2008
Increment Program Counter Increment Program Counter
R1 R1
Program Counter Instruction Register Program Counter Instruction Register
R2 R2
ALU . . . ALU . . .
+1 R16 R16
Decode Instruction Operand Fetch
R1 R1
Program Counter Instruction Register Program Counter Instruction Register
R2 R2
ALU . . . ALU . . .
R16 R16
Memory Read
8
9/16/2008
Operand Fetch Execution
R1 R1
Program Counter Instruction Register Program Counter Instruction Register
R2 R2
ALU . . . ALU . . .
R16 R16
Read Result
Result Store Jump Instruction
• Consider an arithmetic instruction followed by
R1
a jump instruction.
j i t ti
Program Counter Instruction Register
R2 • The arithmetic instruction sets bits in the
ALU . . . status register
R16
Memory Address Reg Memory Buffer Reg
9
9/16/2008
Execution Stage of Instruction 1 Result Save Stage of Instruction 1
Instruction 2 Fetch Instruction 2 Fetch
Memory Read Read Result
10
9/16/2008
Increment Program Counter Increment Program Counter
Decode Instruction Execution of Instruction 2
11
9/16/2008
Saving Result of Instruction 2
Status Register
R1
Program Counter Instruction Register
R2
ALU . . .
R16
Memory Address Reg Memory Buffer Reg
12