How to control the flow of a program in x86 assembly

x86 assembly language just like most other programming languages provides with the ability to control the flow of the program using various instructions.

This article provides an overview of those instructions that can be used to control the flow of a program.

See the last article in this , How to diagnose and locate segmentation faults in x86 assembly.

Using comparison instructions to control applications at the x86 level

x86 instruction set comes with two popular instructions for comparison. They are CMP and TEST.  Let us explore the following program to understand how these two instructions work.

section .text

global _start


mov eax, 101

mov ebx, 100

mov ecx, 100

cmp eax, ebx

cmp ebx, ecx

xor eax, eax

test eax, eax

First, let us assemble and link this program using the following commands.

$ nasm comparison.nasm -o comparison.o -f elf32

$ ld comparison.o -o comparison -m elf_i386

Now, let us load the program in GDB as shown below.

$ gdb ./comparison

Set up a breakpoint at the entry point of the program and run the program as shown

Read More: