Vous êtes sur la page 1sur 7

Lab Experiment 5: Programming in Assembly Language

Task: Write programs in Thumb instruction set and submit a report


Note: Define two variables s1 and s2 of size 4 bytes and assign them your roll numbers. In this
and subsequent experiments
a. b0 refers to LSB and b31 refers to MSB of an integer
b. Set/On refers to bx as logic1
c. Reset/Off/Clear refers to bx as logic0

More Assembly Problems


1. Write a program in Assembly language so that it determines the sum of the five even
numbers starting from 20.
Program 1
2. Based on your program above, fill in every instruction used in each row in table. Mention
the opcode with instruction size using disassembly window.
Instruction Opcode Instruction Size
3. What are the reset values of PC and SP for this program 1
SP = _______________
PC = _______________

4. Write and compile the following program. Fill in the blanks placed in comments after each
program statement.
Program 2
STACKSIZE EQU 0x100
SLOPE EQU 5
OFFSET EQU 10
ARRAYSIZE EQU 4
; Stack area initialization in RAM memory
AREA STACK, NOINIT, READWRITE, ALIGN=3
StackMem SPACE STACKSIZE
; Vector table initialization in ROM memory
AREA RESET, DATA, READONLY
EXPORT Vectors
Vectors
DCD StackMem + STACKSIZE ; stack pointer for empty stack
DCD Reset_Handler ; reset vector
ALIGN
; An array of variables in RAM
AREA MyDSTdata, DATA, READWRITE
dst_data DCW 0, 0, 0, 0
; An array of constants in ROM
AREA MySRCdata, DATA, READONLY
src_data DCW 1, 2, 3, 4
; User program
AREA |.text|, CODE, READONLY, ALIGN=2
ENTRY
EXPORT Reset_Handler
Reset_Handler
MOV r2, #ARRAYSIZE ; R2 = _______________
MOV r3, #OFFSET ; R3 = _______________
MOV r4, #SLOPE ; R4 = _______________
LDR r5, =src_data ; R5 = _______________
LDR r6, =dst_data ; R6 = _______________
; Write values for each loop iteration
Loop ;iteration 1
LDRH r0, [r5], #2 ; R0 = _______________
MLA r0, r0, r4, r3 ; R0 = _______________
STRH r0, [r6], #2 ; R0 = _______________
SUBS r2, r2, #1 ; R2 = _______________
BNE Loop
Loop_Forever
B Loop_Forever
END

5. What are the addresses assigned to following labels:


Address of Loop = _________________
Address of Loop_Forever = ________________

6. Explain the functionality of the following assembly program 2.

7. What will be the contents of the ‘dst_data’ string?


dst_data = __________________,
__________________,
__________________,
__________________

8. Write a program that calculates sum of s1 and s2 stored in ROM (where s1 and s2 are your
roll numbers). The program stores result back in variable Result placed in RAM.
Program 3
9. Write a program that calculates square of number X stored in ROM (use addition only).
The program stores square back in variable Result placed in RAM.
Program 4
10. Execute the program 5 and confirm that it works correctly. Modify it by assigning the Data
AREA of X and Y the READWRITE attribute and see its impact on the working of the
program. In case it does not work properly, try to find the reason for it.
Program 5
; This ARM Assembly language program multiplies two positive
; numbers by repeated addition.
THUMB ; Marks the THUMB mode of operation
StackSize EQU 0x00000100 ; Define stack size to be 256 byes
; Allocate space for the stack.
AREA STACK, NOINIT, READWRITE, ALIGN=3
StackMem SPACE StackSize
; Initialize the two entries of vector table.
AREA RESET, DATA, READONLY
EXPORT __Vectors
__Vectors
DCD StackMem + StackSize ; SP value when stack is empty
DCD Reset_Handler ; reset vector
ALIGN
; Data Variables are declared in DATA AREA
AREA ROMdata, DATA, READONLY
X DCD 10
Y DCD 3
AREA RAMdata, DATA, READWRITE
PRODUCT DCD 0 ;accumulates product of X and Y
; The user code (program) is placed in CODE AREA
AREA |.text|, CODE, READONLY, ALIGN=2
ENTRY ; ENTRY marks the starting point
; of the code execution
EXPORT Reset_Handler
Reset_Handler ; User Code Starts from next line
LDR R0, =X ; load the address of X in R0
LDR R1, =Y ; load the address of Y in R1
LDR R2, =PRODUCT ; load the address of PRODUCT in R2
LDR R3, [R0] ; load the value of X in R3
LDR R4, [R1] ; load the value of Y in R4
LDR R5, [R2] ; load the value of PRODUCT in R5
Lbegin
CBZ R4, Lend
ADD R5, R3
SUB R4, #1
B Lbegin
Lend
STR R5, [R2] ; store the product back to PRODUCT
ALIGN
END ; End of program, matched with ENTRY

11. Reason if the program doesn’t work correctly.

Note: Attach all programs with this manual

Vous aimerez peut-être aussi