Skip to main content

Writing if statements in assembly language

Programs become more and more interesting when you have dynamic elements in them.  On such way of bringing your program to life is by adding logic.  In assembly, the task can seem dubious and awkward, but once you get a grip on the concept, it will be but second nature.

So lets get started!



//We want to write an equivalent program to this in assembly
#include <stdio.h>
int main(){
int x = 40;
if( x > 10){
printf("x is greater than 10\n");
}else{
printf("x is lesser than 10\n");
}
return 0;
} 

To write this in assembly, consider the following:

;an equivalent program to this in assembly
SECTION .data
x: dd 40
msg1: db "x is greater than 10", 10, 0
msg2: db "x is lesser than 10", 10, 0 
SECTION .text




Here, all we did is create our variable x, and the respective message that we will display depending on the result of our if statement.

Continually:
;an equivalent program to this in assembly
SECTION .data

x: dd 40
msg1: db "x is greater than 10", 10, 0
msg2: db "x is lesser than 10", 10, 0 
SECTION .text

;allow access to printf
extern printf
;make our main available externally
global main
main:
 push ebp
 mov ebp , esp
 cmp DWORD [x] , 10
 jg .conditionIsTrue   ;translates to: jmp if [x] > 10
.conditionNotTrue:         ;translate to: else 
 push DWORD msg2
 call printf
.conditionIsTrue:
 push DWORD msg1
 call printf
 jmp .done
.done:
 mov esp, ebp
 pop ebp
 ret
 

Comments

Post a Comment

Popular posts from this blog

Creating local variables In Assembly

Lets go over how to create local variables inside of a pure assembly source code. Much like always, you will start with a *.asm file that looks like this: source code SECTION .data SECTION .bss SECTION .text global main ;make main available to operating system(os) main: ;create the stack frame push ebp push mov ebp, esp ;destroy the stack frame mov esp, ebp pop ebp ret So, the above is the general layout of an NASM source file.  Our goal here is to create a local variable inside of the main method.  The only way to create a local variable is by using the stack.  Why?  Because we can only declare variable in storage locations and the only available storage locations are: text, bss, and data.  However, text section is only for code, so it is out of the question.  The bss and data sections are appealing, but to declare our "local" variable in these sections will defeat the purpose of these varia...

Introduction to Linux Kernel Programming

The Linux kernel is designed as a mixture of a monolithic binary image and a micro-kernel.  This combination allows for the best of both worlds.  On the monolithic side, all the code for the kernel to work with the user and hardware is already installed and ready for fast access, but the downside is that to add more functionality you need to rebuild the entire kernel.   In a different manner, a micro-kernel is composed of small pieces  of code that can be meshed today and more pieces can be added or removed as needed.  However, the downside to micro-kernel is a slower performance. Adding a module to the Kernel Linux is organized as both monolithic, one huge binary, and micro-kernel, as you can add more functionality to it.  The process of adding more functionality to the kernel can be illustrated by the crude image to the left. The process begins by using the command insmod with the name of the kernel module you want (which usually ends with ex...

Introduction to Linux Kernel Device Drivers

If you toy with electronics, perhaps with microcontrollers (MCU), you might at some point in time decide to interface your microcontroller to your computer.  However, you will come to learn that it would not work for a variety of reasons: one being that you need correct voltage between the computer and the microcontroller, and the second, most critical, you need a way for the computer to recognize and speak to your device. Well, this is where device drivers are useful.  Device drivers are the software need for your computer applications to talk to your device.  The power of a device driver and the possibilities that lie with it can not fully be summarized by myself. So, so whatever reason you many want to write a device driver, in this post, along with others, I will demonstrate how to write a device driver. Major and Minor Numbers When we write a device driver, that device driver will become part of the kernel and it will have an identification number calle...