I continued working through the Assembly code basics. So far this has been challenging. There are a lot of concepts and terms that I am not familiar with. This is especially true when it comes to the differences between operating systems and processors.
TLDR;
Okay, so here are the highlights of what I did:
- I read through 30% of chapter 3 of the “Programming from the Ground Up” book by Jonathan Bartlett. I learned a lot of new terms and it was a little overwhelming tbh.
- I built my first assembly code program by copying and pasting some code from the book lol. All it did was exit and return a status code. The interesting thing was that it only ran on my virtual machine running Linux. The program would not run on my windows machine. So… I still need to learn more about the differences between assembly code and the requirements for different operating systems.
Notes from “Programming from the Ground Up”
First Assembly Code Program
#PURPOSE: Simple program that exits and returns a
# status code back to the Linux kernel
#
#INPUT: none
#
#OUTPUT: returns a status code. This can be viewed
# by typing
#
# echo $?
#
# after running the program
#
#VARIABLES:
# %eax holds the system call number
# %ebx holds the return status
#
.section .data
.section .text
.globl _start
_start:
movl $1, %eax # this is the linux kernel command
# number (system call) for exiting
# a program
movl $0, %ebx # this is the status number we will
# return to the operating system.
# Change this around and it will
# return different things to
# echo $?
int $0x80 # this wakes up the kernel to run
# the exit command
Assembler Directives & Pseudo-operations
Anything starting with a period isn’t directly translated into a machine instruction e.g. (.section .data
). Instead, it’s an instruction to the assembler itself. These are called assembler directives
or pseudo-operations
because they are handled by the assembler and are not actually run by the computer.
.section
The .section
command breaks your program up into sections. This command starts the data section
, where you list any memory storage you will need for data. Our program doesn’t use any, so we don’t need the section. It’s just here for completeness. Almost every program you write in the future will have data. e.g. .section .text
(The text section of a program is where the program instructions live.)
Symbols in Assembly Code
Symbols are generally used to mark locations of programs or data, so you can refer to them by name instead of by their location number. Symbols are used so that the assembler and linker can take care of keeping track of addresses, and you can concentrate on writing your program.
Imagine if you had to refer to every memory location by it’s address. First of all, it would be very confusing because you would have to memorize or look up the numeric memory address of every piece of code or data. In addition, every time you had to insert a piece of data or code you would have to change all the addresses in your program!
.globl
.globl
means that the assembler shouldn’t discard this symbol after assembly, because the linker will need it.
.globl _start
This instructs the assembler that _start
is important to remember. _start
is a symbol, which means that it is going to be replaced by something else either during assembly or linking. _start
is a special symbol that always needs to be marked with .globl
because it marks the location of the start of the program. Without marking this location in this way, when the computer loads your program it won’t know where to begin running your program.
Labels in Assembly Code
A label is a symbol followed by a colon. Labels define a symbol’s value. When the assembler is assembling the program, it has to assign each data value and instruction an address. Labels tell the assembler to make the symbol’s value be wherever the next instruction or data element will be. This way, if the actual physical location of the data or instruction changes, you don’t have to rewrite any references to it – the symbol automatically gets the new value.
_start:
defines the value of the _start
label.
Conclusion
That’s all for today. If you are interested in the MIT course you can check out the video lecture I’m currently going through. The lecture is helpful but isn’t sufficient by itself. Anyways, until next time PEACE!