Lecture 11 – Machine Code
Roller Coaster Tycoon
[Link]
Did you found yourself spending >30 minutes trying to
find a bug in your code for the last couple homeworks?
A. Yes, this happens to me often
B. Yes, this happens but for less than half the assignments
C. Yes, but it only happens rarely (maybe once all quarter)
D. No, I never find myself struggling to debug a problem on any
assignment
Why? – to fill in this puzzle
Machine
C Program Assembly
Code
3
Machine Code
• 1’s and 0’s that make up the each
instruction.
• Bad Old Days- enter each bit with a
toggle switch
• We can enter them in HEX numbers
through text editor
Machine Code by Example
• One level below Assembly
• Form the 32-bit numbers comprise each instruction in the
program
int sum(int a, int b){
return a + b;
}
int main(int argc, char **argv){
long a = strtol(argv[1], NULL, 10);
long b = strtol(argv[2], NULL, 10);
int c = sum(a, b);
printf(“%d + %d = %d\n”, a, b, c);
}
[Link]
[Link].ddi0210c/[Link]
Data Processing Instruction
Cond – does this instruction execute conditionally (in cse30, no)
Opcode – what data processing instruction
S – set condition codes (in CSE30, only CMP and TST set condition codes)
Rn – Op1 register number
Rd – dest register number
Operand – shift (always 0 in cse30), Op2 register number
[Link]
Data Processing
Instruction
Format
[Link]
Data Processing
Instruction
Format
Encode:
ADD R0, R0, R1
[Link]
ARM Add Instruction
1110
ADD R0, R0, R1
ARM7TDMI-S Data Sheet
BX Instruction
BX LR (r14) 0x ___ 1 2 f f f 1 ___
BX Instruction
BX LR (r14) 0x __ 1 2 f f f 1
BX Instruction
BX LR (r14) 0x e 1 2 f f f 1 ___
Machine Code Example Complete
int sum(int a, int b){
return a + b;
}
ADD R0, R0, R1
BL LR
public/demos/arm/machsum
Single Data
Transfer
What is the equivalent instruction?
1110 01 1 1 1 0 0 1 0010 0100 0000 0000 0011
A. STR r2, [r4, r3]
B. STR r2, [r4, #3]
C. LDR r4, [r2, r3]
D. LDR r4, [r2, #3]
E. None of the above
Branch
[Link]
Branch
Encode BLT (1011)
[Link]
Branch
8004: cmp r2, r3
8008: blt label What should be the offset (in decimal) for the
8012: add r0, r0, r1 blt instruction?
8016: sub r0, r0, r3 A. 3
8020: str r0, [r4] B. 5
8024: b end C. 12
8028: label: add r0, r0, r2 D. 20
… E. None of the above
[Link]
Your goal
Understand how to encode the following instruction types:
- Arithmetic (add, sub)
- Branch
- Load/Store
Summary
• Assembly instructions need to be converted to machine
instructions (binary) to be executed.
• We need to understand this translation as the physical hardware
(an arm processor) only functions using the machine instructions
Extra materials (formerly handouts)
[Link]
[Link].ddi0210c/[Link]
Data Processing
Instruction
Format
[Link]
Single Data
Transfer
BX Instruction