Sample programs in assembly




















Every assembler may have it's own assembly language designed for a specific computers or an operating system. Assembly language requires less execution time and memory. It is more helful for direct hardware manipulation, real-time critical applications. It is used in device drivers, low-level embedded systems etc. There are various define directives to allocate space for variables for both initialized and uninitialized data.

Procedure is a sub-routine which contains set of statements. In the assembly language program, each segment is given a name. It then considers the segment DATA as a default data segment for each memory operation, related to the data and the segment CODE as a source segment for the machine codes of the program. When the assembler comes across this END directive, it ignores the source lines available later on.

Hence, it should be ensured that the END statement should be the last statement in the file and should not appear in between. No useful program statement should lie in the file, after the END statement. In assembly language programming, the subroutines are called procedures. Thus, procedures may be independent program modules which return particular results or values to the calling programs. The ENDP directive is used to indicate the end of a procedure. A procedure is usually assigned a name, i.

To mark the end of a particular procedure, the name of the procedure, i. The statements, appearing in the same module but after the ENDP directive, are neglected from that procedure. The structure given below explains the use of ENDP. The names appear with the ENDS directive as prefixes to mark the end of those particular segments.

Whatever are the contents of the segments, they should appear in the program before ENDS. Any statement appearing after ENDS will be neglected from the segment. The structure shown below explains the fact more clearly. It goes on assigning the available addresses, i.

The EVEN directive updates the location counter to the next even address if the current location counter contents are not even, and assigns the following routine or variable or constant to that address. The structure given below explains the directive. The above structure shows a procedure ROOT that is to be aligned at an even address.

The assembler will start assembling the main program calling ROOT. When the assembler comes across the directive EVEN, it checks the contents of the location counter. If it is odd, it is updated to the next even value and then the ROOT procedure is assigned to that address, i.

If the content of the location counter is already even, then the ROOT procedure will be assigned with the same address. The use of this directive is just to reduce the recurrence of the numerical values or constants in a program code. The recurring value is assigned with a label, and that label is used in place of that numerical value, throughout the program. While assembling, whenever the assembler comes across the label, it substitutes the numerical value for that label and finds out the equivalent code.

Using the EQU directive, even an instruction mnemonic can be assigned with a label, and the label can then be used in the program in place of th at mnemonic. Suppose, a numerical constant appears 'in a program ten times. This document contains very brief examples of assembly language programs for the x The topic of x86 assembly language programming is messy because:. We will even include a section on DOS assembly language programs for historical interest. These notes are not intended to be a substitute for the documentation that accompanies the processor and the assemblers, nor is it intended to teach you assembly language.

Its only purpose is to show how to assemble and link programs using different assemblers and linkers. Regardless of the assembler, object file format, linker or operating system you use, the programming process is always the same:. Each assembly language file is assembled into an "object file" and the object files are linked with other object files to form an executable. A "static library" is really nothing more than a collection of probably related object files.

This document does not cover how to use all the different assemblers; you need to read the documentation that comes with them. We will, however, give step-by-step instructions and complete examples of all three of these assemblers for a few extremely simple programs.

Some linkers out there include. In our first example we will use system calls for writing to a file call number 1 and exiting a process call number Here it is in the NASM assembly language:. If you just enter " gcc hello. You can suppress the link step with the -c option to gcc , or do the assembly and linking in one step by telling the linker not to use the C library with -nostdlib. System Calls in bit Linux There are some systems with bit builds of Linux out there still.

Although it might be interesting to show some examples for historical reasons, this introduction is probably better kept short. Sometimes you might like to use your favorite C library functions in your assembly code. This should be trivial because the C library functions are all stored in a C library, such as libc. Technically the code is probably in a dynamic library, like libc. Still, all we have to do is place calls to C functions in our assembly language program, and link with the static C library and we are set.

So if we link with a C library, all we have to do is define main and end with a ret instruction! Here is a simple example in NASM, which illustrates calling puts.



0コメント

  • 1000 / 1000