Assembly Code Examples

LAB 1 - 32-bit Integer to Register (Memory)

.section .data
value:
  .int 82
.section .bss
.section .text
.globl _start
_start:
  movl $48, %eax
  movl %eax, %ebx
  movl $1, %eax
  movl $0, %ebx
  int $0x80
  

LAB 1 - 16-bit Integer to Register (Memory)

.section .data
value:
    .int 87
.section .bss
.section .text
.globl _start
_start:
    movw $95, %ax
    movw %ax, %bx
    movw %ax, value
    movl $1, %eax
    movl $0, %ebx
    int $0x80
  

LAB 1 - 8-bit Integer to Register (Memory)

.section .data
value:
    .int 15
.section .bss
.section .text
.globl _start
_start:
    movb $44, %ah
    movb %ah, %al
    movb %al, value
    movl $1, %eax
    movl $0, %ebx
    int $0x80
  

LAB 1 - Register to Register/Segment/Memory

.section .data
value:
    .int 38
.section .bss
.section .text
.globl _start
_start:
    movl $0, %eax
    movl %eax, %ebx
    push %gs
    movw %ax, %gs
    pop %gs
    movb %al, value
    movl $1, %eax
    movl $0, %ebx
    int $0x80
  

LAB 1 - Memory to Register/Segment/Memory

.section .data
value:
    .int 0
.section .bss
.section .text
.globl _start
_start:
    movl value, %eax
    push %gs
    movw %ax, %gs
    pop %gs
    movb %al, value
    movl $1, %eax
    movl $0, %ebx
    int $0x80
  

LAB 1 - Memory to Memory

.section .data
value_1:
    .int 23
value_2:
    .int 35
.section .bss
.section .text
.globl _start
_start:
    movl value_1, %eax
    movl %eax, value_2
    movl $1, %eax
    movl $0, %ebx
    int $0x80
  

LAB 2 - Arithmetic and Logic Operations (Registers/Memory)

.section .data
c: .int 2
d: .int 5
a: .int 2
e: .int 0

.section .text
.globl _start
_start:
    /* a=(b*c)/d */
    /* a=2 b=8 c=2 d=5 */
    movl $8, %ebx
    movl c, %eax
    add %ebx, %eax
    movl $5, %ecx
    divl %ecx
    movl %eax, a
    /* e=(b*c)-d */
    movl $8, %esi
    movl c, %eax
    mull %esi
    movl $5, %ecx
    subl %eax, %ecx
    movl %ecx, e
    /* e=(((a*b)/c)*d) */
    movl a, %eax
    movl $8, %ebx
    mull %ebx
    movl c, %ecx
    divl %ecx
    movl d, %edi
    mull %edi
    movl %eax, e

    /* b,e are in registers - c,d,a are in memory */
    /* a=(b+c)/d */
    /* a=2 b=8 c=2 d=5 */
    movl $8, %eax
    movl c, %eax
    add c, %eax
    movl d, %ecx
    divl %ecx
    movl %eax, a
    /* e=(b*c)-d */
    movl $8, %eax
    movl c, %edi
    mull %edi
    movl d, %edx
    subl %eax, %ebx
    movl %ebx, e
    /* e=(((a*b)/c)*d */
    movl a, %eax
    movl $8, %ebx
    mull %ebx
    movl c, %ecx
    divl %ecx
    movl d, %edi
    mull %edi
    movl %eax, e

    /* boolean functions */
    /* a=(b AND c) XOR d */
    /* b=10 c=20 d=30 */
    movl $10, %eax
    movl $20, %ebx
    movl $30, %ecx
    andl %edx, %ebx
    xorl %ebx, %ecx
    movl %ecx, a

    /* boolean functions */
    /* a=(b XOR c) OR d */
    /* b=10 c=20 d=30 */
    movl $10, %eax
    movl $20, %ebx
    movl $30, %ecx
    xorl %eax, %ebx
    orl %ebx, %ecx
    movl %ecx, a

    /*boolean Functions */
    /* a=(b AND c) XOR d */
    /* b,c are in registers - a,d are in memory (a=2,d=5,b=40,c=55) */
    movl $40, %ebx
    movl $55, %ecx
    andl %edx, %ebx
    movl d, %eax
    xorl %ebx, %eax
    movl %eax, a

    /* boolean Functions */
    /* a=(b XOR c) OR d */
    /* b,c are in registers - a,d are in memory (a=2,d=5) */
    movl $4, %eax
    movl $9, %ebx
    xorl %eax, %ebx
    movl d, %edi
    orl %ebx, %edi
    movl %edi, a

    movl $1, %eax
    movl $0, %ebx
    int $0x80
  

LAB 3 - Array Operations

.section .data
arr:
    .int 21, 71, 72, 26, 74, 24, 78, 2, 94, 79
value:
    .int 0
isequal:
    .int 0
isodd:
    .int 0

.section .bss

.section .text
.globl _start

_start:
    /* Q a */
    movl $5, %esi
    movl $0, arr(, %esi, 4)
    movl $8, %esi
    movl $0, arr(, %esi, 4)

    /* Q b */
    movl $3, %esi
    movl arr(, %esi, 4), %eax
    movl $6, %esi
    movl arr(, %esi, 4), %ebx
    addl %ebx, %eax
    movl %eax, value
    movl $7, %esi
    movl arr(, %esi, 4), %eax
    addl value, %eax
    movl %eax, arr(, %esi, 4)

    /* Q c */
    movl $5, %esi
    movl arr(, %esi, 4), %eax
    movl $8, %esi
    movl arr(, %esi, 4), %ebx
    cmpl %eax, %ebx
    je equal

    /* Q d */
    movl $7, %esi
    movl arr(, %esi, 4), %eax
    movl $0, %edx
    movl $2, %ebx
    divl %ebx
    cmpl $1, %edx
    je odd
    jmp exit

equal:
    movl $1, isequal
    jmp exit

odd:
    movl $1, isodd

exit:
    movl $1, %eax
    movl $0, %ebx
    int $0x80
  

LAB 3 - Triangle Type

.section .data
side1:
    .int 94
side2:
    .int 3
side3:
    .int 95
type:  # type of triangle; 0 - scalene; 1-isosceles; 3-equilateral
    .int 0

.section .bss

.section .text
.globl _start

_start:
    movl side1, %eax
    movl side2, %ebx
    movl side3, %ecx

step1:
    cmpl %eax, %ebx
    jne step2
    addl $1, type
    jmp step2

step2:
    cmpl %ebx, %ecx
    jne step3
    addl $1, type
    jmp step3

step3:
    cmpl %ecx, %eax
    jne exit
    addl $1, type
    jmp exit

exit:
    movl $1, %eax
    movl $0, %ebx
    int $0x80
  

LAB 4 - Array Initialization and Natural Numbers

.section .data
n:
    .int 6

.section .bss
.lcomm naturalnums 24

.section .text
.globl _start

_start:
    movl $1, %eax
    movl n, %ebx
    movl $0, %esi
    jmp loop

loop:
    movl %eax, naturalnums(, %esi, 4)
    addl $1, %eax
    addl $1, %esi
    cmpl n, %esi
    jl loop
    jmp exit

exit:
    movl $1, %eax
    movl $0, %ebx
    int $0x80
  

LAB 4 - Array Insertion

.section .data
arr:
    .int 10, 20, 30, 40, 50
pos:
    .int 3
val:
    .int 60

.section .bss
.lcomm out 24

.section .text
.globl _start

_start:
    movl $0, %esi
    movl $0, %edi
    movl pos, %ecx
    movl val, %ebx
    movl $0, %eax
    jmp loop1

loop1:
    movl arr(, %esi, 4), %eax
    cmpl %ecx, %esi
    je insert
    jmp loop2

loop2:
    movl %eax, out(, %edi, 4)
    addl $1, %esi
    addl $1, %edi
    cmpl $5, %esi
    jl loop1
    jmp exit

insert:
    movl %ebx, out(, %edi, 4)
    addl $1, %edi
    jmp loop2

exit:
    movl $1, %eax
    movl $0, %ebx
    int $0x80
  

LAB 4 - Array Removal

.section .data
arr:
    .int 10, 20, 30, 40, 50
val:
    .int 30

.section .bss
.lcomm out 16

.section .text
.globl _start

_start:
    movl $0, %esi
    movl $0, %edi
    movl val, %ebx
    movl $0, %eax
    jmp loop1

loop1:
    movl arr(, %esi, 4), %eax
    cmpl %eax, %ebx
    je remove
    jmp loop2

loop2:
    movl %eax, out(, %edi, 4)
    addl $1, %esi
    addl $1, %edi
    cmpl $5, %esi
    jl loop1
    jmp exit

remove:
    addl $1, %esi
    jmp loop1

exit:
    movl $1, %eax
    movl $0, %ebx
    int $0x80
  

LAB 4 - Array Filtering (Even Numbers)

.section .data
arr:
    .int 2, 3, 7, 8, 6

.section .bss
.lcomm out 20

.section .text
.globl _start

_start:
    movl $0, %esi
    movl $0, %edi
    movl $2, %ebx
    movl $0, %edx
    jmp loop1

loop1:
    movl arr(, %esi, 4), %eax
    divl %ebx
    cmpl $0, %edx
    je insert
    jmp loop2

loop2:
    addl $1, %esi
    movl $0, %edx
    cmpl $5, %esi
    jl loop1
    jmp exit

insert:
    movl arr(, %esi, 4), %eax
    movl %eax, out(, %edi, 4)
    addl $1, %edi
    jmp loop2

exit:
    movl $1, %eax
    movl $0, %ebx
    int $0x80
  

LAB 4 - Fibonacci Series Generation

.section .data
n:
    .int 7

.section .bss
.lcomm out 28

.section .text
.globl _start

_start:
    movl $0, %eax
    movl $1, %ebx
    movl n, %ecx
    movl $0, %esi
    jmp loop

loop:
    movl %eax, out(, %esi, 4)
    movl %ebx, %eax
    addl out(, %esi, 4), %ebx
    addl $1, %esi
    cmpl %ecx, %esi
    jl loop
    jmp exit

exit:
    movl $1, %eax
    movl $0, %ebx
    int $0x80
  

LAB 5 - String Copy

.section .data
str:
    .asciz "hello world"

.section .bss
.lcomm out 12

.section .text
.globl _start

_start:
    movl $0, %esi
    movb $0, %al
    jmp loop

loop:
    movb str(, %esi, 1), %al
    movb %al, out(, %esi, 1)
    addl $1, %esi
    cmpl $12, %esi
    jl loop
    jmp exit

exit:
    movl $1, %eax
    movl $0, %ebx
    int $0x80
  

LAB 5 - Vowel Counting

.section .data
str:
    .asciz "Hi Welcome"
vowels:
    .ascii "AEIOUAeiou"
count:
    .int 0

.section .bss

.section .text
.globl _start

_start:
    movl $0, %esi
    movl $0, %edi
    movl $0, %ebx
    movl $0, %eax
    jmp loop

loop:
    movb str(, %esi, 1), %al
    addl $1, %esi
    cmpl $11, %esi
    jl checkvowel
    jmp exit

checkvowel:
    movb vowels(, %edi, 1), %bl
    cmpb %al, %bl
    je vowelfound
    addl $1, %edi
    cmpl $10, %edi
    jl checkvowel
    movl $0, %edi
    jmp loop

vowelfound:
    addl $1, count
    jmp loop

exit:
    movl $1, %eax
    movl $0, %ebx
    int $0x80
  

LAB 5 - Remove First Character of Each Word

.section .data
str:
    .asciz "HI Welcome All"
space:
    .ascii " "

.section .bss
.lcomm out 15

.section .text
.globl _start

_start:
    movl $0, %esi
    movl $0, %edi
    movl $0, %eax
    movb space, %bl
    jmp loop

loop:
    movb str(, %esi, 1), %al
    cmpb %al, %bl
    je ommit
    cmpl $0, %esi
    je ommit1
    movb %al, out(, %edi, 1)
    addl $1, %esi
    addl $1, %edi
    cmpl $15, %esi
    jl loop
    jmp exit

ommit:
    movb %al, out(, %edi, 1)
    addl $2, %esi
    addl $1, %edi
    cmpl $15, %esi
    jl loop
    jmp exit

ommit1:
    addl $1, %esi
    cmpl $15, %esi
    jl loop
    jmp exit

exit:
    movl $1, %eax
    movl $0, %ebx
    int $0x80
  

LAB 5 - String Comparison

.section .data
str1:
    .asciz "Hello World"
str2:
    .asciz "Hello World"
isequal:
    .int 1

.section .bss

.section .text
.globl _start

_start:
    movl $0, %esi
    movl $0, %eax
    jmp loop

loop:
    movb str1(, %esi, 1), %al
    cmpb str2(, %esi, 1), %al
    jne notequal
    addl $1, %esi
    cmpl $12, %esi
    jl loop
    jmp exit

notequal:
    movl $0, isequal
    jmp exit

exit:
    movl $1, %eax
    movl $0, %ebx
    int $0x80
  

LAB 6 - Array Searching

.section .data
arr:
    .int 8, 5, 8, 4, 5
key:
    .int 8

.section .bss
.lcomm out 4

.section .text
.globl _start

_start:
    movl $0, %esi
    movl $-1, out
    movl $0, %eax
    jmp loop

loop:
    movl arr(, %esi, 4), %eax
    cmpl key, %eax
    je found
    addl $1, %esi
    cmpl $5, %esi
    jl loop
    jmp exit

found:
    movl %esi, out
    jmp exit

exit:
    movl $1, %eax
    movl $0, %ebx
    int $0x80
  

LAB 6 - Special Character Searching

.section .data
str:
    .asciz "22ETCS002112@msruas.ac.in"
specialchar:
    .ascii "!@#$%^&*()'<>:|\"'`;?[]{},"

.section .bss
.lcomm out 26

.section .text
.globl _start

_start:
    movl $0, %esi
    movl $0, %edi
    movl $0, %ebx
    movl $0, %eax
    jmp loop

loop:
    movb str(, %esi, 1), %al
    addl $1, %esi
    cmpl $26, %esi
    jl checkchar
    jmp exit

checkchar:
    cmpb specialchar(, %edi, 1), %al
    je foundchar
    addl $1, %edi
    cmpl $26, %edi
    jl checkchar
    movl $0, %edi
    jmp loop

foundchar:
    movb %al, out(, %ebx, 1)
    addl $1, %ebx
    movl $0, %edx
    jmp loop

exit:
    movl $1, %eax
    movl $0, %ebx
    int $0x80
  

LAB 6 - Count Occurrences of '@'

.section .data
str:
    .asciz "Hi @ Welcome @ to CSE @ Dep@T "
key:
    .ascii "@"

.section .bss
.lcomm count 4

.section .text
.globl _start

_start:
    movl $0, %esi
    movl $0, %eax
    jmp loop

loop:
    movb str(, %esi, 1), %al
    cmpb key, %al
    je found
    addl $1, %esi
    cmpl $30, %esi
    jl loop
    jmp exit

found:
    addl $1, count
    addl $1, %esi
    cmpl $30, %esi
    jl loop
    jmp exit

exit:
    movl $1, %eax
    movl $0, %ebx
    int $0x80
  

LAB 7 - Second Smallest Number

.section .data
arr:
    .int 4, 6, 9, 7, 9, 6
arrlenght:
    .int 6

.section .bss
.lcomm small 4
.lcomm small2 4

.section .text
.globl _start

_start:
    movl $0, %esi
    movl $0, %edi
    movl $0, %eax
    jmp findsmallest

findsmallest:
    movl arr(, %esi, 4), %eax
    cmpl arr(, %edi, 4), %eax
    jl newsmallest
    jmp loop1

newsmallest:
    movl %esi, %edi
    jmp loop1

loop1:
    addl $1, %esi
    cmpl arrlenght, %esi
    jl findsmallest
    movl arr(, %edi, 4), %eax
    movl %eax, small
    movl $0, %esi
    movl $0, %edi
    movl $0, %eax
    jmp find2smallest

find2smallest:
    movl arr(, %esi, 4), %eax
    cmpl arr(, %edi, 4), %eax
    jl new2smallest
    cmpl small, %edi
    je checkifnotsmallest
    jmp loop2

checkifnotsmallest:
    movl arr(, %edi, 4), %eax
    cmpl small, %eax
    jne loop2
    addl $1, %edi
    jmp loop2

new2smallest:
    movl arr(, %esi, 4), %eax
    cmpl small, %eax
    je loop2
    movl %esi, %edi
    jmp loop2

loop2:
    addl $1, %esi
    cmpl arrlenght, %esi
    jl find2smallest
    movl arr(, %edi, 4), %eax
    movl %eax, small2
    jmp exit

exit:
    movl $1, %eax
    movl $0, %ebx
    int $0x80
  

LAB 7 - Array Sorting (Ascending)

.section .data
arr:
    .int 1,8,4,5,9,6,3
    .equ size, 7

.section .bss

.section .text
.globl _start
_start:
    movl $0, %esi     # index to check all elements
    movl $0, %edx     # portion index sorted
    movl $0, %eax     # holds current element
    movl $0, %ebx     # index for smallest unsorted element
    jmp LoopA

LoopA:  # checks if current element is smallest
    movl arr(, %esi, 4), %eax
    cmpl arr(, %ebx, 4), %eax
    jl issmallest
    jmp LoopB

LoopB:  # increments index
    addl $1, %esi
    cmpl $size, %esi
    jl LoopA
    jmp resetnloop

resetnloop:  # swaps if element smaller than current found
    movl arr(, %edx, 4), %eax
    xorl %eax, arr(, %ebx, 4)
    xorl arr(, %ebx, 4), %eax
    xorl %eax, arr(, %ebx, 4)

    movl %eax, arr(, %edx, 4)
    movl %edx, %esi
    addl $1, %edx
    cmpl $size, %edx
    jl LoopA
    jmp exit

issmallest:
    movl %esi, %ebx
    jmp LoopB

exit:
    int $0x80