LAB-1
1. Write an assembly language program to print numbers 0 to 15 in Hex.
AREA program_data, DATA, READWRITE
num DCD 0
AREA program_code, CODE, READONLY
ENTRY
EXPORT main
main
LDR R0,=num
LDR R1,[R0] ; r1 = num
loop
CMP R1,#16
BEQ Stop
MOV R3,R1 ; r3 = print val
ADD R1,R1,#1
B loop
Stop
B Stop
END
2. Write an assembly language program to compare two values stored in memory
and store the larger one at a new location.
AREA program_data, DATA, READWRITE
n1 DCD 0xA
n2 DCD 0xB
res DCD 0x0
AREA program_code, CODE, READONLY
ENTRY
EXPORT main
main
LDR R0, =n1
LDR R0, [R0]
LDR R1, =n2
LDR R1, [R1]
CMP R0, R1
MOVGT R2, R0 ; If R0 > R1, R2 = R0
MOVLE R2, R1 ; Else, R2 = R1
LDR R3, =res
STR R2, [R3]
Stop
B Stop
END
3. Write an assembly language code that sums 5 integers stored in memory and
stores the result in another memory location.
AREA program_data, DATA, READWRITE
arr DCD 1,2,3,4,5
n DCD 5
res DCD 0
AREA program_code, CODE, READONLY
ENTRY
EXPORT main
main
LDR R0, =arr
LDR R1, =n
LDR R1, [R1]
LDR R2,=res
MOV R3,#0 ; sum
loop
CMP R1,#0
BEQ done
LDR R4,[R0],#4 ; arr[i], i++
ADD R3,R3,R4 ; sum = sum+arr[i]
SUB R1,R1,#1 ; cnt --
B loop
done
STR R3,[R2]
Stop
B Stop
END
4. You are given a 32-bit value. Write an assembly code to toggle every even bit (bit
positions 0, 2, 4, ...) and store the result. Input value will be in the memory address
0x20000000 and store the result in the memory 0x 20000004
AREA my_code, CODE, READONLY
ENTRY
EXPORT main
n DCD 0x12345678
main
LDR R0, =0x20000000
LDR R1,=n
LDR R1,[R1]
STR R1, [R0]
LDR R1, [R0]
EOR R2, R1, #0x55555555 ; odd toggling : 0xAAAAAAAA
LDR R3, =0x20000004
STR R2, [R3]
Stop
B Stop
END
5. Write an assembly code to compare two signed 32-bit integers and store the result
in memory.
1 if A > B
0 if A == B
-1 if A < B
AREA my_data, DATA, READWRITE
n1 DCD 0xAAAAAAAA ; First number
n2 DCD 0xBAAAAAAA ; Second number
res DCD 2 ; To store result
AREA my_code, CODE, READONLY
ENTRY
EXPORT main
main
LDR R0, =n1
LDR R0, [R0] ; R0 = n1
LDR R1, =n2
LDR R1, [R1] ; R1 = n2
LDR R3, =res ; R3 = result address
CMP R0, R1 ; Compare signed
BEQ st_zero
BGT st_one
BLT st_neg_one
st_zero
MOV R4, #0
STR R4, [R3]
B Stop
st_one
MOV R4, #1
STR R4, [R3]
B Stop
st_neg_one
MOV R4, #-1
STR R4, [R3]
B Stop
Stop
B Stop
END
6. 8. Write an assembly language code to count how many bits are set (1s) in a
32-bit integer and store the result in the memory.
AREA my_data, DATA, READWRITE
n1 DCD 0xB0B ; First number
res DCD 0 ; To store result
AREA my_code, CODE, READONLY
ENTRY
EXPORT main
main
LDR R0, =n1
LDR R0, [R0] ; R0 = n1
MOV R1,#0 ;cnt
loop
CMP R0,#0
BEQ done
AND R2,R0,#0x1 ; r2 =0 || 1
ADD R1,R1,R2 ; cnt += r2
LSR R0,R0,#1
B loop
done
LDR R4,=res
STR R1,[R4]
Stop
B Stop
END
7. Write an assembly code to count the number of 1s in an 8-bit value which is
stored in memory. If it’s even, store 0 (even parity) and If it’s odd, store 1 (odd
parity).
AREA my_data, DATA, READWRITE
n1 DCB 0xB0 ; First number
res DCB 0 ; To store result
AREA my_code, CODE, READONLY
ENTRY
EXPORT main
main
LDR R0,=n1
LDRB R0, [R0] ; R0 = n1
MOV R1,#0 ;cnt
loop
CMP R0,#0
BEQ cnt_done
AND R2,R0,#0x1 ; r2 =0 || 1
ADD R1,R1,R2 ; cnt += r2
LSR R0,R0,#1
B loop
cnt_done
MOV R5,#2
UDIV R3,R1,R5 ; R3 = cnt/2
MUL R3,R3,R5 ; r3 = r3*2
SUB R1,R1,R3 ; rem = cnt - r3
LDR R4,=res
STR R1,[R4]
Stop
B Stop
END
8. A
9. A
10.A
11.A
12.a