Assembly basic tutorial - add two numbersWelcome guys, today another basic tutorial on assembly coding. Hear I'll explain you how we can add two integers with assembly. Actually the purpose of this code is not to build a calculator in assembly. But it'll help you to understand some basic theories about assembly language. So let's start it. Hear is the code.
Before we understand what it does let's compile and link it. First I use NASM to compile and ld linker to link. Now we have the object file that can directly run on CPU. Since our program doesn't printout anything we have to get output with environmental variable. If you read my previous tutorial its not a new thing to you. We can use echo $? to be the statue code of our program. Hear we can see the result. Its working. Awesome, let's talk how it do that. First thing to note is there are two sections called .section .data and .section .text. The variables and other data are saved in .data section while actually program instructions are in .text section. In this program there are nothing in .data sections. Because this is a very little program ,we don't want it. The first assembly instruction is mov eax, 0x1. The mov instruction is used to move some value from one place to another place. Those places may be a register , a memory location or even a place on the stack. Actually this is not equal to move a file in Windows or Linux. When we move a file in a OS , we cut the source and paste it in destination. But hear we just copy the value. Hear you can see mov instruction gets two arguments. Remember that we are coding with Intel syntax. So first one is destination while second one is source. This command will copy hex value 0x1 into eax. Second instruction do the same as above one. It'll copy 0x2 into ecx. Next instruction is add eax, ecx. Hear we see another command called add. This will add two arguments and save the result in first argument. So after this command the sum of eax and ecx will be saved in eax. Now our task is completed . we were successfully able to add two numbers. :-) . Finally we want to exit our program and output the result of calculation. You know that we can use the exit system call for this. For that we need to save status value in ebx and fill eax with the system call number. The sys call number for exit is 1.so hear we do that. The result of calculation is currently saved in eax . so I copied eax into ebx and filled eax with 1. Finally I used int 0x80 so it'll call kernel to handle system call. I hope you understand it. In next tutorials I'll explain more concepts in assembly coding. Also I'll write a tutorial on shell coding too. Thank you for reading. It's nice if you share it.
.section .data .section .text mov eax, 0x1 mov ecx, 0x2 add eax, ecx mov ebx, eax mov eax, 0x1 int 0x80
Hi, I'm Thilan from Srilanka. An undergraduate Engineering student of University of Ruhuna. I love to explorer things about CS, Hacking, Reverse engineering etc.