assignment 1 model answers

Upload: 007wasr

Post on 13-Apr-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/27/2019 Assignment 1 Model Answers

    1/5

    Q.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 R0R12.

    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.

  • 7/27/2019 Assignment 1 Model Answers

    2/5

    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:

    1B27

    A5165

    C0192

    11

    BF191

    Justification for these resultant values:

    2700011011

    16510100101

    decimal).

    we

    we get:

    s equal to 191 (in decimal).

  • 7/27/2019 Assignment 1 Model Answers

    3/5

    Q.3

    AREA Prob3, CODE

    ENTRY

    MOV R0,#0 ; To hold the value

    loop 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 ; To hold the first value

    MOV R1,#4 ; To hold the second value

    MOV R2,#0 ; To initialize the third value/ holds the result of subtraction

    MOV R3,#0 ; To initialize the fourth value/ holds the result of multiplication

    BL Subtract ; Branching to do the subtraction function

    BL Multiply ; Branching to do the multiplication function

    B finish ; Branching to end the whole program

    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

  • 7/27/2019 Assignment 1 Model Answers

    4/5

    finish NOP ; End the whole program / No Operations

    END

    Q.5

    AREA Prob5, CODEENTRY

    MOV R1,#4 ; To hold the value needed to get its factorialMOV R4,R1 ; To save that value again

    fun SUB R1,#1 ; Decrement the number by 1

    MUL R5,R4,R1 ; Multiplying that value by the previous oneMOV R4,R5 ; Putting the result in R4 to multiply the new value by it againCMP R1,#1 ; Compare the value to 1

    BNE fun ; If not equal, loop again

    END

    Q.6

    AREA EvenOdd, CODEENTRY

    MOV R0, #12 ; Number to checkMOVS R0, R0, LSR #1

    MOVCS R3, #0MOVCC R3, #1

    END

    Q.7

    AREA Prob7, CODEENTRY

    MOV R0,#5 ; To hold the value to be testedMOV R2,R0 ; To save it again for comparison afterwards

    MOV R1,#2 ; To hold the vale of the divisorMOV R3,#0 ; To indicate the final result

    Div SUB R0,R1 ; Loop to always subtract the divisor from the original valueCMP R0,#0 ; Compare the tested value to zero

    BGT Div ; Branching in case of R0 is greater than zeroBEQ ans ; Branching in case of R0 == zeroBNE fin ; Branching in case of R0 != zero

    ans CMP R2,R1 ; Compare the divisble value to the originalBNE end ; If not equal, then end as it is not prime

  • 7/27/2019 Assignment 1 Model Answers

    5/5

    MOV R3,#1 ; else, it is prime

    fin MOV R0,R2 ; Reset R0 to the original value againADD R1,R1,#1 ; Increment the divisor by 1CMP R1,#9 ; Compare the divisor to 9

    BLE Div ; If it is less than 9, loop (div) againMOV R3,#1 ; else, it is a prime number

    end NOP ; To end the whole program .. No operation

    END

    Q.8

    AREA Prob8, CODEENTRY

    MOV R0,#12 ; To hold the first valueMOV R1,#15 ; To hold the second value

    MOV R2,#0 ; To hold the final result (LCM)MUL R3,R0,R1 ; To hold the multiplication of the given two values

    ; Loop to always compare after doing the subtractiondiv 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 R1BLT less ; Branching in case of R0 less than R1

    great SUB R0,R1 ; Subtracting R1 from R0B div ; Back to the loop

    less SUB R1,R0 ; Subtracting R0 from R1B div ; Back to the loop

    ; In case of equal values, then we have their GCDLCM 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