Assignment 1 Model Answers
Assignment 1 Model Answers
Assignment 1 Model Answers
1 *the types of registers that ARM 7 processor contains are used to control the instructions being executed, to handle addressing of memory and provide arithmetic capability.
General Purpose Registers: These are the workhorses of the system. They are used in different arithmetic operations; they can be addressed as one word or as a 1-byte portion. These are registers from R0 R12. Pointer Registers: they are 32 bits long, they are three registers. o SP Register: R13 (Stack Pointer Register). It provides an offset value, which, when associated with the SS register, refers to the current word instruction being processed in the stack. o LR Register: R14 (Link Register). Contains the offset address of the next instruction to be executed. The PC is associated with the LR register in that the PC indicates the current instruction within the currently executing code segment. o PC Register: R15 (Program Counter Register). Contains the address of the current instruction within the currently executing instruction. Facilitates referencing parameters, which are data and addresses that a program passes via a stack.
Flags Register: (CPSR Register) Indicate the current status of the computer and the results of processing. Many instructions involving comparisons and arithmetic change the status of the flags. o V (Overflow): Indicates overflow of a high-order (leftmost) bit following arithmetic. o N (Negative): Contains the resulting sign of an arithmetic or comparison (0=positive and 1= negative). o Z (Zero): Indicates the result of an arithmetic or comparison operation (0=nonzero and 1=zero result). o C (Carry): Contains carries from a high-order (leftmost) bit following an arithmetic operation; also, contains the contents of the last bit of a shift or rotate operation.
Q.2 *the Contents of Registers R1 R5 are as follows: 27 165 192 1 191 Since when running the code, we get the values of these 5 registers in hexadecimal as follows: 1B 27 A5 165 C0 192 11 BF 191 Justification for these resultant values: 27 00011011 165 10100101
decimal). we
Q.3
AREA Prob3, CODE ENTRY
ADD R0,R0,#1 ; To increment the value by 1 CMP R0,#10 ; Compare the value to 10 BNE loop ; If not equal, loop again
END
Q.4
AREA prob4, CODE ENTRY
MOV R0,#3 MOV R1,#4 MOV R2,#0 MOV R3,#0 BL Subtract BL Multiply
; To hold the first value ; To hold the second value ; To initialize the third value/ holds the result of subtraction ; To initialize the fourth value/ holds the result of multiplication ; Branching to do the subtraction function ; Branching to do the multiplication function
Subtract SUB R2,R1,R0 ; Subtracting R0 from R1, save the result in R2 MOV PC,LR ; Go back to the next instruction to be executed
Muliply MUL R3,R1,R0 ; Multiplying R1 times R0, save the result in R3 MOV PC,LR ; Go back to the next instruction to be executed
finish NOP
/ No Operations
END
Q.5
AREA Prob5, CODE ENTRY MOV R1,#4 MOV R4,R1 fun SUB MUL MOV CMP BNE END ; To hold the value needed to get its factorial ; To save that value again
R1,#1 ; Decrement the number by 1 R5,R4,R1 ; Multiplying that value by the previous one R4,R5 ; Putting the result in R4 to multiply the new value by it again R1,#1 ; Compare the value to 1 fun ; If not equal, loop again
Q.6
AREA EvenOdd, CODE ENTRY MOV R0, #12 ; Number to check MOVS R0, R0, LSR #1 MOVCS R3, #0 MOVCC R3, #1 END
Q.7
AREA Prob7, CODE ENTRY MOV MOV MOV MOV Div SUB CMP BGT BEQ BNE R0,#5 R2,R0 R1,#2 R3,#0 R0,R1 R0,#0 Div ans fin ; ; ; ; To To To To hold the value to be tested save it again for comparison afterwards hold the vale of the divisor indicate the final result divisor from the original value zero R0 is greater than zero R0 == zero R0 != zero
; Loop to always subtract the ; Compare the tested value to ; Branching in case of ; Branching in case of ; Branching in case of
ans CMP R2,R1 ; Compare the divisble value to the original BNE end ; If not equal, then end as it is not prime
MOV R3,#1
; else, it is prime
fin MOV R0,R2 ; Reset R0 to the original value again ADD R1,R1,#1 ; Increment the divisor by 1 CMP R1,#9 ; Compare the divisor to 9 BLE Div ; If it is less than 9, loop (div) again MOV R3,#1 ; else, it is a prime number end NOP END ; To end the whole program .. No operation
Q.8
AREA Prob8, CODE ENTRY MOV MOV MOV MUL R0,#12 ; To hold the first value R1,#15 ; To hold the second value R2,#0 ; To hold the final result (LCM) R3,R0,R1 ; To hold the multiplication of the given two values
; Loop to always compare after doing the subtraction div CMP R0,R1 ; To compare the values in R0, R1 BEQ LCM ; Branching in case of equal values BGT great ; Branching in case of R0 greater than R1 BLT less ; Branching in case of R0 less than R1 great SUB R0,R1 B div less SUB R1,R0 B div ; Subtracting ; Back ; Subtracting ; Back R1 to R0 to from R0 the loop from R1 the loop
; In case of equal values, then we have their GCD LCM SUB R3,R1 ; Subtracting R1 (GCD) from R3 (Their multipliction) ADD R2,R2,#1 ; Incrementing R2 to keep the quotient CMP R3,#0 ; Compare R3 to zero (To stop the loop/ end the division) BNE LCM ; Loop again as long as R3 != 0 END