Few Programs I learned in the CortexM3 lab by hdrohithd |

Few Programs I learned in the CortexM3 lab

Author: hdrohithd



Few Programs I learned in the CortexM3 lab

;1 Multiply two 16 bit numbers

   AREA MYDATA, DATA, READWRITE
MEM DCD 0
    AREA MYCODE, CODE, READONLY ; AREA is directive to allocate memory space for code or data region
    EXPORT __main
__main
    LDR R0,=MEM            ; load address to register
    LDRH R1,[R0]        ; we get the halfword to R1 specified by address in r0
    LDRH R2,[R0,#2]        ; we get the another number to R2 specified by address in r0+2bytes
    MUL R2,R1,R2        ; multiply r2 = r1 x r2
    STR R2,[R0,#4]        ; store r2 data to address of memory = r0+4bytes
label B label             ; infinite loop
    END
Output

;2 ALP to find sum of first 10 integer numbers  
 AREA MYDATA, DATA, READWRITE
COUNT EQU 10
SUM EQU 0
    AREA MYCODE, CODE, READONLY ; AREA is directive to allocate memory space for code or data region
    EXPORT __main
__main
    LDR R0,=COUNT        ; load the value of COUNT=10 in R0
    LDR R1,=SUM            ; load the value of SUM=0 in R1 to hold partial sum
    LDR R2,=1            ; Load R2 with 1, the first number
LOOP ADD R1,R2,R1        ; Accumulate the sum in R1 by adding R2 to R1
    ADD R2,R2,#1        ; Increment R2 by 1 to get the next integer
    SUBS R0,R0,#1        ; Subtract R0 by 1 till count becomes 0
    BNE LOOP            ; branch to loop till R0 is not equal to 0
STOP B STOP                ; infinite loop
    END
Output


;3 ALP to find no. of 0's and 1's in a 32 bit data
AREA MYDATA, DATA, READWRITE
NUM EQU 0x0018    ;0000 0000 0000 0000  0000 0000 0001 1000
    AREA MYCODE, CODE, READONLY ; AREA is directive to allocate memory space for code or data region
    EXPORT __main
__main
    LDR R0,=NUM            ; load value of NUM in R0
    MOV R1,#32            ; Move 32 into R1 to count the number of bits
UP MOVS R0,R0,ROR #1    ; Rotate the number in R0 to right by one position
    BHI DOWN            ; branch to label DOWN if carry is set
    ADD R2,R2,#1        ; increment R2 which counts the number of zeros
    B NEXT                ; unconditionally branch to label NEXT
DOWN ADD R3,R3,#1        ; Increment R3 which counts the no. of 1's
NEXT SUBS R1,R1,#1        ; Decrement R1 to count the number of bits
    BNE UP                ; branch to label UP if count is non-zero
STOP B STOP                ; infinite loop
    END
Output


;4 ALP to determine whether the given 16-bit number is even / odd
  AREA MYDATA, DATA, READWRITE
NUM DCD 0
    AREA MYCODE, CODE, READONLY ; AREA is directive to allocate memory space for code or data region
    EXPORT __main
__main
    MOV R2,#0             ; initialize r2 to 0
    LDR R4,=NUM            ; Load address of NUM in R4
    LDR R0,[R4],#2        ; Load value stored in memory location pointed by R4 into R0 and increment R4 by 2
    MOVS R0,R0,ROR #1    ; rotate the number in R0 to right by one position
    BHI LOOP0            ; branch to label LOOP0 if carry is set
    MOV R2,#0XFFFF        ; move 0xFFFF to R2 if carry is not set, i.e even number
    B STOP                 ; unconditionally branch to STOP
LOOP0 MOV R2,#0X1111    ; move 0x1111 to R2 if carry is set, i.e odd number
STOP B STOP                ; infinite loop
    END
Output


;5 ALP to write data to RAM
;5 ALP to write data to RAM
	AREA MYDATA, DATA, READWRITE
RAMLOC DCB 0	;to allocate memory
	AREA MYCODE, CODE, READONLY ; AREA is directive to allocate memory space for code or data region
	EXPORT __main
DATA DCD 0x12345678	;deine constant data
__main
	LDR R0,DATA			; load data into R0
	LDR R1,=RAMLOC		; load R1 with address of RAMLOC location
	STR R0,[R1]			; store the data in R0 to the RAM location pointed by R1
STOP B STOP				; infinite loop
	END
 
Output

Comments: (0)

Please loging to post comment