Assembly Programming

Programming in Intel x86-64 Assembly

#Intel x86-64 Assembly#Bachelor's Studies#University Studies

In the course "Systemnahe Programmierung" (translates to "System-Level Programming"), we spent some time working with assembly language. Much to our dismay, this didn't just involve reading and understanding assembly code — we also had to write it ourselves. This meant creating small programs, compiling them, and actually running them.

Example Program

To understand how function calls work in assembly, we had to write a program that reads a string using fgets, converts it to uppercase, and prints it to the console.

extern puts extern fgets extern stdin extern toupper ; RDI, RSI, RDX, RCX, R8 SECTION .data msg: db 'Hello ',13,10,'world!',13,10,0 ; Zero is Null terminator input: times 21 db 0 ; SECTION .text global main main: push rbp ; Push stack ;String einlesen mov rdi, input mov rsi, 20 mov rdx, [stdin] call fgets ;loop mov r15, -1 ; r15 nehmen weil r9 an irgendeiner stelle überschrieben wird lop: inc r15 ; Aktuellen Char auslesen mov r8, 0 ;lea rax, [input+r15] ; load adress of current char to rax mov r8b, [input+r15] ; buffer current character ;Newlines (13 und 10) durch beep (7) ersetzen cmp r8b, 13 jne weiter mov r8b, 7 mov [input+r15+1], byte 7 weiter: ; In Großbuchstaben konvertieren mov rdi,r8 mov rax,0 call toupper ;Zurück in String schreiben mov [input+r15], al ;loop cmp al, 0 jne lop ;String ausgeben mov rdi, input mov rax,0 call puts pop rbp ; Pop stack mov rax,0 ; Exit code 0 ret ; Return

This can be compiled using the following short Makefile:

CFLAGS = -Wall -Wextra -v -no-pie -o all: comp comp: nasm -f elf64 -g -F dwarf -o glibber.o glibber.S clang $(CFLAGS) glibber glibber.o clean: rm -f glibber rm -f glibber.o

Comments

Feel free to leave your opinion or questions in the comment section below.