Adsense

Showing posts with label program flow control. Show all posts
Showing posts with label program flow control. Show all posts

Write assembly language code for all type of the conditional and unconditional program flow control instructions.

;Write assembly language code for all type of the conditional and
;unconditional program flow control instructions.
; 1- unconditional jump
; 2- conditional jump
; 2.1 - signed and unsigned conditional jump based on comperision
; 2.2 - signed and unsigned conditional jump based on overflow
; 2.3 - signed conditional jump based on N flag


        TTL transfer
        AREA Myprog, CODE, READONLY
ENTRY
        EXPORT main
main

;************************unconditional branch*****************************************
;Table 1: branch instructions with ranges
;==============================================================================
;Branch range                         Available instruction                                        
;==============================================================================
;Under +/–254 bytes                    B label                                                        
;                                                  B<cond> label                                                
;Under +/–2 KB                         B label                                                            
;Under +/–16 MB                      BL label                                                    
;Over +/–16 MB                         LDR R0, =label    ; Load the address value of label in R0        
;                                                  BX R0; Branch to address pointed to by R0, or                
;                                                  BLX R0; Branch to address pointed to by R0 and update LR    
;==============================================================================
        B LOAD_R2        ; B to the label
load_return

;Branch and link with updated value of LR = R14
        BL LOAD_ADD1        ;branch and updated PC link to the LR(R14), return to the next instruction
; address of MOVS R1,#3 will store into the LR for return            
    
        MOVS R1,#3
        MOVS R2,#3
        MOVS R3,#4
;*****************************conditional instructions***************************        
;Table2: Conditional branch instructions for value comparison operations
;=============================================================================
;Required branch control                 Unsigned data                    Signed data    
;=============================================================================
;If (R0 equal R1) then branch           BEQ label                            BEQ label        
;If (R0 not equal R1) then branch     BNE label                            BNE label        
;If (R0 > R1) then branch                  BHI label                            BGT label        
;If (R0 >= R1) then branch                BCS label/BHS label          BGE label        
;If (R0 < R1) then branch                  BCC label/BLO label          BLT label        
;If (R0 <= R1) then branch                BLS label                            BLE label        
;=============================================================================
;for signed and unsigned equ and Not equ
        CMP R2, R1            ; compare the R2 with R1 and reflect the status in FLAGS       
        BEQ LOOP3            ;branch if R2 = R1
        
LOOP3    CMP R2,R3     ; compare the R2 with R3 and reflect the status in FLAGS   
        BNE LOOP4_1        ; branch if R2 != R3

;unsigned greater than/equal and less than/equal
LOOP4_1    MOVS R1,#5    ;Loop counter starting value is 5
LOOP4
        ADDS R4, #1
        CMP R1, R4
        BLO LOOP5       ;branch if R1<R4/ R1 is lower than R4
        SUBS R1, #1
        BHI LOOP4        ;branch if R1 is higher than 1
        
LOOP5    MOVS R1,#6    ;Loop counter starting value is 6              
        ADDS R4, #1
        CMP R1, R4
        BLS LOOP6        ;branch if R1<= R4/ R1 lower than or same to R4
        SUBS R1, #1
        BHS LOOP5        ;branch if R1 is higher and same to 1

;signed greater than/equal and less than/equal
LOOP6    MOVS R1,#5    ;Loop counter starting value is 5
        MOVS R4,#0    ;Set R4 to zero
LOOP7
        ADDS R4, #1
        CMP R1, R4
        BLT LOOP8        ;branch if R1<R4
        SUBS R1, #1
        BGT LOOP7        ;branch if R1>1
        
LOOP8    MOVS R1,#6    ;Loop counter starting value is 3              
        ADDS R4, #1
        CMP R1, R4
        BLE LOOP9        ; branch if R1<=R4  
        SUBS R1, #1        
        BGE LOOP8        ; branch if R1>=1
        
;Table3: Conditional branch instructions for overflow detections
;==============================================================================
;Required branch control                           Unsigned data           Signed data
;==============================================================================
;If (overflow(R0 + R1)) then branch                BCS label            BVS label
;If (no_overflow(R0 + R1)) then branch         BCC label            BVC label
;If (overflow(R0 – R1)) then branch                BCC label            BVS label
;If (no_overflow(R0 – R1)) then branch         BCS label            BVC label
;==============================================================================

;Table4: Conditional branch instructions for positive or negative value detection
;==============================================================================
;Required branch control             Unsigned data                 Signed data
;==============================================================================
;If (result >=0) then branch         Not applicable                  BPL label
;If (result < 0) then branch          Not applicable                  BMI label
;==============================================================================

;unsigned overflow detection/ carry flag based branch
LOOP9
        LDR R6,=0xFFFFFFFF
        LDR R5,=0x00000001
LOOP11    ADDS R6,R6,R5
        ; during first addition carry flag is set, ans is C=1 R6=0x00000000
        ; during second addition carry flag is reset, ans is C=0 R6=0x00000001
        BCC LOOP10            ;branch if  C=0
        BCS LOOP11            ;branch if C=1
;above example peform the overflow for addition operations
;for subtraction you can do it by your own         
        
;Signed overflow incorporate signed ov, carry and Signed flag        
LOOP10
        LDR R6,=0xFFFFFFFF
        LDR R5,=0x80000000
LOOP12    ADDS R6,R6,R5
        ; during first addition sign change and ov flag is set, ans is N=0,C=1,ov=1 R6=0x7FFFFFFF
        ; during second addition sign changes and ov flag is reset, ans is N=1,C=0,ov=0 R6=0xFFFFFFFF
        BVC LOOP13                  ;branch if V=0
        BCS    LOOP15                ;branch if C=1
LOOP15    BPL LOOP16        ;branch if N=0
LOOP16    BVS LOOP12        ;branch if V=1
LOOP13    BCC LOOP14        ;branch if C=0
LOOP14    BMI LOOP17        ;branch if N=1


LOOP17    B done

LOAD_R2
        MOVS R2,#0X04
        LDR R0,=load_return ; psudo load value of label into the R0 for BX instruction
        BX R0                ; unconditional return through branch and exchange

;loop inside the loop
;use of BLX and BL with <Rm>
LOAD_ADD1
        MOV R1,R14            ;Storing the LR(R14) value into R1 for further use
        LDR R2,=load        
        BLX R2                ;Jump to the PC where R2 is pointing and update LR with new value
        LDR R3,=add2
        BLX R3                ;Jump to the PC where R3 is pointing and update LR with new value
        BX R1                ;Return from original location/ return to the line BL LOAD_ADD1
;load subroutine        
load    LDR R6,=0x034        
        LDR R7,=0x235
        BX LR                ; return to the line next to BLX R2
;add subroutine        
add2    ADDS R6, R7, R6
        BX LR                  ; return to the line next to BLX R3
        
done
        SWI &11
        END

Write embedded C and assembly code to understand program flow instructions for "for loop".

Usually, we start coding with assembly and than we move to the embedded C code. In this case we first understand the  embedded C code and than we will write the equivalent assembly code to mimic the same thing.

Embedded C code:

int main ()
{
volatile unsigned int total, i;

total = 0;                    // initialize the total
        for (i=0;i<5;i=i+1)
        {
        total = total + i;
        }
return(0);
    
}

       
 Result:

 

Assembly language code:

        TTL transfer
        AREA Myprog, CODE, READONLY
ENTRY
        EXPORT main
main
        MOVS R2, #0 ; Total = 0
        MOVS R1, #0 ; i = 0
loop
        ADDS R2, R2, R1 ; Total = Total + i
        ADDS R1, R1, #1 ; i = i + 1
        CMP R1, #5 ; compare i to 5
        BLT loop ; if less than then branch to loop
done
        SWI &11
        END

Result:


Write a ARM cortex M0+ assembly language code based on arithmatc and logical instructions.

 Problem 1: Implement following code conversions to convert binary no 0x89ABCDEF(32-bits) into a) BCD (64-bits)   b) Gray (32-bits) Problem...