Back to Assembly from C.

Stay out of memory if you can.
Post Reply
Site Admin
Posts: 178
Joined: Tue Sep 29, 2009 6:25 pm
Location: Moss Norway

Back to Assembly from C.

Post by KBleivik »

Let us get started. I use my old stationary pc with Window Xp home edition and my laptop with Windows Vista. Both have a DOS subtask that I for convenience have a pointer to on my desktop. So now go to DOS subtask. If you are not used to DOS, you get an overview of available commands by writing help at the DOS prompt. Since I use C++ Builder 2010 I, made a directory bcpp2010 by the command md bcpp2010, then changed to that directory with the command cd bcpp2010 and made a subdirectory c by the command md c and changed to that directory by writing cd c. This could of course have been made from Windows explorer. It is however good to know how to do things at the DOS prompt when you are working in the DOS shell. If you issued the help command above you should have noticed that cls clears the screen. Help tree gives a deeper explanation of the tree command. You can repeat your last command by hitting F3 + Enter. If you wrote something wrong on the last command, you can use F2 repeatedly. Combined with ins and del that is a fast way to fix a wrong command. Since I use the command line version of C++ Builder I must check that it is configured correctly. The path command does that and everything seems ok by my default installation and configuration of the compiler. Now it is time to write the first c program. DOS is fast, so you do not need to load an editor for small programs. You can write the program directly to the console from the DOS prompt. So start by typing line by line exactly as:

copy con first.c
main () {}

and after the last curly brace } press CTRL + Z + ENTER. You should now have a small C source file and the smallest C program. Check it by typing dir at the DOS prompt. Check that everything is spelled exactly as above by issuing type first.c. If it is not, you have two fast options to fix it. Enter the DOS editor by the command edit first.c. Alternatively you can delete the file. Help erase explains how. Then you can repeat the above steps so that everything is correct. But you need not erase the file. You can overwrite the wrong file by the two lines above. Now we are ready to use the BCC32 C++ command line compiler. The following command

BCC32 -h

give an overview of available options. Now clear your screen, cls - observe the files in your working directory, dir and compile your first c program with the following command:

BCC32 first.c

Ignore the messages at this time. Issue dir and note that there are three new files first.exe, first.obj and first.tds. The first is the executable program file that can be run by typing first or first.exe. Since the program did not do anything, you only get a new DOS prompt. But you want more. You want the assembler code for the program, that is human readable machine instructions. Unless you operate on binary numbers, you will most probably never come closer to your computer than what we are going to do now. Repeat the BCC32 –h command and note the options for compiling to .ASM and assembly. The –B and –S options is what we are looking for.

BCC32 -B first.c

Should perhaps do the job. But issuing dir after that command shows that there is no new file first.asm. What about BCC32 -S first.c ? That does the job and there is a new file first.asm. That is a pure text file, so you can look at the code in your editor, edit first.asm. Don’t worry about the content now unless you are interested in learning assembly. Close the file. Again write BCC32 –h at the DOS prompt and note that there is an option –Axxx that enables ANSI conformance. BCC32 -h -A will give a more detailed explanation of the A option. We are interested in the Kernighan and Ritchie (K&R) keywords and extensions, -AK. First we rename first.asm to first-defaule.asm by the command

ren first.asm first-default.asm

and issues the new command

BCC32 -AK -S first.c

Compare the two assembly files first.asm and first-default.asm. There is no difference in the assembler instructions as far as I can see. Now, what if we get rid of the warning during the compilation by returning a value from the main() function. Edit first.c as follows

main ()
return 0;

and save it as second.c and compile it by issuing

BCC32 -AK -S -Zx second.c.

We got rid of the warning and BCC32 -h -Z explains the last –Zx option. Issuing dir we note that there is a new file second.xml file, but second.asm is empty (0 bytes). We have to recompile, BCC32 -S second.c to produce the assembly file. Take a look at the new files and compare them with the former if you intend to understand some assembly and xml.

Exercise: Generally compile with different options and specifically with various –Z options.

Now we want to produce some output. So modify second.c to third.c by issuing copy second.c third.c and edit third.c to:

#include < stdio.h>
int main (void)
printf(“Programming is easy and fun even at the low level!”);
return 0;

Now run the program by first issuing third and then third.exe. It is time to learn some more assembly while we are at the machine code level. If your computer is like mine, there is a tool called debug at the DOS prompt. If you start debug you will note that the debug prompt is a - and if you write a ? (it may be another key on your keyboard than the question mark. On my laptop shift combined with - produce the question mark) at the debug prompt and hit enter, you get an overview of the available commands. Don’t do anything else than what is explained here, since you are able to do a lot of harm to your computer in debug. The goal here is to learn a tool that can be very powerful if you use it correctly. So issue the following commands D, R, U and Q that is completely meaningless unless you know debug or some assembly. Now issue the following command at the DOS prompt:

debug third.c

and repeat the four debug commands above. Do exactly the same with third.exe. Note that third.c is a text file while third.exe is a compiled program file that you will not understand much from if you load it into DOS edit. Try and see. When you loaded the .exe file in debug the code looked similar, but far from identical to the one produced by the source file, third.c. So debug can read a program file and translate its content to human readable assembly code. In addition, note that U in debug means unassemble, another word for disassemble or reverse engineer. Nice to know.

Now, at the DOS prompt, type exit to return to Windows.

More about debug and assembly here:

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest