A few weeks ago i bought myself a Surface Pro 2 in an efford to create a Nintendo Toast (or as some ppl might know it the Nintendo Switch) sort of portable media station. I’ll most certainly write about that too in the near future, as there were some problems to solve, but i do like the result.
After I had done some work on it, it was time to install RetroArch, so I asked my package manager about retro. Why? Because i am lazy and I didn’t want to type the extra letters. As it happend, one of my favourite distrebutions (The Void (Linux)) returned not only RetroArch but a package named Cool-Retro-Term.
My interest was sparked so I installed it and being born in the mid 80s I felt nostalgic right away. It is a really nice program, but as a main terminal emulator a little useless as you have to waste some workspace for its lovely looks (I’d easyly get double the lines properly readable on a screen with Konsole).
So thinking about what use this great tool could be, I came up with the Idea of making it a screensaver ‘sort of active lockscreen’. I didn’t use a desktop environment (DE) but i3 so i was already using i3lock. All I needed was a tool able to lock input while still showing the currently active windows.
I was with a couple of similar minded people at spline and together we became rather obsessed with the idea to have htop actually running as your screensaver/lockscreen.
What is the CRT-L
Exactly that, using Cool-Retro-Term and xtrlock to produce a running htop lockscreen.
It can look liek This
Why the CRT-L
As stated above Cool-Retro-Term is to great to not be used, but not only does it not make optimal use of screenspace, it is a openGL terminal emulator, that does really need some GPU power. So using it as soem sort of shortterm demo seemed like the best solution to not ditch it.
Requirements
If your system varies from my setup, you might ofcourse need different requirements met. For this to work i used the following components.
As those simple things usually turn out, making it happen required a little more efford then initially expected, so a short zsh script was written and I’ll also include my CRT theme and the line added to my i3 config.
Step 1
Create a file crt-lock.sh in a folder of your choice, e.g. ~/.tools/crt-lock and paste
%defineBUFFSIZE256global_startsection.dataerrordb"Please enter one or less parameters.",0xA,0x0errlenequ$-errorsection.bssbufferresbBUFFSIZEsection.text_start:; test for argumentscmpqword[rsp],0x2stop1:jl_clireadjg_error; calculate length of stringmovrdi,[rsp+16]; destination indexxorrcx,rcx; rcx = -1notrcx; rcx = -1xoral,alcld; process String from lowest to highest address; (clear diretion flag);SCASB: Compare AL with byte at RDI then set status flags.;REPNZ: Repeats a string instruction RCX times or until ZF = 1repnzscasb; get the string length (dec rcx through NUL)notrcx; rev all bits of negative results in absolute valuedecrcx; -1 to skip the null-terminator, rcx contains; length; initialize source for writemovrsi,[rsp+16]; read from [rsp+16]movrdx,rcx; read rcx bytesjmp_cliwrite_cliread:; read (0) from stdin (0)movrax,0movrdi,0movrsi,buffermovrdx,BUFFSIZEsyscall; initialize source for writemovrsi,buffermovrdx,BUFFSIZEjmp_cliwrite_error:; initialize source for writemovrsi,errormovrdx,errlen_cliwrite:; write (1) to stdout (1)movrax,1movrdi,1syscall_end:movrax,60movrdi,1syscall
I have been tutoring a course called ‘Computer Architecture’ here at the Free University of Berlin more then 5 times over the past three years and most students had a common problem: getting into writing their very first assembly program. As we use Linux 64 bit this tutorial will focus on 64 Bit ELF.
Why Assembly?
I know of only one reason to write programs in assembly, it’s as close as you can get to the way your CPU works, being its biggest disadvantage as well: you’re programming from the perspective of a CPU, not that of a human brain. This prooves difficult for a wide range of students, who are not familiar with CPU architecture. Thus this tutorial will not only try to teach you some assembly basics and NASM syntax, it will additionally try to shed some light on the principles of a computer.
Why NASM?
NASM (Netwide Assembler) is an open source (80x86 and x86-64 architechture) assembler and a pritty good one at that. Compared to MASM, TASM or GAS it is rather easy to use and provides a solid amount of syntactic candy.
How?
When trying something new we usually want to get some positive feedback asap. The normal approcach for an assembly tutorial would be to list all the requirements, work through those step by step, and once they’re met introduce the audience to the actual coding. As I find this highly unsatisfying I’ll get started right away with having you produce your first few lines of code and provide you with the help necessary to fix any requirements on the road. For those of you preferring the more classic approach, here is the requirements section.
Some Coding
Hello World In NASM
So let’s get right to it! Here is the code for a simple Hello World program written in NASM. Go ahead and copy paste the code to a texteditor of your choice and save it as hw.asm.
1
2
3
4
5
6
7
8
9
10
11
12
13
section.datamsgdb"Hello World!",10; db: data byte, 10: ASCII newlinesection.textglobal_start_start:movrax,1; writemovrdi,1; to stdoutmovrsi,msg; starting at msgmovrdx,13; for len bytessyscallmovrax,60; exitmovrdi,0; with successsyscall;_
Once you have a folder containing the hw.asm file with the above content opened in a shell of your choice type
nasm -f elf64 hw.asm && ld hw.o -o hw && ./hw
and hit Return. Now one of two things can happen. It will either work (you’ll see Hello World! written on your console) or it won’t (everything else). I’ll assume it worked, otherwise take a look at the troubleshooting section.
HINT: Typing program1 && program2 in a shell is equivalent to typing program1 and hitting the Return Button first and then typing program2.
If you ever wrote a hallo world program before, you’ll find 13 lines of code alot for such a simple thing. That is because we’re used to using libraries. If we write assembler, we’ll just have to do everything manually (not true btw, later more ;) ). Let’s start in line one.
1 section .data: this is where we can use memory with an initial value.
2 msg db "Hello World!", 10: we get some piece of memory and name its starting address msg, make it the size of so many data bytes as the string Hello World! contains (12 that is) and one more for the ASCII newline being the 10. Everything preceeded by a ; is a comment.
3 section .text: this is where our actual code starts.
4 global _start: the label _start shall be visible globally, thats a good thing, as it is the standart entrypoint for your Linux programs.
5 _start:: create the label _start (don’t forget the colon). It’s basically telling your CPU to go to that location in your code when called.
6 mov rax, 1: NASM syntax gets a Mnemonic (an instruction) at the beginning, followed by a number of arguments (number can be zero :p). Here mov rax 1 means move the number 1 into the CPU-Register RAX.
7 mov rdi, 1: as above, just into CPU-Register RDI.
8 mov rsi, msg: i think you have a solid idea what this does, just keep in mind that msg is an address to the point in memory where our text starts.
9 mov rdx, 13: yhe, repetitive ain’t it… We do indeed store a 13 into CPU-Register RDX.
10 syscall: throws an interrupt that is a syscall, our operating systems will now do a couple of things. First it will look for the kind of syscall in RAX. As we put a 1 there it knows it’s a write syscall. The write syscall needs three additional parameters. The 1 in RDI tells it to write to stdout (your standard output, usually your console), msg in RSI tells it where the text to be written starts and finally the 13 in RDX tells the write syscall to write the first 13 bytes. So basically we will write Hello World! with an additional newline ASCII character to stdout.
With that knowledge the next three lines are rather easy. All we need to know is that we need to manually terminate our program. A 60 in RAX tells the OS it shall perform the exit syscall, which only expects one parameter in RDI, a 0 in our case, being the return value of our program.
So in order to get things done in pure NASM we need to have some knowledge. We need not only the Mnemonics but the syscalls and a proper understanding for our CPU. To make thing a little more easy one can utalize a higher programming language like C. The following example will do an addition in assember and use that function from a C-Program to write the result to stdout.
Go ahead and save this as addit.asm,
1
2
3
4
5
6
section.text; codeglobaladditaddit:movrax,rdi;param1 in raxaddrax,rsi;additionsergebnis in raxret
This is a really nice thing to have, as we don’t need to worry about those syscalls. We’ll handle all the syscalls from within C (wich has libraries for it) and let our NASM function do the real work.
There are only two things our addit.asm needs to know:
Where do my parameters come from? First parameter (the 100) in RDI and second parameter (the 78) in RSI.
Where do i need to save the result? In RAX.
This also tells us that add rax, rsi in line 5 computes like: take the value from RSI, add it to the value in RAX and store the result in RAX. The ret in line 6 ends the function call.
HINT: It is possible to write Mnemonics and Registers in upper- as well as lowercase in NASM.
If we take a look at the C-Programm the advantage really shows. Most of the functionality our 13 line Hello World NASM Program had is found in line 7 of the C-Proramm …
Requirements
There are a couple of things you need in order to get started with writing code in NASM, being able to assemble and link it and then finally execute it.
You will need a 64 bit Linux OS; either native, in a VM or through ssh.
You will need some sort of text editor.
You will need a Shell.
You will need to have NASM installed.
You will need to have ld installed.
You will need to have GCC installed.
Troubleshooting
If you landed in this section, you’re probably not familiar with Linux. This usually happens to people who mainly used Windows or MacOS. That is the majoraty of people, even for students in computer science, so you’re in good company ;). But don’t fret as i have tought hundrets of students starting with little or nothing how to code in NASM. So let us work you through those requirements.
Operatin System
For an OS you’ll need a 64 bit Linux. I recommend Linux Mint 64 bit Cinnamon if you have no idea where to start. As you probably want to read less and do more you might like this installation tutorial. Here you can get the full User Guide in your language. This will set you up with an OS feeling alot like Windows with alot of graphical tools to use. I will write a few words about Linux Distros soon, so keep an eye out.
HINT: A good place to get to know some Linux Distros is Distro Watch.
Texteditor
There are some really great texteditors out there and i’m not going to judge them here. If you don’t know where to start i recommend ATOM because its open source and intuitive to use if you come from Windows or MacOS.
In order to install ATOM in Linux Mint type
sudo add-apt-repository ppa:webupd8team/atom
sudo apt-get update
sudo apt-get install atom
in a shell of your choice.
Shell
As with editors there are alot of great shells out there. I myself am using ZSH with some extras i will cover in a future post. For beginners I recommend fish, as its easy to set up and use.
Additional Software
Install the required software by typing these lines in a terminal:
Über ALU (Algorithmical Logical Unit), CU (Control Unit), Memory, I/O (Input/Output) und mehr.
Das Wintersemester 2016/17 hat begonnen und mit ihm der Kurs Rechnerarchitektur aus dem Modul ‘Rechnerarchitektur, Betriebs- und Kommunikationssysteme’. Für viele Ersties heißt das zum ersten mal Programmieren in NASM und C.
Damit der Start besonders gut klappt gibt es auf der Seite des ersten Tutoriums neben allen organisatorischen Fakten auch eine Einführung für das Softwareproduzieren, so wie ein kleines Beispiel für einen C-Wrapper mit einer NASM-Funktion.
Mein Tipp: Der Einstieg kann schwer sein, lieber ein Wochende mehr damit verbringen das erste Programm zum Laufen zu bekommen, als das gesamte Semester nicht in die Materie einsteigen zu können.