;ex3_2400 exercises for chapter 3 of  CENG2400. It is for your own revision purpose,no need to submit answers to tutors.
;1) create a project based on this .s code
;2) In keil-ide, use project/rebuild all target files to build the project
;3) use Debug/run_to_cursor_line to run the top line of the program, 
;    i.e. movs r0,#1,just under the label ex3_2a 
;4)  use the single step mode to view the registers and cpsr after the execution of each statement
;5) Explain the observations and results.



			  ; declare variables      New test12D
        AREA    |.data|, DATA, READWRITE
Data1p     DCD 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
        align
;-------------------------------------------------------------

; User Initial Stack & Heap
        AREA    |.text|, CODE, READONLY

        EXPORT  __main
__main    LDR R0, =Data1p
;;;;;;;;;;;; CEG2400   ex3_2
loop_top
;clear flags

ex3_2a ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
        movs r0,#1 ; this clears N,Z
        adds r0,#1 ; this clears C,V
        ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
        mov r1,#15    ;r1=15
        mov r2,#0xffffffff ; in 2' complement it is -1. 
        ADD r0,r1,r2
        ADC r0,r1,r2    ;r0=r1+r2+C
        SUB r0,r1,r2    ;r0=r1-r2
        SBC r0,r1,r2    ;r0=r1-r2+C-1
;Question1: explain the result in r0 and cpsr of the above steps .

ex3_2b ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
        movs r0,#1 ; this clears N,Z
        adds r0,#1 ; this clears C,V
        ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
        mov r1,#0x7ffffffF ;=the biggest 32-bit 2's complement num. +2,147,483,647
        mov r2,#0x1
        ADDS r0,r1,r2;r0=0x80000000.  
;Questio2: explain the result in cpsr.

ex3_2c ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
        movs r0,#1 ; this clears N,Z
        adds r0,#1 ; this clears C,V
        ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
        mov r1,#0x7ffffffE ;=the 2nd biggest 32-bit 2's complement num.  +2,147,483,647-1
        mov r2,#0x1
        ADDS r0,r1,r2; ;
;Question3: explain the result in cpsr.
ex3_2D ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
        movs r0,#1 ; this clears N,Z
        adds r0,#1 ; this clears C,V
        ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
        mov r1,#0xFffffffF ; THE VALUE IS -1 IN 2'S COMPLEMENT
        mov r2,#0x1   ; IS 1
        ADDS r0,r1,r2; ;
;Question4: explain the result in r0 and cpsr.

ex3_3;;;;;;;;;;;; CEG2400   ex3_3
        movs r0,#1 ; this clears N,Z
        adds r0,#1 ; this clears C,V
        ;;;;;;;;;;;;;;;;;;;
        mov r1,#0x55
        mov r2,#0x61
        and r0,r1,r2    ;
;Question5: explain the result in r0 and cpsr.
        orr r0,r1,r2 ;r0=r1 or r2
        EOR r0,r1,r2 ;
;Question6: explain the result in r0 and cpsr.
        BIC r0,r1,r2;Question: 
;Question7: explain the result in r0 and cpsr.
ex3_4 ;;;;;;;;;;;;;;;;;;;;;;;;;
        movs r0,#1 ; this clears N,Z
        adds r0,#1 ; this clears C,V
        ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
        MOV r1,#0x3
         MOV r2,#0x7
        MOV r2,#12    ;r2=#12
        MOV  r0,r2 ;
;Question8: explain the result in r0 and cpsr.
        MVN r1,r2 ;Question: explain the result in cpsr.
;Question9: explain the result in r0 and cpsr.
ex3_5    ;
        movs r0,#1 ; this clears N,Z
        adds r0,#1 ; this clears C,V
        ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
        mov r1,#0x11 ;r1=0000 0011 (the least significant 8 bits) 
        mov r2,#0x23; r2=0000 0023
        subs r3, r1, r2          
;Question10: explain the result in r3 and cpsr.
        movs r0,#1 ; this clears N,Z
        adds r0,#1 ; this clears C,V
        cmp r1,r2;   ;
;Question11: explain the result in r0->r12 and cpsr.
	mov r1,r2;    ; r1<=r2
        CMP r1, r2;    
;Question12: explain the result in r0->r12 and cpsr.
ex3_6a    ; place ex6
        movs r0,#1 ; this clears N,Z
        adds r0,#1 ; this clears C,V
        ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
        mov r1,#15 ;r1=15 decimal=0xf=0000 1111 (the least significant 8 bits)
        mov r2,#24 ;r2=24 =0x18      =0001 1000 (the least significant 8 bits)
        TST r1,r2  ;
;Question13: explain the result in r0->r12 and cpsr.
;not saved to any registers) is neither nagative nor zero 
                   ; (bits c,v are not affected by tsts)
        movs r0,#1 ; this clears N,Z
        adds r0,#1 ; this clears C,V
        TEQ r1,r2  ;
;Question14: explain the result in r0->r12 and cpsr.
ex3_6b    ; place ex6
        movs r0,#1 ; this clears N,Z
        adds r0,#1 ; this clears C,V
        ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
        mov r1,#0x0f ;15=0x0f=  0000 1111 (the least significant 8 bits)
        mov r2,#0xf0 ;r2=0x18=  1111 0000 (the least significant 8 bits)
        TST r1,r2    ; 
;Question15: explain the result in r0->r12 and cpsr.
        movs r0,#1 ; this clears N,Z
        adds r0,#1 ; this clears C,V
        TEQ r1,r2  ;
;Question16: explain the result in r0->r12 and cpsr.
        END