Adsense

Showing posts with label Logical. Show all posts
Showing posts with label Logical. Show all posts

Write Embedded C code to perform the arithmetic and logical operations for integer data type on FRDM KL25Z.

Main C file

//-Global definition area of the C file-
#include "ALops_func.h"
//include the user defined header file containing adder function
//---main of the program---
int main(void){
volatile unsigned int temp0=0x9F00,temp1=0X6A0,temp2=0x0;
//define 13 variable of the data type long
volatile unsigned int temp3=0x0,temp5=0x0,temp6=0x0,temp7=0x0;
volatile unsigned int temp8=0x0,temp9=0x0,temp10=0x0,temp11=0x0;
volatile unsigned int temp12=0x0;
volatile unsigned long long temp4=0x0;
//define a variable of the data type long
temp2=add2(temp0,temp1);
temp3=sub2(temp0,temp1);
temp4=mul2(temp0,temp1);
temp5=div2(temp0,temp1);
temp6=rem (temp0,temp1);
temp7=and2(temp0,temp1);
temp8=or2(temp0,temp1);
temp9=xor2(temp0,temp1);
temp10=inv(temp0);
//left shift by 2 bit
temp11=left_shift(temp0,2);
//right shift by 2 bit
temp12=right_shift(temp0,2);
return(0);
}
 

// now you need to create header file same like C file you are creating
procedure: goto new file -> create new file -> paste code below mentioned -> save it with name ALops_func.h -> header file is ready to use



User defined header file

//Function defined in global definition area

unsigned int add2(unsigned int ,unsigned int );
unsigned int sub2(unsigned int ,unsigned int );
unsigned long long mul2(unsigned int ,unsigned int );
unsigned int div2(unsigned int ,unsigned int );
unsigned int rem (unsigned int ,unsigned int );
unsigned int and2(unsigned int ,unsigned int );
unsigned int or2(unsigned int ,unsigned int );
unsigned int xor2(unsigned int ,unsigned int );
unsigned int inv(unsigned int );
unsigned int left_shift(unsigned int ,unsigned int );
unsigned int right_shift(unsigned int ,unsigned int );

/*starting of the header file*/
#if!defined(ALops_func_h_)

/*Check if memory map has not been already included */
#define ALops_func_h_
//-bodyofheaderfile-
//addition of two unsigned integer
unsigned int add2(unsigned int x,unsigned int y){return(x+y);}
//subtraction of two unsigned integer
unsigned int sub2(unsigned int x,unsigned int y){return(x-y);}
//multiplicationof two unsigned integer
unsigned long long mul2(unsigned int x,unsigned int y){return(x*y);}
//divisonoftwo unsigned integer
unsigned int div2(unsigned int x,unsigned int y){return(x/y);}
//Modulus(remainder) of two unsigned integer
unsigned int rem (unsigned int x,unsigned int y){return(x%y);}
//AND of two unsigned integer
unsigned int and2(unsigned int x,unsigned int y){return(x&y);}
//OR of two unsigned integer
unsigned int or2(unsigned int x,unsigned int y){return(x|y);}
//XOR of two unsigned integer
unsigned int xor2(unsigned int x,unsigned int y){return(x^y);}
//invert the unsigned integer
unsigned int inv(unsigned int x){return(~x);}
//left shift by Y bits
unsigned int left_shift(unsigned int x,unsigned int y){return(x<<y);}
//right shift by Y bits
unsigned int right_shift(unsigned int x,unsigned int y){return(x>>y);}
//--endofthebody--
#endif

Write assembly code to perform logical operations

; Write assembly code to perform logical operations
;------------------------------------------------------------------
; section 1: Targeted logical instructions are of mainly four type
;     1- Logical (AND, OR, EXOR, & NOT)
;    2- Shift (Arithmetic & Logical)
;     3- Rotate
;    4- Reverse
;---------------------------------------------------------------------
        TTL transfer
        AREA Myprog, CODE, READONLY

ENTRY
        EXPORT main
main
       
        ;Initialize values into R1, and R2
        LDR R1,=0x0F0F0F0F
        LDR R2,=0x12345678
        LDR R3,=0xFEDCBA98
        LDR R4,=0xF0F0F0F0
        LDR R5,=0xFFFF0000
;part 1 logical operations       
        ANDS R3, R3, R1        ;ANDS <Rd>, <Rd>, <Rm>   
        ;Rd = AND(Rd, Rm), APSR.N and APSR.Z update
        ORRS R4, R4, R2        ;ORRS <Rd>, <Rd>, <Rm>   
        ;Rd = OR(Rd, Rm), APSR.N and APSR.Z update
        EORS R5, R5, R1        ;EORS <Rd>, <Rd>, <Rm>   
        ;Rd = XOR(Rd, Rm), APSR.N and APSR.Z update
       
        ;Logical Bitwise Clear
        BICS R1, R1, R2        ;BICS <Rd>, <Rd>, <Rm>   
        ;Rd = AND(Rd, NOT(Rm)), APSR.N and APSR.Z update (0x00000FF0=> 0xFFFFF00F)
       
        ;Logical Bitwise NOT
        MVNS R6, R2            ;MVNS <Rd>, <Rm>       
        ;Rd = NOT(Rm), APSR.N and APSR.Z update
       
        ;Test (bitwise AND) the AND but result is not stored
        TST R7, R2            ;TST <Rn>, <Rm>           
        ;Calculate AND(Rn, Rm), APSR.N and APSR.Z update
       
        LDR R3,=0xF1234560
        LDR R1,=0x01
        LDR R2,=0x04
;Part 2 shift operations
        ; Arithmetic shift operaration 1000 1111 -> 1100 0111
        ;When ASR is used, the MSB of the result is unchanged,
        ;and the Carry flag is updated using the last bit shifted out.
        ASRS R3, R3, R1        ;ASRS <Rd>, <Rd>, <Rm>   
        ;Rd = Rd >> Rm, last bit shift out is copied to
        ;Flags APSR.C, APSR.N and APSR.Z are updated
        ASRS R4, R3,#0x04    ;ASRS <Rd>, <Rm>, #immed5
        ;Rd = Rm >> immed5, last bit shifted out is copied to
        ;APSR.C, APSR.N and APSR.Z are also updated.
       
        ;Logical shift operations
        LSLS R3, R3, R1        ;LSLS <Rd>, <Rd>, <Rm>   
        ;Rd = Rd << Rm, last bit shifted out is copied to
        ;APSR.C, APSR.N and APSR.Z are also updated
        LSLS R5, R3, #0x01    ;LSLS <Rd>,<Rm>,#immed5
        ;Rd = Rm << #immed5, last bit shifted out is copied to
        ;APSR.C, APSR.N and APSR.Z are also updated
        LSRS R3, R3, R2        ;LSRS <Rd>, <Rd>, <Rm>   
        ;Rd = Rd >> Rm, last bit shifted out is copied to
        ;APSR.C, APSR.N and APSR.Z are also updated
        LSRS R7, R3,#0x01    ;LSRS <Rd>,<Rm>,#immed5   
        ;Rd = Rm >> #immed5, last bit shifted out is copied to
        ;APSR.C, APSR.N and APSR.Z are also updated

        ;0001 1010 shift right by 2 times -> 0000 0110
       
;Part 3 rotate operation
        RORS R3, R3, R2        ;RORS <Rd>, <Rd>, <Rm>   
        ;Rd = Rd rotate right by Rm bits, last bit shifted out is
        ;copied to APSR.C, APSR.N and APSR.Z are also updated
       
        ;0001 1010 4 time rotate to right -> 1010 0001
       
        LDR R4,=0x56789ABC
        LDR R3,=0x12345678
;Part 4 reverse operations
        REV R7, R4            ;REV <Rd>, <Rm>           
        ;Rd = {Rm[7:0], Rm[15:8], Rm[23:16], Rm[31:24]}
        REV16 R6, R3        ;REV16 <Rd>, <Rm>       
        ;Rd = {Rm[23:16], Rm[31:24], Rm[7:0] , Rm[15:8]}
        REVSH R5, R4        ;REVSH <Rd>, <Rm>       
        ;Rd = SignExtendof 7 to [16:31],({Rm[7:0] , Rm[15:8]})

done
        SWI &11            ; Call system interrupt to end the program
        END

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