KembaraXtra-Computer Science - Calculating Factorial in Machine Code
1. What is a Factorial?
• The factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n.
• Example: 4! = 4 × 3 × 2 × 1 = 24
2. ARM Machine Code for Factorial Calculation
• The following code calculates the factorial of an integer n.
• Initial condition: n is stored in register r0.
• Final condition: n! (the factorial of n) is stored in register r0.
2.1. Machine Code in Memory
• Machine code is loaded into memory as hexadecimal values. Each instruction is 4 bytes (32 bits) long.
Address Data
0001007c e2503001
00010080 da000002
00010084 e0000093
00010088 e2533001
0001008c 1afffffc
2.2. Assembly Language Equivalent
• Disassembling the machine code gives the corresponding assembly language instructions:
Address Data Assembly
0001007c e2503001 subs r3, r0, #1
00010080 da000002 ble 0x10090
00010084 e0000093 mul r0, r3, r0
00010088 e2533001 subs r3, r3, #1
0001008c 1afffffc bne 0x10084
00010090 ---
• The code starts at address 0001007c and ends (factorial calculation) at address 00010090.
3. ARM Instructions Explained
Instruction Details
subs Rd, Rn, #Const Subtract: Subtracts the constant value Const from the value stored in register Rn and stores the result in register Rd. Rd = Rn - Const. Also, subs affects the status register (used for conditional branching).
mul Rd, Rn, Rm Multiply: Multiplies the value stored in register Rn by the value stored in register Rm and stores the result in register Rd. Rd = Rn × Rm
ble Addr Branch if less than or equal: If the previous operation's result was ≤ 0, jump to address Addr.
bne Addr Branch if not equal: If the previous operation's result was not 0, jump to address Addr.
4. Branching and the Status Register
• ARM processors use a status register to track the results of operations.
• Specific bits in the status register (flags) indicate conditions like negative result, zero result, etc.
• Instructions like subs update the status flags.
• Branch instructions (e.g., ble, bne) check the status flags to determine whether to jump or continue.
5. Exercise: Understanding the Factorial Program
• Goal: Trace the execution of the assembly code to calculate the factorial of 4.
• Initial condition: r0 = 4
• Expected final result: r0 = 24
Procedure:
i. Create a table to track the values of r0 and r3 before and after each instruction.
ii. Execute each instruction sequentially, updating the register values accordingly.
iii. Pay attention to how the subs instructions affect the status flags and how the branch instructions (ble, bne) use these flags to control the flow of execution.
iv. Stop when the program reaches address 00010090.
v. Verify that r0 contains the value 24.
Assembly Code:
Address Assembly
0001007c subs r3, r0, #1
00010080 ble 0x10090
00010084 mul r0, r3, r0
00010088 subs r3, r3, #1
0001008c bne 0x10084
00010090 ---
6. Additional Projects (See Text for Details)
• Project #12: Assemble the factorial code and examine it while it runs.
• Project #13: Learn additional approaches for examining machine code.