Adsense

Showing posts with label Arithmetic. Show all posts
Showing posts with label Arithmetic. 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 Arithmetic operations

; Write assembly code to perform arithmetic operations
;------------------------------------------------------------------
; section 1: Targeted arithmetic instructions are of mainly four type
;     1- Addition
;    2- Subtraction
;     3- Multiplication
;    4- Comparision
;---------------------------------------------------------------------
        TTL transfer
        AREA Myprog, CODE, READONLY

ENTRY
        EXPORT main
main
        
        ;Initialize values into R1, and R2
        LDR R1,=0xFA
        LDR R2,=0x15
        
;part 1 Addition operations        
        ;Three operand arithmetic instructions
        ADD R2,R1,R2        ;ADDS <Rd>, <Rn>, <Rm>     
        ;Rd = Rn + Rm, APSR update
        ADDS R3,R1,#0x07    ;ADDS <Rd>, <Rn>, #immed3
        ;Rd = Rn + ZeroExtend(#immed3), APSR update
        ADDS R3,#0xFF        ;ADDS <Rd>, #immed8        
        ;Rd = Rd + ZeroExtend(#immed8), APSR update
        ADD R4,R2            ;ADD <Rd>, <Rm>            
        ;Rd = Rd + Rm
        
        ;Arithmetic operations with SP
        ADD R3, SP, R3        ;ADD <Rd>, SP, <Rd>        
        ;Rd = Rd + SP
        ADD SP, R2            ;ADD SP, <Rm>            
        ;SP = SP + Rm
        ADD R2, SP, #0x04    ;ADD <Rd>, SP, #immed8    
        ;Rd = SP + ZeroExtend (#immed8)
        ADD SP, SP, #0x08    ;ADD SP, SP, #immed7    
        ;SP = SP + ZeroExtend(#immed7)
        
        ;Arithmetic operations with PC
        ADD R2, PC, #0x04    ;ADD <Rd>, PC, #immed8     
        ;Rd = PC[31:0] + ZeroExtend(#immed8 << 2).

        
        ;Arithmetic operations with carry
        ADCS R3, R2            ;ADCS <Rd>, <Rm>            
        ;Rd = Rd + Rm + Carry.

;Part 2 Subtraction oprtations        
        ;Three operand arithmatic instructions
        SUBS R5, R1, R2        ;SUBS <Rd>, <Rn>, <Rm>        
        ;Rd = Rn – Rm, APSR update
        SUBS R5, R1, #0x07    ;SUBS <Rd>, <Rn>, #immed3    
        ;Rd = Rn – ZeroExtend(#immed3), APSR update.
           SUBS R5, #0x01        ; SUBS <Rd>, #immed8        
        ;Rd = Rd – ZeroExtend(#immed8), APSR update.

        ;Arithmetic operations with SP
        SUB SP, SP, #0x04    ;SUB SP, SP, #immed7        
        ;SP = SP – ZeroExtend(#immed7)
        
        ;Arithmetic  operations with carry
        
        SBCS R6, R6, R2        ;SBCS <Rd>, <Rd>, <Rm>        
        ;Rd = Rd – Rm – Borrow, APSR update
        
        ;Reverse Subtract
        RSBS R7, R1, #0x00    ;RSBS <Rd>, <Rn>, #0        
        ;Rd = 0 – Rm, APSR update

;part 3 multiplication        
        ;Multiply
        MULS R7, R1, R7        ;MULS <Rd>, <Rm>, <Rd>        
        ;Rd = Rd * Rm, APSR.N and APSR.Z

;part 4 comparision
        ;Compare by subtraction but result is not stored
        ;Rm >Rn C =0, Z =0
        ;Rm <Rn C =1, Z =0
        ;Rm =Rn C =0, Z =1
        CMP R1, R2            ;CMP <Rn>, <Rm>                
        ;Calculate Rm – Rn, APSR update
        CMP R2, #0x04        ;CMP <Rn>, #immed8            
        ; ZeroExtended(#immed8) – Rd , APSR update
        CMN R1, R2            ;CMN <Rn>, <Rm>                
        ;Compare negative, Calculate  NEG(Rm) – Rn , APSR update

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