asm_linux | C (Programming Language) | Assembly Language

Please download to get full document.

View again

of 23
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Information Report



Views: 0 | Pages: 23

Extension: PDF | Download: 0

Related documents
Learn 80386/486/Pentium Assembly Programmin... Learn 32-bit ( for 80386/486 /Pentium/ MMX CPUs ) assembly language programming on a Linux Platform. All you need to carry out these exercises is a PC (386 or up) with Linux loaded. It normally comes with gcc, the Gnu C compiler. S. K. Ghoshal SERC, IISc Bangalore 560 012 India. Index Copyright © 1998 Permission granted only for educational use. For other types of usage, Contact the author. Introdu
  Learn 32-bit ( for 80386/486 /Pentium/ MMX CPUs ) assembly language programming on a Linux Platform. All you need to carry outthese exercises is a PC (386 or up) with Linux loaded. It normally comes with gcc, the Gnu C compiler. S. K. Ghoshal SERC, IISc Bangalore 560 012 India. Index  Copyright © 1998Permission granted only for educational use. For other types of usage, Contact the author.IntroductionStep 1 - The first programStep 2 - There is an assembly equivalent for anything that executesStep 3 - Assembly language needs no segmentation to work -appreciate the flat modelStep 4 - The C compilation roadmapStep 5 - How the control flowsStep 6 - Public SymbolsStep 7 - Parameter Passing RulesStep 8 - Look at the Stack FrameStep 9 - Pass Two Parameters now Step A - Call C program from Assembly Step B - Allocate and access static variablesStep C - Static variables are by referenceStep D - Master address arithmeticStep E - Static Variables defined in C program body Step F - Automatic VariablesStep 10 - towards recursion Learn 80386/486/Pentium Assembly Programmin... von 232011-06-09 16:46  Step 11 - factorialStep 12 - FibonacciStep 13 - Ackermann FunctionStep 14 - From here Your Feedback  Introduction  Assembly language programs are required when one needs to controlhardware directly, without any compiler-generated code coming inbetween the programmer and the microprocessor.This (that is, the practice of programming in assembly language) hasthe advantage of speed of execution as well as predictable latencies.In other words, segments of human-written assembly language codereduces execution time down to the last possible clock cycle. Also, thetime for the program control to pass between two checkpoints (e.g. twolabels in an assembly program fragment) can be predicted with theresolution of one clock cycle.The disadvantage is that writing huge assembly language programs istediuos and error-prone. So for writing larger programs, one prefers to write in high-level languages. C is one such language widely used by programmers who write system software.Most system software (e.g. the Unix operating system) is written mostly in C, with a few assembly language routines at the core. 95% of the codeis in C. The C routines call assembly routines where it must. Sometimesassembly routines call C programs too. One example is where the kernelinitializations are done by executing assembly language programs uptothe point that the execution model required by a C program (that is aprogram written in C and compiled using a C compiler) can besupported. From then onwards, the rest of the initialization in C.Therefore, at that point an assembly routine has to call a C routine.Thus a person aspiring to write or understand the structure of large andcomplex system software must first understand how to write C-callableassembly language programs and vice-versa.In this class, we shall learn assembly language programming. We focuson C-callable assembly language programs. We shall use 80X86 (Thatincludes 80386/80486/Pentium/Pentium Pro/MMX/Pentium II Learn 80386/486/Pentium Assembly Programmin... von 232011-06-09 16:46  microprocessors) based IBM PCs with Linux operating system.Once you undergo this course, you will also learn how compiler-translated code actually works on the PC platform. Many of your other doubts will be resolved as well.Go back to Top Step 1 - Our first Program Let us write and run our first C callable assembly program on a Linux PC. Write a C caller program like this: main(){int iret;iret = ascallee(5);printf ( retval=%d\n , iret);} Call this program as caller.cThis small C program will be our driver program for many assembly routines that we write. And an assembly language callee like this: .globl ascalleeascallee:push %ebpmovl %esp, %ebp;movl 8(%ebp), %eaxshl %eaxpop %ebpret Call this program as callee.sThis assembly language function returns twice the value of the integer argument supplied to it. In the next step we shall see how this works.Compile by typing: cc caller.c callee.s Run the program by typing: a.out Learn 80386/486/Pentium Assembly Programmin... von 232011-06-09 16:46  That will print out 10, which is twice the value of 5.Go back to Top Step 2 - Look at the C equivalent of  what we just ran Let us understand how the above worked. Note that if we had writtenthe doubler function in C, like int double(int arg1){return(2*arg1);} that C function would been translated into code which is similar to theassembly code we wrote ourselves. Ultimately every high-level languagecode becomes assembly language code after compilation which isassembled into machine language and that is what is actually executedby the microprocessor.It is just that there is only one way to translate an assembly programinto an equivalent machine language program. On the other hand thereare many (possibly infinite) ways to translate a high-level languageprogram into an assembly language program.Go back to Top Step 3 - Understand the Flat model This is  Linux  , some type of Unix. Unix on PCs is a 32-bit OS. Here we use32-bit wide data values and 32-bit addresses. That makes the Virtualaddress space 4GB wide. (10 bits address a Kilo, 20 a Meg and 30 aGiga. 2 raised to the power of 2 is 4. So with 32 bits, we can address4GB)This type of a memory model is called a  flat memory model . Here, wecan do any type of address arithmetic by treating the address as data . We can use the result of the arithmetic as an address to point atanything within the virtual address space.This memory model is called the flat memory model. There is nosegmentation imposed by the architecture. Compiler writers and OS Learn 80386/486/Pentium Assembly Programmin... von 232011-06-09 16:46
We Need Your Support
Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks