Hello Dear Readers,
Today in this post I will provide some basics to advanced ARM's assembly language practice QA part-3, I have used the Keil tool for code writing.
Q-1). Create a stack that starts from address 0x5000 and contains 5 values, and the stack pointer always points to the uppermost value.
Code:
; program to create stack and store 5 values
area stack, code, readonly
entry
ldr r13,=0x5000
ldr r1,=10
ldr r2,=11
ldr r3,=12
ldr r4,=13
ldr r5,=14
STMDB SP!,{r1-r5}
add r4,r3,r5
end
Output:
Q-2). Write a code to find the multiplication of 10 numbers stored consecutively starting from 0x4000.
Code:
; program of the multiplication of the 10 numbers
area mul, code, readonly
entry
mov r0,#0x4000
mov r1,#9
ldr r2,[r0]
next
add r0,r0,#4
ldr r3,[r0]
muls r2,r3,r2
subs r1,r1,#1
bne next
add r0,r0,#44
str r2,[r0]
stop b stop
end
Output:
Q-3). Write an assembly language program to perform dot product of two vectors using a subroutine. Pass arguments to the subroutine using pass-by-stack technique.
Code:
; program of product two vector
sram_base equ 0x00004000
area paasbystack, code, readonly
entry
ldr sp,=sram_base
mov r0,#2 ; vector 1 scaler in x direction
mov r1,#4 ; scaler in y direction
mov r2,#2
mov r3,#2
STMIA sp!,{r0-r3}
BL loop
LDMDB sp!,{r0-r3}
stop b stop
loop
STMIA sp!,{r4-r6,lr}
ldr r5,[sp, #-0x20]
ldr r6,[sp, #-0x1c]
mul r4,r5,r6
ldr r5,[sp, #-0x18]
ldr r6,[sp, #-0x14]
mla r4,r5,r6,r4
str r4,[sp, #-0x20]
LDMDB sp!,{r4-r6,pc}
End
Output:
Q-4). Write an assembly language program to reverse a string without using any other memory location.
Code:
; program of the reverse string
area string, code,readonly
CR EQU 0x0D
entry
ldr r0,=data
ldr r2,=0x40000000
ldr r3,=0x40000020
mov r4,#00
up
ldrb r5,[r0],#1
cmp r5,#CR
strb r5,[r2],#1
add r4,r4,#1
BNE up
sub r4,r4,#1
sub r2,r2,#2
BNE down
down
ldrb r6,[r2]
strb r6,[r3],#1
sub r2,r2,#1
subs r4,r4,#1
BNE down
stop b stop
data dcb "DAIICT",CR
end
Output:
Q-5). Count the number of alphabets in a string.
Code:
area count,code,readonly
ENTRY
LDR R0, =0 ; length = 0
LDR R1, =data ;
loop
LDRB R2, [R1] ; load next byte of string
CMP R2, #0
BEQ again ; finished if 0
ADD R0, R0, #1 ; length += 1
ADD R1, R1, #1 ; R1 -> next byte in s
B loop ; next byte
again
stop B stop
data DCB "daiict"
end
Output:
Q-6). Write a code to find the count of even numbers from 8 numbers stored consecutively starting from 0x4000.
Code:
area even,code,readonly
SRAM EQU 0X4000
entry
LDR R0, =0X8
LDR R1, =0X0
LDR R2, =0X1
LDR SP, =SRAM
loop2
LDMIA SP!, {R3}
ANDS R3, R3, R2
BEQ loop
SUBS R0, R0, #1
BNE loop
BEQ STOP
loop
ADD R1, R1, #1
SUBS R0, R0, #1
BNE loop2
STOP B STOP
END
Output:
Q-7). Write an assembly language program to solve the expression x^2+y^2 using a subroutine. Pass arguments to the subroutine using both pass-by-register and pass-by-reference techniques.
Code:
AREA equ,CODE,READONLY
SRAM EQU 0X4000
ENTRY
LDR SP, =SRAM
LDR R0, =0X5
BL loop
MOV R4, R2
LDR R0, =0X6
BL loop
MOV R5, R2
ADDS R7, R4, R5
STOP B STOP
loop
STMIA SP!, {LR}
MOV R1, R0
MULS R2, R1, R0
LDMDB SP!, {PC}
END
Output:
Q-8). Write an assembly language program to check whether a string is a palindrome or not using a subroutine. Pass arguments to the subroutine using the pass-by-reference technique.
Code:
AREA plan, CODE, READONLY
ENTRY
LDR SP, =SRAM
LDR R8, =SRAM +100
BL check
LDR R2, =0X4000
LDR R3, =0X4100
LDR R4, [R2]
LDR R5, [R3]
STMIA R8, {R4,R5}
BL same
STOP B STOP
check
STMIA SP!, {LR}
LDR R0, =DATA
LDR R2, =0x4000
LDR R4, =0X5
l2
LDRB R5, [R0]
STRB R5, [R2]
ADD R0, R0, #1
ADD R2, R2, #1
SUBS R4, R4, #1
BNE l2
SUB R2, R2, #1
LDR R4, =0X5
LDR R3, =0x4100
loop
LDRB R6, [R2]
STRB R6, [R3]
SUB R2, R2, #1
ADD R3, R3, #1
SUBS R4, R4, #1
BNE loop
LDMDB SP!, {PC}
same
STMIA SP!, {LR}
LDMIA R8, {R9-R10}
CMP R9, R10
LDREQ R11, =0X11111111
LDMDB SP!, {PC}
SRAM EQU 0X5000
DATA DCB "VIVVIV"
END
Output:
Q-9). Write an assembly language program to find the GCD of two numbers using a subroutine. Pass arguments to the subroutine using pass-by-stack technique.
Code:
AREA program, CODE, READONLY
SRAM EQU 0X4000
ENTRY
LDR SP, =SRAM
LDR R1, =0X9
LDR R2, =0X5
STMIA SP!, {R1-R2}
AGAIN
BL GCD
BNE AGAIN
STOP B STOP
GCD
STMIA SP!, {LR}
LDR R8, [SP, #-8]
LDR R9, [SP, #-12]
CMP R9, R8
SUBSGT R9, R9, R8
SUBSLT R8, R8, R9
STR R8, [SP, #-8]
STR R9, [SP, #-12]
END
Output:
Connect with me
Super post
ReplyDeleteWell informed post
ReplyDelete