0% found this document useful (0 votes)
16 views4 pages

Assembly Language Program Examples

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views4 pages

Assembly Language Program Examples

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

■ Assembly Language Programs Collection

■ Program 1: String Concatenation (Simplified)


Objective: To concatenate two strings (STR1 and STR2) and store the combined result in
RESULT.
Explanation: Registers used and operation details for concatenating two strings ending with CR
(0x0D).
;----------------------------------------------------------
; Program: String Concatenation (Simplified)
;----------------------------------------------------------
ORIGIN 1000H

LOAD R0, STR1_ADDR ; R0 = address of first string


LOAD R1, STR2_ADDR ; R1 = address of second string
LOAD R2, RESULT_ADDR ; R2 = destination buffer
LOAD R4, CR_VALUE ; R4 = end-of-string marker (0x0D)
CALL STRING_CONCAT ; Call subroutine
BRANCH END_PROGRAM ; Program ends

;----------------------------------------------------------
; Subroutine: STRING_CONCAT
; Description: Copies STR1 and STR2 into RESULT
;----------------------------------------------------------

STRING_CONCAT:

COPY_FIRST:
LOAD R3, [R0]
SUBTRACT R5, R3, R4
BRANCH.Z START_SECOND

STORE R3, [R2]


ADD R0, R0, 1
ADD R2, R2, 1
BRANCH COPY_FIRST

START_SECOND:
LOAD R3, [R1]
SUBTRACT R5, R3, R4
BRANCH.Z END_CONCAT

STORE R3, [R2]


ADD R1, R1, 1
ADD R2, R2, 1
BRANCH START_SECOND

END_CONCAT:
STORE R4, [R2]
RETURN

;----------------------------------------------------------
; Data Section
;----------------------------------------------------------
ORIGIN 2000H

STR1_ADDR: DATAWORD STR1


STR2_ADDR: DATAWORD STR2
RESULT_ADDR: DATAWORD RESULT
CR_VALUE: DATAWORD 0x0D

STR1: DATAWORD 'H'


DATAWORD 'E'
DATAWORD 'L'
DATAWORD 'L'
DATAWORD 'O'
DATAWORD 0x0D

STR2: DATAWORD 'W'


DATAWORD 'O'
DATAWORD 'R'
DATAWORD 'L'
DATAWORD 'D'
DATAWORD 0x0D

RESULT: RESERVE 32

END_PROGRAM:
BRANCH END_PROGRAM

■ Program 2: Passing Parameters through Stack


Objective: Demonstrate parameter passing through stack to compute the sum of N numbers.
;----------------------------------------------------------
; Program: Parameter Passing through Stack
;----------------------------------------------------------

MOVE R2, #NUM1


SUBTRACT SP, SP, #4
STORE R2, (SP)

LOAD R2, N
SUBTRACT SP, SP, #4
STORE R2, (SP)

CALL LISTADD

LOAD R2, 4(SP)


STORE R2, SUM
ADD SP, SP, #8

LISTADD:
SUBTRACT SP, SP, #16
STORE R2, 12(SP)
STORE R3, 8(SP)
STORE R4, 4(SP)
STORE R5, (SP)

LOAD R3, 16(SP)


LOAD R4, 20(SP)
CLEAR R5
CLEAR R6

LOOP:
LOAD R2, (R4)
ADD R5, R5, R2
ADD R4, R4, #4
ADD R6, R6, #1
SUBTRACT R2, R3, R6
BRANCH_IF_R2≠0 LOOP

STORE R5, 20(SP)

LOAD R5, (SP)


LOAD R4, 4(SP)
LOAD R3, 8(SP)
LOAD R2, 12(SP)
ADD SP, SP, #16
RETURN

;----------------------------------------------------------
; Data Section
;----------------------------------------------------------
N: DATAWORD 3
SUM: DATAWORD 0
NUM1: DATAWORD 10
NUM2: DATAWORD 20
NUM3: DATAWORD 30

■ Program 3: Compute Test-wise Sums Using Nested Loops


Objective: Compute sum of marks per test (column-wise) using nested loops.
;------------------------------------------------------------
; Program: Compute Test-wise Sums Using Nested Loops
;------------------------------------------------------------

MOVE R4, J
ADD R4, R4, #1
MULTIPLY R4, R4, #4

MOVE R1, #LIST


ADD R1, R1, #4
MOVE R3, #SUM

MOVE R10, J

OUTER:
MOVE R11, N
CLEAR R2
CLEAR R0

INNER:
ADD R0, R0, (R1, R2)
ADD R2, R2, R4
SUBTRACT R11, R11, #1
BRANCH_IF_[R11]>0 INNER
LOAD R3, R0
ADD R3, R3, #4
ADD R1, R1, #4
SUBTRACT R10, R10, #1
BRANCH_IF_[R10]>0 OUTER

You might also like