Mes Programs
Mes Programs
1. Construct/Develop an ALP to find the sum of N integers stored in an array. The result
is stored in internal RAM.
AREA ADDITION, CODE, READONLY
ENTRY
MOV R5, #5 ; initialize counter(n) to 5
MOV R0, #0 ; initialize sum to 0
LDR R1, =VALUE1 ; loads address of first value from VALUE1
LOOP: LDRH R3, [R1], #02 ; loads the contents of r1 to r3 and increments by 2
ADD R0, R0, R3 ; add r0 and r3 and move result to r0
SUBS R5, R5, #01 ; decrement counter by 1 till 0
BNE LOOP ; branch back to loop label
LDR R4, =RESULT ; loads the address of result
STR R0, [R4] ; stores result in contents of r4
BACK B BACK
VALUE1 DCW 0x0001,0x0002,0x0003,0x0004,0x0005 ; array of 16-bit numbers
AREA DATA2, DATA, READWRITE ; to store result in given address
RESULT DCD 0x0
END
6. Construct/Develop an ALP to count the number of ones and zeroes in a given number.
AREA ONEZERO, CODE, READONLY
ENTRY
MOV R2, #0 ; initialize counter for ones
MOV R3, #0 ; initialize counter for zero
MOV R6, #0x0001 ; loads address of given number
LOOP
MOV R1, #32 ; initialize bit counter to 32
LDR R0, [R6] ; loads the content of r6 to r0
LOOP0
MOVS R0, R0, ROR #1 ; rotate right and place the bit in the carry flag
BHI ONES ; if carry is set (bit was 1), branch to ones
ZEROS
ADD R3, R3, #1 ; increment zero counter
B LOOP1 ; skip the ones increment
ONES
ADD R2, R2, #1 ; increment one counter
LOOP1
SUBS R1, R1, #1 ; decrement bit counter
BNE LOOP0 ; if bit counter is not zero, repeat
BACK B BACK
END
9. Construct/Develop an ALP to check whether the given number is even or odd number
AREA EVENODD, CODE, READONLY
ENTRY
MOVE R1, #3 ; move the given number to r1
ROR R1, R1, #1 ; rotate right the number by 1
BHI LOOP ; branch to loop label if carry is high(1)
MOVE R0, #0 ; set r0 to 0
LOOP MOVE R0, #1 ; set r0 to 1
BACK B BACK
END
[NOTE: IF R0= 0, NUMBER IS EVEN; R0=1, NUMBER IS ODD]
10. Construct/Develop an ALP to find the GCD of 2 numbers
AREA GCD, CODE, READONLY
ENTRY
MOV R1, #48 ; move first number to r1
MOV R2, #18 ; move second number to r2
LOOP CMP R1, R2 ; compare r1 and r2
BEQ GCD ; if r1==r2 , branch to gcd label
BHI LOOP2 ; if r1>r2, branch to loop2 label
SUB R2, R2, R1 ; if r1<r2, subtract r1 from r2 and move result to r2
B LOOP ; branch back to loop label
LOOP2 SUB R1, R1, R2 ; subtract r2 from r1 and move result to r1
B LOOP ; branch back to loop label
GCD MOV R0, R1 ; move the gcd result from r1 to r0
BACK B BACK
END
11. Write a C code and it’s corresponding assembly code to print the squares of the
integer 0 to 9.
AREA SQUARE, CODE, READONLY
ENTRY
MOV R0, #0 ; move 0 to r0
LOOP LDR R1, = DEST ; loads address of first value from DEST
CMP R0, #10 ; compare r0 value with 10
BEQ BACK ; if equal, branch to back label
MUL R2, R0, R0 ; square r0 and move to r2 otherwise
STR R2, [R1], #4 ; store the result from r1 to contents of r1 and increment by 4
ADD R0, R0, #1 ; increment r0 by 1
B LOOP ; branch back to loop
BACK B BACK
AREA DATA2, DATA, READWRITE ; result array address
DEST 0x0 END
C Program:
#include <stdio.h>
int square(int i);
int main(void)
{
for (int i=0; i<10; i++)
printf("Square of %d is %d\n", i, square(i));
}
int square(int i){
return i*i; }
12. With an example show how to call a subroutine from an assembly routine.(Example
of printf as a subroutine from C Libraries)