Assembly Language Courseware:
- What is assembly language?
- Why learn assembly?
- Brief history of assembly languages
2. Setting Up the Environment
- Installing NASM
- Setting up a text editor
- Basic command-line usage for assembling and linking
- Structure of a NASM program
- Comments, labels, and directives
- Basic instructions (MOV, ADD, SUB)
- Binary and hexadecimal systems
- Signed and unsigned integers
- Little-endian vs Big-endian
- CPU registers (general-purpose, segment, and special registers)
- Memory addressing modes
- Stack operations
6. Basic Arithmetic Operations
- Addition and subtraction
- Multiplication and division
- Bitwise operations
7. Adding Two Numbers: Step-by-Step
- Program structure
- Declaring variables
- Moving data into registers
- Performing addition
- Storing the result
- Displaying the result
8. Sample Program: Adding Two Numbers
- Walkthrough of a complete NASM program
- Explanation of each line of code
- Assembling and running the program
9. Debugging and Troubleshooting
- Common errors and how to fix them
- Using a debugger (e.g., GDB) with assembly code
- Modify the program to add three numbers
- Implement subtraction of two numbers
- Create a program that adds user input numbers
11. Advanced Topics (Optional)
- Floating-point arithmetic
- SIMD instructions for parallel addition
- Optimizing addition operations
12. Resources for Further Learning
- Recommended books and online tutorials
- Assembly language forums and communities
admin@ironman:~$ nasm -felf64 factorial.asm && gcc -std=c99 factorial.o factorial.c && ./a.out
admin@ironman:~$ nasm -felf64 maxofthree.asm && gcc maxofthree.o max.c && ./a.out
Adding Two Numbers in NASM
section .data
prompt1 db "Enter the first number: ", 0
prompt2 db "Enter the second number: ", 0
fmt_input db "%ld", 0
fmt_output db "The sum is: %ld", 10, 0
section .bss
num1 resq 1
num2 resq 1
section .text
global main
extern printf
extern scanf
main:
; Prologue
push rbp
mov rbp, rsp
; Prompt for first number
mov rdi, prompt1
xor eax, eax
call printf
; Read first number
mov rdi, fmt_input
mov rsi, num1
xor eax, eax
call scanf
; Prompt for second number
mov rdi, prompt2
xor eax, eax
call printf
; Read second number
mov rdi, fmt_input
mov rsi, num2
xor eax, eax
call scanf
; Perform addition
mov rax, [num1]
add rax, [num2]
; Print result
mov rdi, fmt_output
mov rsi, rax
xor eax, eax
call printf
; Epilogue
mov rsp, rbp
pop rbp
; Exit program
xor eax, eax
ret
default rel
section .data
prompt1 db "Enter the first number: ", 0
prompt2 db "Enter the second number: ", 0
fmt_input db "%ld", 0
fmt_output db "The maximum is: %ld", 10, 0
section .bss
num1 resq 1
num2 resq 1
section .text
global main
extern printf
extern scanf
main:
push rbp
mov rbp, rsp
; Prompt and read first number
lea rdi, [prompt1]
xor eax, eax
call printf
lea rdi, [fmt_input]
lea rsi, [num1]
xor eax, eax
call scanf
; Prompt and read second number
lea rdi, [prompt2]
xor eax, eax
call printf
lea rdi, [fmt_input]
lea rsi, [num2]
xor eax, eax
call scanf
; Compare numbers and find max
mov rax, [num1]
mov rbx, [num2]
cmp rax, rbx
jge print_result ; Jump if num1 >= num2
mov rax, rbx ; If num2 > num1, move num2 to rax
print_result:
lea rdi, [fmt_output]
mov rsi, rax
xor eax, eax
call printf
mov rsp, rbp
pop rbp
xor eax, eax
ret
Save the code to a file, e.g., max_numbers.asm
nasm -f elf64 max_numbers.asm
gcc max_numbers.o -o max_numbers -no-pie
https://www.nasm.us/xdoc/2.15.05/html/nasmdoc0.html