• Home

Apa Itu Bahasa Assembly

 

How exactly do I convert this C program into assembly code? I am having a hard time understanding this process or how to even start it. I am new to this. Any help would be appreciated!

  1. Definisi Bahasa
  2. Apa Itu Array

Side Note: Assume two positive integers a and b are already given in register R0 and R1.
Can you leave comments explaining how you did it?

Learn programming of 8051 microcontroller using embedded C language and assembly language.

SamSam
Apa Itu Bahasa Assembly

5 Answers

If you are using gcc, you can get the assembly as gcc -S -o a.s a.c if your source code is a.c. If you are using Visual Studio, you can get it when you debug by selecting the 'disassembly' window. Here is the output of Visual studio (I named the subrountine/function called 'common' that's why 'common' appears):

Here variable a is saved in memory initially and so is b (dword ptr [b]).

CS PeiCS Pei

The professor that taught me system programming used what he called 'atomic-C' as a stepping stone between C and assembly. The rules for atomic-C are (to the best of my recollection):

  1. only simple expressions allowed, i.e. a = b + c; is allowed a = b + c + d; is not allowed because there are two operators there.
  2. only simple boolean expressions are allowed in an if statement, i.e. if (a < b) is allowed but if (( a < b) && (c < d)) is not allowed.
  3. only if statements, no else blocks.
  4. no for / while or do-while is allowed, only goto's and label's

So, the above program would translate into;

I hope I got that correct..it has been almost twenty years since I last had to write atomic-C. Now assuming the above is correct, lets start converting some of the atomic-C statements into MIPS (assuming that is what you are using) assembly. From the link provided by Elliott Frisch, we can almost immediately translate the subtraction steps:

I used unsigned subtraction due to both a and b being positive integers.

Definisi Bahasa

The comparisons can be done thusly:

Quarter

The problem here is that the third parameter of the beq op-code is the displacement that the PC moves. We will not know that value till we are done doing the hand assembly here.

The inequality is more work. If we leave of the pseudo code instructions, we first need to use the set on less than op-code which put a one in destination register if the first register is less than the second. Once we have done that, we can use the branch on equal as described above.

Jumps are simple, they are just j and then the label to jump to. So,

Last thing we have to handle is the return. The return is done by moving the value we want to a special register V0 and then jumping to the next instruction after the call to this function. The issue is MIPS doesn't have a register to register move command (or if it does I've forgotten it) so we move from a register to RAM and then back again. Finally, we use the special register R31 which holds the return address.

With this information, the program becomes. And we can also adjust the jumps that we didn't know before:

It has been almost twenty years since I've had to do stuff like this (now a days, if I need assembly I just do what others have suggested and let the compiler do all the heavy lifting). I am sure that I've made a few errors along the way, and would be happy for any corrects or suggestions. I only went into this long-winded discussion because I interpreted the OP question as doing a hand translation -- something someone might do as they were learning assembly.

cheers.

thurizasthurizas
1,9911 gold badge8 silver badges12 bronze badges

I've translated that code to 16-bit NASM assembly:

Apa Itu Array

I used ax in place of r0 and bx in place of r1

SirPythonSirPython
Tunaki
94.8k23 gold badges216 silver badges298 bronze badges
denishdenish

Try executing your code here. Just copy it inside the main function, define a and b variables before your while loop and you are good to go.

You can see how the code is compiled to assembly with a fair amount of explanation, and then you can execute the assembly code inside a hypothetical CPU.

darijandarijan
Assembly

Not the answer you're looking for? Browse other questions tagged cassembly or ask your own question.