You will agree that a visual debugger such as DDD is quite useful, especially for investigating large programs. In this chapter, we will introduce SASM (for SimpleASM). It is an open source, cross-platform integrated development environment (IDE). It features syntax highlighting and graphical debugging. It’s a fantastic tool for an assembler programmer!
Installing SimpleASM
This will normally install all the required missing packages.
Using SASM
A bunch of files will be installed, and you won’t see the error anymore.
Be very careful here, because the settings have to be exactly as shown in the figure; one space too many, even hidden at the end of a line, and SASM will not do what you want. When you are ready, click the OK button and restart SASM.
If jump.asm does not exist, SASM will start with a new editor window; just delete the default code. If the file exists, it will open in the editor window.
jump.asm
When you save a file in SASM, the source code will be saved. If you want to save the executable, you need to choose Save.exe in the File menu.
To start debugging, click in the numbered left margin to the left of the main: label. This will put a red circle between the main: label and its line number. This is a breakpoint. Then at the top click the green triangle with the bug on it. In the top menu, choose Debug and select Show Registers and Show Memory. A number of additional windows will appear on your screen: Registers, Memory, and also a GDB command-line widget.
This line can confuse other debuggers such as GDB, so make sure to remove it from the code before you run GDB separately from the CLI.
In the SASM menu Settings ➤ Common, make sure to select Yes for “Show all registers in debug.” When debugging in SASM, scroll down in the register window. At the bottom you will see 16 ymm registers, each with two values between parentheses. The first value is the corresponding xmm register. We will explain these registers in more detail when we talk about SIMD.
In the program we use a compare instruction cmp and two jump instructions, jge and jmp. The cmp instruction is what is called a conditional instruction. Here cmp compares two operands, in this case two registers. One of the two operands can also be a memory operand, and the second operand can be an immediate value. In any case, the size of the two operands must be the same (byte, word, and so on). The cmp instruction will set or clear flags in the flag register.
The flags are bits located in the rflags register that can be set to 1 or cleared to 0, depending on a number of conditions. Important in our case are the zero flag (ZF), the overflow flag (OF), and the sign flag (SF). You can use your debugger to examine these and other flags. With SASM you can easily see what is happing to all the registers, including the flag register, called eflags in SASM. Different values in the cmp operands will result in different flags being set or cleared. Experiment a little bit with the values to see what is happening with the flags.
Jump Instructions and Flags
Instruction | Flags | Meaning | Use |
---|---|---|---|
je | ZF=1 | Jump if equal | Signed, unsigned |
jne | ZF=0 | Jump if not equal | Signed, unsigned |
jg | ((SF XOR OF) OR ZF) = 0 | Jump if greater | Signed |
jge | (SF XOR OF) = 0 | Jump if greater or equal | Signed |
jl | (SF XOR OF) = 1 | Jump if lower | Signed |
jle | ((SF XOR OF) OR ZF) = 1 | Jump if lower or equal | Signed |
ja | (CF OR ZF) = 0 | Jump if above | Unsigned |
jae | CF=0 | Jump if above or equal | Unsigned |
jb | CF=1 | Jump if lesser | Unsigned |
jbe | (CF OR ZF) = 1 | Jump if lesser or equal | Unsigned |
In our program we have also an unconditional jump instruction, jmp . If the program execution hits this instruction, the program jumps to the label specified after jmp, regardless of flags or conditions.
jumploop.asm
The program adds all the numbers from 0 to the value in number. We use rbx as a counter and rax to keep track of the sum. We created a loop, which is the code between jloop: and jle jloop. In the loop, we add the value in rbx to rax, increase rbx with 1, and then compare if we have reached the end (number). If we have in rbx a value lower than or equal to number, we restart the loop; otherwise, we continue with the instruction after the loop and get ready to print the result. We used an arithmetic instruction, inc, to increase rbx. We will discuss arithmetic instructions in later chapters.
betterloop.asm
Here you see that there is a special loop instruction that uses rcx as a decreasing loop counter. With every pass through the loop, rcx is decreased automatically, and as long as rcx is not equal to 0, the loop is executed again. That’s less code to type.
You may wonder why we bothered to use DDD when there is a tool such as SASM. Well, you will see Iater that in SASM you cannot investigate the stack, but you can with DDD. We will return to DDD later.
Summary
How to use SASM
How to use jump instructions
How to use the cmp instruction
How to use the loop instruction
How to evaluate flags