Assembly
- It is a low-level programming language, strongly corresponding with the architecture's machine code instruction
( Assembly language usually has one statement per machine instruction )
- Assembly code is converted into executable machine code (binary) bya utility program called assembler
- Different machines have different assembly language but most assembly languages can be used universally with any OS
- Compiling is much more complicated than assembling so low-level assembly language is more conveniently portable across devices than high-level languages (similarly disassembler is much more easier than a decompiler)
- Assembly language can help manipulate hardware directly, issues relating to performance, and also provide access to special instructions for processors
- Ex of uses: coding device drivers, real-time systems, low-level embedded systems, boot codes, test system hardwares, operating system kernels, reverse engineering
- C allows to embed assembly language directly in the source code -help construct abstractions using different assembly on each hardware platform
- Assembly is the gateway to speed and efficiency and overall less overhead (compared to high-level languages)
( Assembly language usually has one statement per machine instruction )
- Assembly code is converted into executable machine code (binary) bya utility program called assembler
- Different machines have different assembly language but most assembly languages can be used universally with any OS
- Compiling is much more complicated than assembling so low-level assembly language is more conveniently portable across devices than high-level languages (similarly disassembler is much more easier than a decompiler)
- Assembly language can help manipulate hardware directly, issues relating to performance, and also provide access to special instructions for processors
- Ex of uses: coding device drivers, real-time systems, low-level embedded systems, boot codes, test system hardwares, operating system kernels, reverse engineering
- C allows to embed assembly language directly in the source code -help construct abstractions using different assembly on each hardware platform
- Assembly is the gateway to speed and efficiency and overall less overhead (compared to high-level languages)
Your browser does not support viewing this document. Click here to download the document.
.data # kind of like global variables, used for declaring initialized data or constants
string1:
.asciiz "Enter a number: "
num1:
.word 42 # Integers are referred to as words
arr1:
.word 1, 2, 3, 4, 5 # Array of words
arr2:
.byte 'a', 'b' # Array of bytes and each char is 1 byte
buffer:
.space 60 # Used to allocate space in RAM
flt:
.float 3.14 # Floats are 4 byte each
dbl:
.double 7.0 # Doubles are 8 byte each
hlf:
.half 53 # Stores values worth of 2 bytes each while word store 4 byte
----------------------------------------------------------------------------------------------------------------------
.text # This indicates that the section contains the actual program
.include "somefile.asm" # Takes code from somefile.asm and puts it here
.globl _main # Makes it accessible to other files and also indicates the starting function
_main:
. . .
-----------------------------------------------------------------------------------------------------------------------
la $a0, string1 #$a0 contains arguments and string1 contains the string that will printed out
li $v0, 4 # 4 is the code used for printing out strings
syscall
--------------------------------------------------------------------------------------------------------------------------------------------------------
la = load address, li = load immediate, lb = load byte . . .
sw = store word
move $v0, $s2 # $v0 = $s2
string1:
.asciiz "Enter a number: "
num1:
.word 42 # Integers are referred to as words
arr1:
.word 1, 2, 3, 4, 5 # Array of words
arr2:
.byte 'a', 'b' # Array of bytes and each char is 1 byte
buffer:
.space 60 # Used to allocate space in RAM
flt:
.float 3.14 # Floats are 4 byte each
dbl:
.double 7.0 # Doubles are 8 byte each
hlf:
.half 53 # Stores values worth of 2 bytes each while word store 4 byte
----------------------------------------------------------------------------------------------------------------------
.text # This indicates that the section contains the actual program
.include "somefile.asm" # Takes code from somefile.asm and puts it here
.globl _main # Makes it accessible to other files and also indicates the starting function
_main:
. . .
-----------------------------------------------------------------------------------------------------------------------
la $a0, string1 #$a0 contains arguments and string1 contains the string that will printed out
li $v0, 4 # 4 is the code used for printing out strings
syscall
--------------------------------------------------------------------------------------------------------------------------------------------------------
la = load address, li = load immediate, lb = load byte . . .
sw = store word
move $v0, $s2 # $v0 = $s2
--------------------------------------------------------------------------------------------------------------------------------------------------------
.macro println() # Macros help prevent repeating same parts of code again & again
la $a0, newline
li $v0, 4
syscall
.end_macro
println() # The assembler will copy above block of code here, so this whole thing prints 2 newlines
.macro imme(%a, %b) # Macros can also include parameters
add $t0, %a, %b # Parameters can include immediate, global var
.end_macro
imme(3,5) # Now, $t0 contains 8
------------------------------------------------------------------------------------------------------------------------------------------------------------
lw $t1, 4($s0) # Copy a word value from an address stored in $s0 w/ an offset of 4 bytes (addr + 4)
lb $t2, 0($s0) # Copy a byte value from source (use 'lh' for load halfword)
sw $t0, 8($s0)
------------------------------------------------------------------------------------------------------------------------------------------------------------
IF Statements
beq $t0, $t1, if_block # beq can be varying replaced with beqz, bneq, bneqz, ...
else_block:
. . .
j end_block
if_block:
. . .
end_block:
. . .
LOOPS
li $t0, 0
while:
bgt $t0, 10, end_while
addi $t0, $t0, 1
. . . # do stuff inside loop
j while
end_while:
. . . # finish
FUNCTIONS
sum:
add $v0, $a0, $a1 # This function takes in 2 args and sums it
jr $ra # Tells it to go back to return address
main:
li $a0, 1
li $a1, 2 # Args are stored in the $a* registers
jal sum # Calling function sum and telling it to come back here after finishing execution
# Return value of sum is in $v0 register
-------------------------------------------------------------------------------------------------------------------------------------------------------
RECURSION
ex: factorial
fact:
addi $sp, $sp, -8 # Always allocate space in stack for recursion
sw $s0, ($sp) # Store register that holds the current number
sw $ra, 4($sp) # Store previous Program Counter
li $v0, 1 # Initial return value / base case
beq $a0, 0, fact_done # Finish if parameter is 0
move $s0, $a0
sub $a0, $a0, 1
jal fact
mul $v0, $s0, $v0
fact_done:
lw $s0, ($sp)
lw $ra, ($sp)
addi $sp, $sp, 8 # Restore Program Counter
jr $ra
ex: Power function # $a1 = exponent, $a0 = base
recursivePowFunc:
bne $a1, $zero, recursion
li $v0, 1
jr $ra
recursion:
addi $sp, -4
sw $ra, 0($sp)
addi $a1, $a1, -1
jal recursivePowFunc
mul $v0, $v0, $a0
lw $ra, 0($sp)
addi $sp, 4
jr $ra
.macro println() # Macros help prevent repeating same parts of code again & again
la $a0, newline
li $v0, 4
syscall
.end_macro
println() # The assembler will copy above block of code here, so this whole thing prints 2 newlines
.macro imme(%a, %b) # Macros can also include parameters
add $t0, %a, %b # Parameters can include immediate, global var
.end_macro
imme(3,5) # Now, $t0 contains 8
------------------------------------------------------------------------------------------------------------------------------------------------------------
lw $t1, 4($s0) # Copy a word value from an address stored in $s0 w/ an offset of 4 bytes (addr + 4)
lb $t2, 0($s0) # Copy a byte value from source (use 'lh' for load halfword)
sw $t0, 8($s0)
------------------------------------------------------------------------------------------------------------------------------------------------------------
IF Statements
beq $t0, $t1, if_block # beq can be varying replaced with beqz, bneq, bneqz, ...
else_block:
. . .
j end_block
if_block:
. . .
end_block:
. . .
LOOPS
li $t0, 0
while:
bgt $t0, 10, end_while
addi $t0, $t0, 1
. . . # do stuff inside loop
j while
end_while:
. . . # finish
FUNCTIONS
sum:
add $v0, $a0, $a1 # This function takes in 2 args and sums it
jr $ra # Tells it to go back to return address
main:
li $a0, 1
li $a1, 2 # Args are stored in the $a* registers
jal sum # Calling function sum and telling it to come back here after finishing execution
# Return value of sum is in $v0 register
-------------------------------------------------------------------------------------------------------------------------------------------------------
RECURSION
ex: factorial
fact:
addi $sp, $sp, -8 # Always allocate space in stack for recursion
sw $s0, ($sp) # Store register that holds the current number
sw $ra, 4($sp) # Store previous Program Counter
li $v0, 1 # Initial return value / base case
beq $a0, 0, fact_done # Finish if parameter is 0
move $s0, $a0
sub $a0, $a0, 1
jal fact
mul $v0, $s0, $v0
fact_done:
lw $s0, ($sp)
lw $ra, ($sp)
addi $sp, $sp, 8 # Restore Program Counter
jr $ra
ex: Power function # $a1 = exponent, $a0 = base
recursivePowFunc:
bne $a1, $zero, recursion
li $v0, 1
jr $ra
recursion:
addi $sp, -4
sw $ra, 0($sp)
addi $a1, $a1, -1
jal recursivePowFunc
mul $v0, $v0, $a0
lw $ra, 0($sp)
addi $sp, 4
jr $ra
Mini-Projects in Assembly
I worked on small projects to demonstrate my understanding of the assembly language. I have briefly explained the implementation process and displayed demonstration for each assembly program.
List-Sum-Average-Calculator
This is a simple program when given a list of numbers, it calculates the total sum and its average. The program uses loops to iteratively prompt the user to provide an integer as part of the list. As each number is entered, the total sum and the amount of numbers entered are stored and kept track by temporary registers. When the user enters 0 to indicate the end of list, the total numbers entered, the sum and the average, calculated by dividing the sum by the total numbers entered are printed. My code can be viewed here.
List-Sum-Average-Calculator
This is a simple program when given a list of numbers, it calculates the total sum and its average. The program uses loops to iteratively prompt the user to provide an integer as part of the list. As each number is entered, the total sum and the amount of numbers entered are stored and kept track by temporary registers. When the user enters 0 to indicate the end of list, the total numbers entered, the sum and the average, calculated by dividing the sum by the total numbers entered are printed. My code can be viewed here.
Mini-Calculator
The mini-calculator program, given 2 numbers will perform the specified operation and print the result. My code can be viewed here.
The mini-calculator program, given 2 numbers will perform the specified operation and print the result. My code can be viewed here.
Day-Calculator
This project is written in x86 assembly. The program prompts user to input a date and uses it to calculate the day number in that year from 1 to 365 (or 366 in leap year). A demonstration is shown below. My code can be viewed here.
This project is written in x86 assembly. The program prompts user to input a date and uses it to calculate the day number in that year from 1 to 365 (or 366 in leap year). A demonstration is shown below. My code can be viewed here.
Right-Triangle-Checker
The right-triangle calculator determines whether a triangle has a right angle. Given the lengths of the 3 sides, the program utilizes Pythagoras' theorem (a^2 + b^2 = c^2) to determine if the side lengths form a right-triangle. My code can be viewed here.
The right-triangle calculator determines whether a triangle has a right angle. Given the lengths of the 3 sides, the program utilizes Pythagoras' theorem (a^2 + b^2 = c^2) to determine if the side lengths form a right-triangle. My code can be viewed here.
Sorted Binary Search
This search program constructs and stores an array in allocated memory. The array is then sorted using bubble sort algorithm. When provided an element, the program utilizes binary search to determine the index if the element exists else returns ‘element was not found’. My code can be viewed here.
This search program constructs and stores an array in allocated memory. The array is then sorted using bubble sort algorithm. When provided an element, the program utilizes binary search to determine the index if the element exists else returns ‘element was not found’. My code can be viewed here.
32-Bit-Adder
This 32-Bit Adder program is capable of handling values over a range from (-1* (2^32 - 1) = -4294967295) up to (2^32=4294967296). The user-entered values are converted into a 32-bit binary value and 8-bit hexadecimal value and stored in an array. The addition is performed through a loop bit-by-bit and the results are printed to the output. My code can be viewed here.
This 32-Bit Adder program is capable of handling values over a range from (-1* (2^32 - 1) = -4294967295) up to (2^32=4294967296). The user-entered values are converted into a 32-bit binary value and 8-bit hexadecimal value and stored in an array. The addition is performed through a loop bit-by-bit and the results are printed to the output. My code can be viewed here.
Coconut Escape
Using the Bitmap display address given in the Mars simulator, I implemented a variation of the Doodle Jump game in the assembly language. The Doodle must evade the deadly coconuts and navigate upwards. The Keyboard and Display MMIO simulator collects input from the user on if the start key - 's' , left key - 'j' or the right key 'k' was entered and move/repaint the Doodle correspondingly. The code for this project can be viewed here.
Using the Bitmap display address given in the Mars simulator, I implemented a variation of the Doodle Jump game in the assembly language. The Doodle must evade the deadly coconuts and navigate upwards. The Keyboard and Display MMIO simulator collects input from the user on if the start key - 's' , left key - 'j' or the right key 'k' was entered and move/repaint the Doodle correspondingly. The code for this project can be viewed here.