Programming Fundamentals/Variables/Assembly
name_x86.asm[edit | edit source]
;This program displays "Hello <name>!" based on user input.
;
;References:
; https://www.tutorialspoint.com/assembly_programming/
; https://www.tutorialspoint.com/compile_assembly_online.php
; https://www.tutorialspoint.com/assembly_programming/assembly_system_calls.htm
global _start
_start: section .text
prompt_out: mov eax, sys_write ;eax = sys_write
mov ebx, stdout ;ebx = stdout
mov ecx, prompt ;ecx = prompt address
mov edx, prompt_len ;edx = prompt length
int sys_call ;call system
name_in: mov eax, sys_read ;eax = sys_read
mov ebx, stdin ;ebx = stdin
mov ecx, name ;ecx = name address
mov edx, name_maxlen ;edx = name maximum length
int sys_call ;call system
sub eax, 1 ;eax = input name length - newline
mov [name_len], eax ;save name length
hello_out: mov eax, sys_write ;eax = sys_write
mov ebx, stdout ;ebx = stdout
mov ecx, hello ;ecx = prompt address
mov edx, hello_len ;edx = prompt length
int sys_call ;call system
name_out: mov eax, sys_write ;eax = sys_write
mov ebx, stdout ;ebx = stdout
mov ecx, name ;ecx = prompt address
mov edx, [name_len] ;edx = name length
int sys_call ;call system
exclam_out: mov eax, sys_write ;eax = sys_write
mov ebx, stdout ;ebx = stdout
mov ecx, exclam ;ecx = exclamation address
mov edx, exclam_len ;edx = exclamation length
int sys_call ;call system
exit: mov eax, sys_exit ;eax = sys_exit
mov ebx, 0 ;ebx = return code (0)
int sys_call ;call system
section .data
sys_exit equ 1
sys_read equ 3
sys_write equ 4
sys_call equ 0x80
stdin equ 0
stdout equ 1
linefeed equ 0x0a
newline db linefeed
newline_len equ $ - newline
prompt db 'Enter your name:', linefeed
prompt_len equ $ - prompt
name_len dd 4
hello db 'Hello '
hello_len equ $ - hello
exclam db '!'
exclam_len equ $ - exclam
section .bss
name resb 255
name_maxlen equ $ - name
name_x64.asm[edit | edit source]
;This program displays 'Hello world!"
;
;References:
; http://cs.lmu.edu/~ray/notes/nasmtutorial/
; http://blog.rchapman.org/posts/Linux_System_Call_Table_for_x86_64/
global _start
_start: section .text
prompt_out: mov rax, sys_write ;rax = sys_write
mov rdi, stdout ;rdi = stdout
mov rsi, prompt ;rsi = prompt address
mov rdx, prompt_len ;rdx = prompt length
syscall ;call system
name_in: mov rax, sys_read ;rax = sys_read
mov rdi, stdin ;rdi = stdin
mov rsi, name ;rsi = name address
mov rdx, name_maxlen ;rdx = name maximum length
syscall ;call system
mov [name_len], rax ;save name length
hello_out: mov rax, sys_write ;rax = sys_write
mov rdi, stdout ;rdi = stdout
mov rsi, hello ;rsi = hello address
mov rdx, hello_len ;rdx = hello length
syscall ;call system
name_out: mov rax, sys_write ;rax = sys_write
mov rdi, stdout ;rdi = stdout
mov rsi, name ;rsi = name address
mov rdx, [name_len] ;rdx = name length
syscall ;call system
exclam_out: mov rax, sys_write ;eax = sys_write
mov rdi, stdout ;rdi = stdout
mov rsi, exclam ;rsi = exclam address
mov rdx, exclam_len ;rdx = name length
syscall ;call system
exit: mov rax, sys_exit ;rax = sys_exit
mov rdi, 0 ;rdi = return code (0)
syscall ;call system
section .data
sys_read equ 0
sys_write equ 1
sys_exit equ 60
stdin equ 0
stdout equ 1
linefeed equ 0x0a
prompt db "Enter your name:", linefeed
prompt_len equ $ - prompt
name_len dq 0
hello db 'Hello '
hello_len equ $ - hello
exclam db '!'
exclam_len equ $ - exclam
section .bss
name resb 255
name_maxlen equ $ - name
Try It[edit | edit source]
Copy and paste the code above into one of the following free online development environments or use your own Assembly compiler / interpreter / IDE.
Assembly language (also called assembler language), often abbreviated asm, is a set of mnemonic languages with a 1 to 1 logical mapping of instructions to the machine code of various architectures. Assembly is usually used when the programming task is small and local, as it has very little modularity and is platform-dependent, unlike higher-level languages. Other uses of assembly are in program debugging (in which machine instructions can be executed one at a time), and in reverse-engineering compiled programs by disassembly (in which there is no higher-level code to associate with).
Assembly language can be known as any low-level programming language in which there is a very strong correspondence between the program's statements and the architecture's machine code instructions.[1]
Architecture[edit | edit source]
The architecture is the most important thing to know when programming in Assembly Language. The architecture in question might be the specific hardware that the application is designed to run on, or a virtual machine. A virtual machine is an example of abstract hardware, and usually also has its own version of machine code. The architecture dictates the internal representations used by data types, instructions understood by the CPU, and available resources. Since Assembly Language is a 1 to 1 logical mapping to machine code, the steps taken to implement an algorithm are frequently much smaller and more numerous than those in higher level languages. A typical hardware architecture contains a CPU, registers, and memory.
Common Architectures:
Hardware[edit | edit source]
The CPU (Central Processing Unit) acts as a kind of brain for the computer system. It usually contains a cache and registers. The cache is typically used to store segments of program code, while registers are used for immediate data access. The CPU successively executes instructions from the cache until it encounters a branch or interrupt.
Registers are a part of the CPU, and are the fastest memory available. The number, size, and use of registers available depends upon the architecture in question. A register is usually considered as an integer, but can be conceptualized as anything from a character to a pointer.
Memory access is a trade-off between speed and size. Accessing memory is much faster than disk I/O, but much slower than accessing a register. Memory contains both program code and variable storage, and has the advantage of being able to store large or complex objects.
Common Instructions[edit | edit source]
An instruction in Assembly Language usually declares both where this data is located, and the format of the expected data. Instructions can be categorized as mathematical, logical, flow control, or memory operations. Common mathematical operations include adding, subtracting, multiplying, dividing, and shifting, while common logical operations are the bitwise operations AND, OR, XOR, and NOT. Flow control operations are most often branches or comparisons, and memory is often loaded, stored, or moved. Each architecture comes with an instruction set which is in machine code (1's and 0's) for the machine but presented to the human programmer in more readable forms. See MIPS architecture - a fine example.
Free Online IDEs[edit | edit source]
Famous free Online IDEs for assembly includes:
- IDEone: Support Assembler 32-bit gcc 8.3 and nasm 2.14 and Assembler 64-bit nasm 2.14
- TutorialsPoint
See also[edit | edit source]
- Operating Systems/x86
- Topic:Electronic engineering
- GitHub: PC Assembly Language Book
- LMU.edu: NASM Tutorial
- TutorialsPoint: Assembly Programming Tutorial
- Virginia.edu: Guide to x86 Assembly
Reference[edit | edit source]
Go to the School of Computer Science |