Computer Organization and Architecture: Chapter 1: Introduction

Assembly Language Programming Examples

Example Programs - Assembly Language Programming Examples

Assembly Language Programming

Programming Examples

 

Example 1: Add Two Numbers

Program to add two numbers stored in memory and store the result back in memory.

Program:

.data

num1: .word 10

num2: .word 20

result: .word 0.

.text

main:

lw $t0, num1  # Load num1 → $t0

lw $t1, num2  # Load num2 → $t1

add $t2, $to, $t1 # $t2 = $t0 + $t1

sw $t2, result  #Store result→ memory

j end     #End program

end:

Example 2 : Subtract and Store Result

Program to perform subtraction of two numbers and save the result in memory.

Program:

.data

num1: .word 50

num2: .word 30

diff: .word 0

.text

main:

       lw $t0, num1

       lw $t1, num2

       sub $t2, $t0, $t1 # $t2 = $t0 ‒ $t1

       sw $t2, diff

       j end

end:

Example 3 : Sum of First 10 Natural Numbers

Program to calculate 1+2+3+ ... + 10 and store the result.

Program:

.data

sum: .word 0

.text

main:

       li $t0, 1  #i=1

       li $t1, 0  # sum = 0

loop:

       add $t1, $t1, $t0  #sum= sum + i

        addi $t0, $t0, 1  #sum #i = i+1

       ble $t0, 10, loop  # continue until i < = 10

       sw $t1, sum   # store result

       j end

end:

Example 4 : Find the Larger of Two Numbers

Program to compare two numbers and store the larger one.

Program:

.data

num1: .word 45

num2: .word 78

larger: .word 0

.text

main:

       lw $t0, num1

       lw $t1, num2

       bgt $t0, $t1, num1_is_larger

       sw $t1, larger # if num2 > num1

       j end

       num1_is_larger:

       sw $t0, larger

end:

Example 5 : Factorial of a Number (Loop‒Based)

Program to calculate the factorial of 5 (i.e., 5!= 120) and store the result in memory.

Program:

.data

num: .word 5

fact: .word 1

.text

main:

       lw $t0, num    #n = 5

       li $t1, 1     # fact = 1

loop:

       mul $t1, $t1, $t0  # fact fact* n

       addi $to, $t0, ‒1  # n=n‒1

       bgtz $t0, loop   # loop until n > 0

       sw $t1, fact    #store factorial

       j end  

end:

Example 6 : Array Sum

Program to sum the elements of an array,

Program:

 .data

array: .word 1, 2, 3, 4, 5

sum: .word 0

n: .word 5

.text

main:

       la $t0, array  # address of array

       lw $t3, n   # number of elements (5)

       li $t1, 0    # sum = 0

       li $t2, 0    # i = 0

loop:

       lw $t4, 0($t0)   # load array[i]

       add $t1, $t1, $t4 # sum += array[i]

       addi $t0, $t0, 4  # move to next element

       addi $t2, $t2, 1  #i++

       blt $t2, $t3, loop # loop if i < n

       sw $t1, sum   # store result

       j end

end:


Computer Organization and Architecture: Chapter 1: Introduction : Tag: Computer : - Assembly Language Programming Examples


Computer Organization and Architecture: Chapter 1: Introduction



Under Subject


Computer Organization and Architecture

CW25201 3rd Semester IT department. | 2025 Regulation | 3rd Semester 2025 Regulation



Related Subjects


Discrete Mathematics

MA25C14 3rd Semester CSE,IT,CY,AIDS departments. | 2025 Regulation | 3rd Semester 2025 Regulation


Data Structures

CS25C08 3rd Semester CSE,IT,CY,AIDS departments. | 2025 Regulation | 3rd Semester 2025 Regulation


Computer Organization and Architecture

CW25201 3rd Semester IT department. | 2025 Regulation | 3rd Semester 2025 Regulation


Object Oriented Programming

CS25C07 3rd Semester IT department. | 2025 Regulation | 3rd Semester 2025 Regulation


Web Technologies

IT25301 3rd Semester IT department. | 2025 Regulation | 3rd Semester 2025 Regulation


English Communication Skills Laboratory I

EN25C03 3rd Semester all department. | 2025 Regulation | 3rd Semester 2025 Regulation


Skill Development Course I

3rd Semester all department. | 2025 Regulation | 3rd Semester 2025 Regulation