Assembly

Page content

URL

Notes to Assembly

https://www4.cs.fau.de/Lehre/WS20/V_BS/Uebungen/seminar-asm.pdf https://www.tutorialspoint.com/assembly_programming/assembly_basic_syntax.htm https://www4.cs.fau.de/Lehre/WS09/V_BS/Uebungen/oostubs/assembler.shtml

Sections

  • data
  • bss
  • text

Data

for declaring initialized data or constants

BSS

Declaring Variables

Text

Contains the actual Code

CPU Registers

RAX:  Akkumulator
RBX:  Base Register
RCX:  Counter
RDX:  Data Register
RSI:  Source Index
RDI:  Destination Index

RBP:  Base Pointer    # Don't touch
RSP:  Stack Pointer   # Don't touch

RIP:  Instruction Pointer -> contains the address of the command to be loaded next
RFLAGS: Status Register   -> contains PSW (Processor Status Word)

Move


mov rax, 43               -> Konstante to Register
mov rax, rcx              -> Register to Register
mov rax, [0xb8000]        -> Speicher to Register
mov [0xb8000 + 4], rax    -> Register to Speicher

Stack


pop rax                   -> Stack -> Register
mov rax, [rsp]
add rsp, 8

push rax                  -> Register -> Stack
sub rsp, 8
mov [rsp], rax

Check Args

section .data
	SYS_WRITE	equ 1
	STD_IN		equ 1
	SYS_EXIT	equ 60
	EXIT_CODE	equ 0

	NEW_LINE	db 0xa
	WRONG_ARGC	db "Must be two command line argument", 0xa

section .text
	global	_start

_start:
	;; rcx - argc
	pop	rcx

	;;
	;; Check argc
	;;
	cmp	rcx, 3
	jne	argcError

;;
;; Print argc error
;;
argcError:
	;; sys_write syscall
	mov	rax, 1
	;; file descritor, standard output
	mov	rdi, 1
	;; message address
	mov	rsi, WRONG_ARGC
	;; length of message
	mov	rdx, 34
	;; call write syscall
	syscall
	;; exit from program
	jmp	exit

;;
;; Exit from program
;;
exit:
	;; syscall number
	mov	rax, SYS_EXIT
	;; exit code
	mov	rdi, EXIT_CODE
	;; call sys_exit
	syscall

sha256: db0fb651963c38171b0fe370d3867e831b186529cc02d2c1cb530b13127a2705