How to Read a File With Spaces in Assembly
x86 Assembly Guide
Contents: Registers | Retentivity and Addressing | Instructions | Calling Convention
This is a version adapted past Quentin Carbonneaux from David Evans' original document. The syntax was changed from Intel to AT&T, the standard syntax on UNIX systems, and the HTML code was purified.
This guide describes the nuts of 32-bit x86 associates language programming, covering a pocket-sized but useful subset of the available instructions and assembler directives. There are several different associates languages for generating x86 machine lawmaking. The one nosotros will use in CS421 is the GNU Assembler (gas) assembler. We will uses the standard AT&T syntax for writing x86 assembly code.
The full x86 education gear up is large and complex (Intel's x86 instruction set manuals comprise over 2900 pages), and we do not embrace it all in this guide. For example, there is a 16-bit subset of the x86 education prepare. Using the 16-bit programming model can be quite complex. Information technology has a segmented retentivity model, more than restrictions on register usage, and so on. In this guide, we will limit our attention to more modernistic aspects of x86 programming, and delve into the educational activity ready only in plenty item to get a basic feel for x86 programming.
Registers
Modern (i.eastward 386 and beyond) x86 processors accept eight 32-bit general purpose registers, every bit depicted in Effigy 1. The register names are mostly historical. For example, EAX used to be chosen the accumulator since it was used past a number of arithmetic operations, and ECX was known as the counter since information technology was used to hold a loop alphabetize. Whereas about of the registers have lost their special purposes in the modern instruction fix, by convention, two are reserved for special purposes — the stack arrow (ESP) and the base pointer (EBP).
For the EAX, EBX, ECX, and EDX registers, subsections may be used. For case, the least significant two bytes of EAX tin be treated as a 16-chip register called AX. The least significant byte of AX tin be used every bit a single 8-bit annals called AL, while the about significant byte of AX tin be used as a single viii-flake register called AH. These names refer to the aforementioned physical register. When a two-byte quantity is placed into DX, the update affects the value of DH, DL, and EDX. These sub-registers are mainly agree-overs from older, xvi-bit versions of the educational activity ready. However, they are sometimes convenient when dealing with data that are smaller than 32-bits (eastward.g. 1-byte ASCII characters).
Figure 1. x86 Registers
Retentiveness and Addressing Modes
Declaring Static Data Regions
You tin declare static data regions (analogous to global variables) in x86 associates using special assembler directives for this purpose. Data declarations should be preceded by the .data directive. Following this directive, the directives .byte, .short, and .long tin can be used to declare one, 2, and 4 byte data locations, respectively. To refer to the address of the data created, nosotros can label them. Labels are very useful and versatile in associates, they requite names to retentiveness locations that will be figured out later by the assembler or the linker. This is similar to declaring variables by name, just abides by some lower level rules. For case, locations alleged in sequence will exist located in memory next to 1 another.
Case declarations:
.information var: .byte 64 /* Declare a byte, referred to as location var, containing the value 64. */ .byte 10 /* Declare a byte with no label, containing the value 10. Its location is var + i. */ 10: .short 42 /* Declare a 2-byte value initialized to 42, referred to equally location x. */ y: .long 30000 /* Declare a 4-byte value, referred to as location y, initialized to 30000. */
Dissimilar in high level languages where arrays can accept many dimensions and are accessed by indices, arrays in x86 assembly language are simply a number of cells located contiguously in memory. An array tin be declared by just listing the values, as in the first instance below. For the special case of an array of bytes, string literals can be used. In case a big area of memory is filled with zeroes the .aught directive tin be used.
Some examples:
due south: .long i, 2, 3 /* Declare iii 4-byte values, initialized to 1, 2, and 3.
The value at location due south + 8 will exist 3. */barr: .zilch ten /* Declare x bytes starting at location barr, initialized to 0. */ str: .cord "howdy" /* Declare half-dozen bytes starting at the address str initialized to
the ASCII character values for howdy followed by a nul (0) byte. */
Addressing Retention
Modernistic x86-compatible processors are capable of addressing upwards to 232 bytes of retention: memory addresses are 32-$.25 broad. In the examples above, where nosotros used labels to refer to memory regions, these labels are actually replaced by the assembler with 32-bit quantities that specify addresses in memory. In add-on to supporting referring to memory regions by labels (i.e. constant values), the x86 provides a flexible scheme for computing and referring to retentiveness addresses: up to two of the 32-bit registers and a 32-chip signed abiding can be added together to compute a memory address. One of the registers tin be optionally pre-multiplied by 2, 4, or viii.
The addressing modes can be used with many x86 instructions (nosotros'll describe them in the side by side section). Here we illustrate some examples using the mov instruction that moves data between registers and retention. This instruction has two operands: the kickoff is the source and the second specifies the destination.
Some examples of mov instructions using address computations are:
mov (%ebx), %eax /* Load 4 bytes from the retentivity address in EBX into EAX. */ mov %ebx, var(,i) /* Move the contents of EBX into the 4 bytes at memory accost var.
(Notation, var is a 32-fleck abiding). */mov -4(%esi), %eax /* Move 4 bytes at memory address ESI + (-4) into EAX. */ mov %cl, (%esi,%eax,1) /* Movement the contents of CL into the byte at accost ESI+EAX. */ mov (%esi,%ebx,4), %edx /* Move the four bytes of data at address ESI+iv*EBX into EDX. */
Some examples of invalid address calculations include:
mov (%ebx,%ecx,-one), %eax /* Can but add register values. */ mov %ebx, (%eax,%esi,%edi,one) /* At nearly ii registers in address ciphering. */
Operation Suffixes
In general, the intended size of the of the data item at a given memory accost can be inferred from the assembly code instruction in which it is referenced. For example, in all of the above instructions, the size of the memory regions could exist inferred from the size of the register operand. When nosotros were loading a 32-bit annals, the assembler could infer that the region of memory nosotros were referring to was 4 bytes wide. When we were storing the value of a one byte annals to memory, the assembler could infer that we wanted the address to refer to a single byte in retention.
However, in some cases the size of a referred-to retention region is ambiguous. Consider the instruction mov $2, (%ebx). Should this education move the value ii into the single byte at address EBX? Perhaps information technology should move the 32-scrap integer representation of 2 into the 4-bytes starting at address EBX. Since either is a valid possible interpretation, the assembler must be explicitly directed as to which is correct. The size prefixes b, w, and l serve this purpose, indicating sizes of 1, 2, and 4 bytes respectively.
For instance:
movb $ii, (%ebx) /* Move 2 into the single byte at the address stored in EBX. */ movw $2, (%ebx) /* Move the 16-scrap integer representation of 2 into the 2 bytes starting at the address in EBX. */ movl $2, (%ebx) /* Move the 32-scrap integer representation of 2 into the 4 bytes starting at the address in EBX. */
Instructions
Machine instructions more often than not autumn into three categories: data motion, arithmetics/logic, and command-flow. In this section, we will look at important examples of x86 instructions from each category. This section should non exist considered an exhaustive list of x86 instructions, merely rather a useful subset. For a consummate listing, see Intel's educational activity set reference.
We utilize the following note:
<reg32> Any 32-fleck register (%eax, %ebx, %ecx, %edx, %esi, %edi, %esp, or %ebp) <reg16> Any 16-bit register (%ax, %bx, %cx, or %dx) <reg8> Whatever eight-bit register (%ah, %bh, %ch, %dh, %al, %bl, %cl, or %dl) <reg> Whatever register <mem> A memory address (east.g., (%eax), 4+var(,i), or (%eax,%ebx,one)) <con32> Any 32-bit immediate <con16> Any sixteen-bit immediate <con8> Any 8-bit immediate <con> Whatever 8-, 16-, or 32-scrap firsthand
In associates language, all the labels and numeric constants used as firsthand operands (i.e. non in an address adding like 3(%eax,%ebx,viii)) are always prefixed by a dollar sign. When needed, hexadecimal notation can be used with the 0x prefix (due east.k. $0xABC). Without the prefix, numbers are interpreted in the decimal basis.
Data Movement Instructions
mov — Motility
The mov instruction copies the information item referred to by its get-go operand (i.e. annals contents, memory contents, or a constant value) into the location referred to past its second operand (i.e. a register or memory). While register-to-register moves are possible, direct memory-to-memory moves are not. In cases where memory transfers are desired, the source retentiveness contents must first be loaded into a register, then tin can be stored to the destination memory address.Syntax
mov <reg>, <reg>
mov <reg>, <mem>
mov <mem>, <reg>
mov <con>, <reg>
mov <con>, <mem>
Examples
mov %ebx, %eax — copy the value in EBX into EAX
movb $five, var(,1) — store the value 5 into the byte at location var
push — Push on stack
The push pedagogy places its operand onto the superlative of the hardware supported stack in memory. Specifically, push first decrements ESP by four, so places its operand into the contents of the 32-bit location at accost (%esp). ESP (the stack pointer) is decremented by push since the x86 stack grows down — i.e. the stack grows from high addresses to lower addresses.Syntax
push button <reg32>
button <mem>
push <con32>Examples
push button %eax — button eax on the stack
push var(,i) — push the four bytes at address var onto the stack
pop — Popular from stack
The pop instruction removes the 4-byte data element from the top of the hardware-supported stack into the specified operand (i.due east. annals or memory location). Information technology first moves the 4 bytes located at memory location (%esp) into the specified register or memory location, and so increments ESP past 4.Syntax
Examples
pop <reg32>
pop <mem>
pop %edi — pop the peak element of the stack into EDI.
pop (%ebx) — pop the pinnacle element of the stack into memory at the 4 bytes starting at location EBX.
lea — Load effective address
The lea instruction places the accost specified by its commencement operand into the register specified by its 2d operand. Note, the contents of the memory location are not loaded, simply the effective accost is computed and placed into the register. This is useful for obtaining a pointer into a memory region or to perform unproblematic arithmetic operations.Syntax
lea <mem>, <reg32>
Examples
lea (%ebx,%esi,8), %edi — the quantity EBX+8*ESI is placed in EDI.
lea val(,1), %eax — the value val is placed in EAX.
Arithmetic and Logic Instructions
add — Integer add-on
The add instruction adds together its two operands, storing the consequence in its second operand. Annotation, whereas both operands may exist registers, at most one operand may be a retentiveness location.Syntax
add together <reg>, <reg>
add <mem>, <reg>
add <reg>, <mem>
add <con>, <reg>
add <con>, <mem>
Examples
add $x, %eax — EAX is set to EAX + 10
addb $10, (%eax) — add ten to the single byte stored at memory address stored in EAX
sub — Integer subtraction
The sub teaching stores in the value of its second operand the effect of subtracting the value of its first operand from the value of its second operand. As with add, whereas both operands may exist registers, at nearly one operand may be a memory location.Syntax
sub <reg>, <reg>
sub <mem>, <reg>
sub <reg>, <mem>
sub <con>, <reg>
sub <con>, <mem>
Examples
sub %ah, %al — AL is ready to AL - AH
sub $216, %eax — subtract 216 from the value stored in EAX
inc, dec — Increment, Decrement
The inc education increments the contents of its operand past 1. The dec instruction decrements the contents of its operand by one.Syntax
inc <reg>
inc <mem>
dec <reg>
december <mem>Examples
dec %eax — subtract 1 from the contents of EAX
incl var(,1) — add together 1 to the 32-chip integer stored at location var
imul — Integer multiplication
The imul instruction has 2 basic formats: 2-operand (first ii syntax listings above) and three-operand (last two syntax listings to a higher place).The ii-operand form multiplies its two operands together and stores the result in the second operand. The event (i.e. second) operand must be a register.
The 3 operand form multiplies its second and third operands together and stores the event in its concluding operand. Again, the result operand must be a register. Furthermore, the first operand is restricted to being a abiding value.
Syntax
imul <reg32>, <reg32>
imul <mem>, <reg32>
imul <con>, <reg32>, <reg32>
imul <con>, <mem>, <reg32>Examples
imul (%ebx), %eax — multiply the contents of EAX by the 32-bit contents of the memory at location EBX. Store the result in EAX.
imul $25, %edi, %esi — ESI is set to EDI * 25
idiv — Integer sectionalization
The idiv instruction divides the contents of the 64 bit integer EDX:EAX (constructed by viewing EDX as the near significant 4 bytes and EAX as the least meaning iv bytes) by the specified operand value. The quotient issue of the division is stored into EAX, while the residual is placed in EDX.Syntax
idiv <reg32>
idiv <mem>Examples
idiv %ebx — carve up the contents of EDX:EAX by the contents of EBX. Place the quotient in EAX and the remainder in EDX.
idivw (%ebx) — dissever the contents of EDX:EAS by the 32-fleck value stored at the retentivity location in EBX. Place the quotient in EAX and the remainder in EDX.
and, or, xor — Bitwise logical and, or, and sectional or
These instructions perform the specified logical performance (logical bitwise and, or, and exclusive or, respectively) on their operands, placing the consequence in the showtime operand location.Syntax
and <reg>, <reg>
and <mem>, <reg>
and <reg>, <mem>
and <con>, <reg>
and <con>, <mem>
or <reg>, <reg>
or <mem>, <reg>
or <reg>, <mem>
or <con>, <reg>
or <con>, <mem>
xor <reg>, <reg>
xor <mem>, <reg>
xor <reg>, <mem>
xor <con>, <reg>
xor <con>, <mem>
Examples
and $0x0f, %eax — clear all but the concluding iv bits of EAX.
xor %edx, %edx — prepare the contents of EDX to zero.
not — Bitwise logical non
Logically negates the operand contents (that is, flips all flake values in the operand).Syntax
not <reg>
not <mem>Instance
not %eax — flip all the bits of EAX
neg — Negate
Performs the two's complement negation of the operand contents.Syntax
neg <reg>
neg <mem>Example
neg %eax — EAX is set to (- EAX)
shl, shr — Shift left and right
These instructions shift the bits in their first operand's contents left and correct, padding the resulting empty bit positions with zeros. The shifted operand tin be shifted up to 31 places. The number of bits to shift is specified by the 2nd operand, which can be either an 8-bit constant or the register CL. In either case, shifts counts of greater then 31 are performed modulo 32.Syntax
shl <con8>, <reg>
shl <con8>, <mem>
shl %cl, <reg>
shl %cl, <mem>shr <con8>, <reg>
shr <con8>, <mem>
shr %cl, <reg>
shr %cl, <mem>Examples
shl $ane, eax — Multiply the value of EAX past two (if the most significant bit is 0)
shr %cl, %ebx — Shop in EBX the floor of result of dividing the value of EBX past ii n where north is the value in CL. Caution: for negative integers, it is unlike from the C semantics of division!
Control Menstruum Instructions
The x86 processor maintains an instruction pointer (EIP) annals that is a 32-bit value indicating the location in retentiveness where the current instruction starts. Normally, it increments to betoken to the next instruction in memory begins after execution an pedagogy. The EIP annals cannot be manipulated directly, but is updated implicitly past provided control flow instructions.
We use the notation <characterization> to refer to labeled locations in the program text. Labels tin can exist inserted anywhere in x86 associates code text by entering a label name followed by a colon. For case,
mov 8(%ebp), %esi begin: xor %ecx, %ecx mov (%esi), %eax
The 2nd instruction in this code fragment is labeled brainstorm. Elsewhere in the lawmaking, we can refer to the retention location that this instruction is located at in memory using the more convenient symbolic name begin. This characterization is but a convenient way of expressing the location instead of its 32-bit value.
jmp — Jump
Transfers programme control flow to the instruction at the retention location indicated by the operand.Syntax
jmp <label>Example
jmp begin — Jump to the pedagogy labeled begin.
jstatus — Provisional jump
These instructions are provisional jumps that are based on the status of a set of condition codes that are stored in a special register called the machine status word. The contents of the machine status word include data about the last arithmetic operation performed. For example, one fleck of this word indicates if the last result was zero. Another indicates if the last result was negative. Based on these condition codes, a number of conditional jumps can be performed. For case, the jz education performs a jump to the specified operand label if the result of the last arithmetic operation was cypher. Otherwise, control proceeds to the next didactics in sequence.A number of the conditional branches are given names that are intuitively based on the last functioning performed being a special compare instruction, cmp (see below). For case, conditional branches such every bit jle and jne are based on first performing a cmp operation on the desired operands.
Syntax
je <label> (jump when equal)
jne <label> (jump when not equal)
jz <label> (jump when last result was naught)
jg <label> (leap when greater than)
jge <label> (spring when greater than or equal to)
jl <label> (jump when less than)
jle <characterization> (leap when less than or equal to)Instance
cmp %ebx, %eax jle doneIf the contents of EAX are less than or equal to the contents of EBX, bound to the label done. Otherwise, continue to the next instruction.
cmp — Compare
Compare the values of the ii specified operands, setting the condition codes in the car condition word accordingly. This instruction is equivalent to the sub instruction, except the consequence of the subtraction is discarded instead of replacing the beginning operand.Syntax
cmp <reg>, <reg>
cmp <mem>, <reg>
cmp <reg>, <mem>
cmp <con>, <reg>Case
cmpb $ten, (%ebx)
jeq loopIf the byte stored at the memory location in EBX is equal to the integer constant 10, jump to the location labeled loop.
telephone call, ret — Subroutine call and return
These instructions implement a subroutine call and return. The telephone call teaching starting time pushes the current code location onto the hardware supported stack in retentiveness (see the push didactics for details), and so performs an unconditional bound to the code location indicated past the label operand. Unlike the unproblematic jump instructions, the call pedagogy saves the location to return to when the subroutine completes.The ret educational activity implements a subroutine return mechanism. This instruction commencement pops a code location off the hardware supported in-retentiveness stack (see the popular instruction for details). It then performs an unconditional jump to the retrieved code location.
Syntax
call <characterization>
ret
Calling Convention
To allow separate programmers to share code and develop libraries for use by many programs, and to simplify the use of subroutines in general, programmers typically adopt a common calling convention. The calling convention is a protocol about how to call and return from routines. For instance, given a ready of calling convention rules, a programmer need not examine the definition of a subroutine to determine how parameters should be passed to that subroutine. Furthermore, given a set of calling convention rules, high-level linguistic communication compilers can be fabricated to follow the rules, thus assuasive hand-coded assembly language routines and high-level language routines to phone call ane another.
In practice, many calling conventions are possible. We will describe the widely used C language calling convention. Following this convention will allow you to write assembly language subroutines that are safely callable from C (and C++) code, and will also enable y'all to telephone call C library functions from your assembly language code.
The C calling convention is based heavily on the utilize of the hardware-supported stack. Information technology is based on the push, pop, telephone call, and ret instructions. Subroutine parameters are passed on the stack. Registers are saved on the stack, and local variables used by subroutines are placed in retentivity on the stack. The vast majority of high-level procedural languages implemented on most processors have used similar calling conventions.
The calling convention is broken into 2 sets of rules. The commencement fix of rules is employed by the caller of the subroutine, and the second set of rules is observed by the author of the subroutine (the callee). It should be emphasized that mistakes in the observance of these rules quickly result in fatal program errors since the stack volition be left in an inconsistent land; thus meticulous intendance should exist used when implementing the call convention in your ain subroutines.
Stack during Subroutine Telephone call
[Thanks to James Peterson for finding and fixing the bug in the original version of this figure!]
A good way to visualize the operation of the calling convention is to draw the contents of the nearby region of the stack during subroutine execution. The image above depicts the contents of the stack during the execution of a subroutine with three parameters and three local variables. The cells depicted in the stack are 32-bit wide memory locations, thus the retentivity addresses of the cells are 4 bytes apart. The commencement parameter resides at an offset of eight bytes from the base pointer. Above the parameters on the stack (and below the base of operations pointer), the call instruction placed the return address, thus leading to an extra 4 bytes of start from the base arrow to the first parameter. When the ret educational activity is used to return from the subroutine, it will jump to the return accost stored on the stack.
Caller Rules
To make a subrouting call, the caller should:
- Before calling a subroutine, the caller should save the contents of certain registers that are designated caller-saved. The caller-saved registers are EAX, ECX, EDX. Since the called subroutine is allowed to modify these registers, if the caller relies on their values after the subroutine returns, the caller must push the values in these registers onto the stack (and then they can be restore subsequently the subroutine returns.
- To pass parameters to the subroutine, push button them onto the stack earlier the telephone call. The parameters should be pushed in inverted society (i.e. last parameter get-go). Since the stack grows downward, the get-go parameter will be stored at the lowest address (this inversion of parameters was historically used to allow functions to exist passed a variable number of parameters).
- To call the subroutine, employ the call instruction. This instruction places the return address on superlative of the parameters on the stack, and branches to the subroutine lawmaking. This invokes the subroutine, which should follow the callee rules beneath.
After the subroutine returns (immediately following the call teaching), the caller can expect to discover the render value of the subroutine in the annals EAX. To restore the machine land, the caller should:
- Remove the parameters from stack. This restores the stack to its state before the call was performed.
- Restore the contents of caller-saved registers (EAX, ECX, EDX) by popping them off of the stack. The caller can presume that no other registers were modified by the subroutine.
Example
The code below shows a role telephone call that follows the caller rules. The caller is calling a function myFunc that takes three integer parameters. First parameter is in EAX, the second parameter is the constant 216; the third parameter is in the memory location stored in EBX.
button (%ebx) /* Push final parameter kickoff */ push button $216 /* Push the second parameter */ push %eax /* Push outset parameter terminal */ phone call myFunc /* Phone call the function (assume C naming) */ add together $12, %esp
Note that after the phone call returns, the caller cleans up the stack using the add instruction. We take 12 bytes (3 parameters * 4 bytes each) on the stack, and the stack grows down. Thus, to get rid of the parameters, we tin simply add 12 to the stack arrow.
The result produced by myFunc is now available for employ in the register EAX. The values of the caller-saved registers (ECX and EDX), may accept been changed. If the caller uses them later on the phone call, it would take needed to salvage them on the stack before the call and restore them after it.
Callee Rules
The definition of the subroutine should adhere to the following rules at the starting time of the subroutine:
- Push button the value of EBP onto the stack, and and so copy the value of ESP into EBP using the following instructions:
push %ebp mov %esp, %ebp
This initial activity maintains the base pointer, EBP. The base arrow is used by convention every bit a point of reference for finding parameters and local variables on the stack. When a subroutine is executing, the base arrow holds a copy of the stack pointer value from when the subroutine started executing. Parameters and local variables will always be located at known, constant offsets away from the base pointer value. We push the quondam base arrow value at the beginning of the subroutine then that we can afterward restore the appropriate base pointer value for the caller when the subroutine returns. Remember, the caller is not expecting the subroutine to change the value of the base pointer. We and then move the stack arrow into EBP to obtain our signal of reference for accessing parameters and local variables. - Next, allocate local variables past making space on the stack. Retrieve, the stack grows down, so to make space on the meridian of the stack, the stack arrow should exist decremented. The corporeality by which the stack pointer is decremented depends on the number and size of local variables needed. For instance, if 3 local integers (iv bytes each) were required, the stack pointer would need to be decremented by 12 to brand space for these local variables (i.e., sub $12, %esp). Every bit with parameters, local variables volition be located at known offsets from the base pointer.
- Next, relieve the values of the callee-saved registers that will be used past the role. To save registers, push them onto the stack. The callee-saved registers are EBX, EDI, and ESI (ESP and EBP volition besides be preserved by the calling convention, but need non be pushed on the stack during this stride).
Afterward these three actions are performed, the body of the subroutine may proceed. When the subroutine is returns, information technology must follow these steps:
- Leave the return value in EAX.
- Restore the old values of any callee-saved registers (EDI and ESI) that were modified. The annals contents are restored by popping them from the stack. The registers should exist popped in the inverse club that they were pushed.
- Deallocate local variables. The obvious way to do this might exist to add the appropriate value to the stack arrow (since the infinite was allocated by subtracting the needed amount from the stack arrow). In practice, a less error-prone fashion to deallocate the variables is to move the value in the base pointer into the stack pointer: mov %ebp, %esp. This works because the base pointer e'er contains the value that the stack arrow contained immediately prior to the allocation of the local variables.
- Immediately before returning, restore the caller's base of operations pointer value past popping EBP off the stack. Remember that the first thing we did on entry to the subroutine was to push button the base arrow to salvage its old value.
- Finally, return to the caller past executing a ret instruction. This instruction will notice and remove the appropriate render address from the stack.
Note that the callee's rules autumn cleanly into two halves that are basically mirror images of ane another. The first half of the rules use to the beginning of the role, and are normally said to ascertain the prologue to the office. The latter half of the rules utilise to the end of the function, and are thus commonly said to define the epilogue of the function.
Example
Here is an example role definition that follows the callee rules:
/* Kickoff the lawmaking department */ .text /* Define myFunc as a global (exported) function. */ .globl myFunc .blazon myFunc, @function myFunc: /* Subroutine Prologue */ button %ebp /* Salvage the old base pointer value. */ mov %esp, %ebp /* Set the new base pointer value. */ sub $4, %esp /* Make room for one 4-byte local variable. */ push %edi /* Save the values of registers that the function */ push %esi /* will alter. This role uses EDI and ESI. */ /* (no need to save EBX, EBP, or ESP) */ /* Subroutine Body */ mov 8(%ebp), %eax /* Motion value of parameter 1 into EAX. */ mov 12(%ebp), %esi /* Motion value of parameter 2 into ESI. */ mov xvi(%ebp), %edi /* Motility value of parameter 3 into EDI. */ mov %edi, -4(%ebp) /* Move EDI into the local variable. */ add %esi, -4(%ebp) /* Add ESI into the local variable. */ add together -4(%ebp), %eax /* Add together the contents of the local variable */ /* into EAX (final result). */ /* Subroutine Epilogue */ pop %esi /* Recover register values. */ pop %edi mov %ebp, %esp /* Deallocate the local variable. */ pop %ebp /* Restore the caller's base pointer value. */ ret
The subroutine prologue performs the standard actions of saving a snapshot of the stack pointer in EBP (the base pointer), allocating local variables by decrementing the stack pointer, and saving annals values on the stack.
In the body of the subroutine we tin can see the utilise of the base pointer. Both parameters and local variables are located at constant offsets from the base of operations pointer for the elapsing of the subroutines execution. In particular, nosotros observe that since parameters were placed onto the stack before the subroutine was chosen, they are always located beneath the base arrow (i.eastward. at higher addresses) on the stack. The showtime parameter to the subroutine can e'er be establish at retentiveness location (EBP+8), the second at (EBP+12), the third at (EBP+16). Similarly, since local variables are allocated afterwards the base pointer is gear up, they e'er reside above the base of operations pointer (i.e. at lower addresses) on the stack. In particular, the beginning local variable is e'er located at (EBP-4), the 2d at (EBP-eight), and so on. This conventional utilize of the base of operations arrow allows us to speedily identify the use of local variables and parameters within a role body.
The function epilogue is basically a mirror image of the role prologue. The caller'southward register values are recovered from the stack, the local variables are deallocated by resetting the stack pointer, the caller'south base of operations pointer value is recovered, and the ret didactics is used to return to the appropriate code location in the caller.
Credits: This guide was originally created by Adam Ferrari many years ago,
and since updated past Alan Batson, Mike Lack, and Anita Jones.
It was revised for 216 Jump 2006 by David Evans.
It was finally modified past Quentin Carbonneaux to utilise the AT&T syntax for Yale's CS421.
Source: https://flint.cs.yale.edu/cs421/papers/x86-asm/asm.html
0 Response to "How to Read a File With Spaces in Assembly"
Post a Comment