Adsense

Showing posts with label Assembly. Show all posts
Showing posts with label Assembly. Show all posts

solution of assembly code to find the maximum and minimum from block of 32 bit data stored from memory location 0x1FFFF104, No. of data elements is stored on memory location 0x1FFFF100.

Solution1:

;jigar visani perform the code successfully roll no d19ec120 

              TTL transfer
        AREA Myprog, CODE, READONLY
ENTRY
        EXPORT main
main
        LDR R7,=0X1FFFF100 ; initialize the register R7 with starting address
        LDR R1,[R7,R1]     ; fetch the first value to R1 for counting  
        LSLS R1,R1,#0x02   ; left shift the R1 by 2 position to multiply with 4
        ADDS R7,R7,#0X04   ; add R7= R7+4 to start fetch the data from 0x1FFFF104
        LDR R3,=0X00000000 ; store maximum value
        LDR R4,=0XFFFFFFFF ; store the minimum value
       
COMPARE LDR R5,[R7,R6]    ; fetch first data into R5
        ADDS R6,R6,#0X04  ; Shift R6 to fetch the next word
        CMP R6,R1         ; compare R6 with R1 to continue loop
        BEQ done          ; if R6=R1 than end loop otherwise continue
        CMP R5,R3         ; compare the R5 with R3 to find greater
        BHI GREATER       ; number are unsigned so if R5>R3 than jump to label
back    CMP R5,R4         ; compare R5 with R4 to find minimum number
        BLO LESS          ; if R5<R3 jump to desired location
        B COMPARE         ; unconditional jump to comapre
      
GREATER MOVS R3,R5        ; transfer if R5 is higher than R3
        B back            ; jump to label
       
LESS    MOVS R4,R5        ; transfer if R5 smaller than R4
        B COMPARE         ; jump to label
 
done
        SWI &11
        END

Solutions .........

Please students try different logic. So, i can put some more stuff here.

Write assembly language code for all type of the conditional overgflow and signed program flow control instructions

;Write assembly language code for all type of the conditional overgflow and signed
;program flow control instructions
        TTL transfer
        AREA Myprog, CODE, READONLY
ENTRY
        EXPORT main
main
;Table1: 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
;==============================================================================

;Table2: 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
        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 done        ;branch if N=1
done
        SWI &11
        END

Write assembly language code for all type of the conditional for value comparison operations

 ;Write assembly language code for all type of the conditional for value comparison operations
 TTL transfer
        AREA Myprog, CODE, READONLY
ENTRY
        EXPORT main
main
;*****************************conditional instructions***************************        
;Table: 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        
;=============================================================================
        MOVS R1,#3
        MOVS R2,#3
        MOVS R3,#4
;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 done        ; branch if R1<=R4  
        SUBS R1, #1        
        BGE LOOP8        ; branch if R1>=1
done
        SWI &11
        END

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

;Write assembly language code for all type of unconditional program flow control instructions.
;************************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, BX LR                                                    
;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    
;==============================================================================
        TTL transfer
        AREA Myprog, CODE, READONLY
ENTRY
        EXPORT main
main

        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
;***************************by-pass subroutine***************************
LOOP17    B done
;*****************sub-routins********************************************
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
;*****************end of subroutine****************************************
done
        SWI &11
        END

write code to transfer block of data from 1FFFF104 to 1FFFF164 and blocksize is stores on 1FFFF100.

;please note here that data transfer done in word only

;first word transfer done between the addresses from 1FFFF1(04 - 05 - 06 - 07) to 1FFFF1(64 - 65 - 66 - 67)

;second word transfer done between the addresses from 1FFFF1(08 - 09 - 0A - 0B) to 1FFFF1(68 - 69 - 6A - 6B)

; and so on

; size of block will be available at location 1FFFF100

;Solution:

 TTL transfer
        AREA Myprog, CODE, READONLY
ENTRY
        EXPORT main
main
            LDR R7,=0x1FFFF100      ;first location initialize to the R7
            LDR R6,=0x1FFFF160    ;second location initialize to R6
            LDR R1,[R7,#0x00]        ;data number need to multiply of 4 as PC+4
            LSLS R1,R1,#0x02        
        ;multiply by 4 if words, 2 if half words and instruction not require in bytes are transferring
            LDR R3,=0x04    ; necessary to shift because PC+4   
Labl1    LDR R2,[R7,R3]    ;first word transfer to R2
            STR R2,[R6,R3]    ;first word store from R2
            ADDS R3,R3,#0x04    ;shift due to word transfer
            SUBS R1,R1,#0x04    ;
            BHI Labl1
done
            SWI &11
            END

Result:

Red colors: data fetch from memory

Green colors: data stored to the memory

Write assembly and embedded C code to find the maximum and minimum from 32 bit data stored from memory location 0x1FFFF104, No. of data elements is stored on memory location 0x1FFFF100.

;Write assembly and embedded C code to find the maximum, and minimum  ;from 32 bit data stored from memory location 0x1FFFF104, No. of data ;elements is stored on memory location 0x1FFFF100.

Steps to find the maximum from given set of 32 bit data

 

Step 1:

- Initialize register with the memory location 0x1FFFF100 i.e. R7 for holding base address.

- Initialize 0x00000000 to any of the register for holding Shift value i.e. R6. We need to add 0x04

In case, of 

Word we need to ADD 0x04

half word ADD 0x02 and

byte ADD 0x01.

- Initialize 0x00000000 to any of the register for holding maximum value i.e. R3.


Memory transfer

Step 2:

- Fetch 32 bit data from next memory location and initialize to any of register for the looping i.e. R1. 

- Add 0x04 into base address(0x1FFFF100) to now start fetching from 0x1FFFF104.

Step 3:

- Fetch next data (32bit) and store to any register i.e. in R2.

- Fix any of the register to store maximum number from list i.e. in R3

Arithmetic 

Step 5:

Compare content of both registers mentioned above.

Step 6: 

if the content of Data register (R2) is higher than content of maximum no register(R3) than transfer the content of data register(R2) into max register(R3).

Conditional branch

Step 7:

- Repeat steps 3 and 6 also decrease the looping register(R1) 

- Continue up to the value stored in Step 2 i.e. register R1 goes to zero.

 

Repeat above Steps to find the minimum from given set of 32 bit data.

 

Note:

if you are facing any doubt, please ask the query in comment section

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