banner



What Is The Value In The Edx Register (In Decimal)

x86 Assembly Guide

Contents: Registers | Memory and Addressing | Instructions | Calling Convention

This is a version adapted by 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 basics of 32-bit x86 assembly language programming, roofing a small but useful subset of the available instructions and assembler directives. At that place are several different associates languages for generating x86 machine code. The one we will use in CS421 is the GNU Assembler (gas) assembler. We will uses the standard AT&T syntax for writing x86 associates code.

The full x86 instruction set is large and complex (Intel's x86 teaching set manuals contain over 2900 pages), and nosotros do not embrace it all in this guide. For example, in that location is a 16-chip subset of the x86 instruction gear up. Using the 16-bit programming model can exist quite complex. Information technology has a segmented retentivity model, more than restrictions on register usage, then on. In this guide, nosotros will limit our attending to more modern aspects of x86 programming, and delve into the didactics set but in plenty item to get a basic feel for x86 programming.

Registers

Modern (i.eastward 386 and beyond) x86 processors have 8 32-fleck general purpose registers, as depicted in Figure ane. The register names are mostly historical. For example, EAX used to be chosen the accumulator since it was used by a number of arithmetic operations, and ECX was known as the counter since it was used to hold a loop index. Whereas well-nigh of the registers have lost their special purposes in the modern teaching set, by convention, two are reserved for special purposes — the stack pointer (ESP) and the base pointer (EBP).

For the EAX, EBX, ECX, and EDX registers, subsections may be used. For example, the least significant 2 bytes of EAX can be treated as a sixteen-bit register called AX. The least pregnant byte of AX tin can be used as a single 8-bit annals called AL, while the most meaning byte of AX tin exist used as a unmarried 8-flake annals called AH. These names refer to the same 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 concur-overs from older, sixteen-bit versions of the instruction ready. However, they are sometimes convenient when dealing with data that are smaller than 32-bits (e.g. 1-byte ASCII characters).


Figure 1. x86 Registers

Memory and Addressing Modes

Declaring Static Data Regions

You can declare static data regions (coordinating to global variables) in x86 associates using special assembler directives for this purpose. Information declarations should be preceded by the .information directive. Following this directive, the directives .byte, .brusk, and .long can be used to declare one, two, and four byte data locations, respectively. To refer to the address of the information created, we tin label them. Labels are very useful and versatile in assembly, they requite names to memory locations that will exist figured out afterwards past the assembler or the linker. This is similar to declaring variables by name, but abides past some lower level rules. For example, locations declared in sequence will be located in memory next to one some other.

Case declarations:

.data
var:
.byte 64 /* Declare a byte, referred to every bit location var, containing the value 64. */
.byte 10 /* Declare a byte with no label, containing the value 10. Its location is var + 1. */
x:
.short 42 /* Declare a 2-byte value initialized to 42, referred to as location x. */
y:
.long 30000 /* Declare a 4-byte value, referred to every bit location y, initialized to 30000. */

Unlike in high level languages where arrays can have many dimensions and are accessed by indices, arrays in x86 assembly language are simply a number of cells located contiguously in retentivity. An array can exist declared by just listing the values, as in the offset example below. For the special case of an array of bytes, string literals can be used. In case a large area of memory is filled with zeroes the .zippo directive tin can be used.

Some examples:

southward:
.long one, ii, 3 /* Declare three iv-byte values, initialized to 1, 2, and three.
The value at location southward + 8 will be iii. */
barr:
.zero 10 /* Declare ten bytes starting at location barr, initialized to 0. */
str:
.cord "how-do-you-do" /* Declare half-dozen bytes starting at the address str initialized to
the ASCII character values for hello followed by a nul (0) byte. */

Addressing Memory

Modern x86-compatible processors are capable of addressing up to two32 bytes of retentiveness: retentivity addresses are 32-$.25 wide. In the examples above, where we used labels to refer to retention regions, these labels are actually replaced by the assembler with 32-bit quantities that specify addresses in memory. In addition to supporting referring to retentivity regions by labels (i.east. constant values), the x86 provides a flexible scheme for computing and referring to memory addresses: upwardly to two of the 32-bit registers and a 32-bit signed constant can exist added together to compute a retention address. I of the registers can be optionally pre-multiplied by ii, 4, or viii.

The addressing modes can be used with many x86 instructions (we'll draw them in the next department). Here we illustrate some examples using the mov instruction that moves data between registers and retentiveness. This didactics has two operands: the first is the source and the second specifies the destination.

Some examples of mov instructions using address computations are:

mov (%ebx), %eax /* Load four bytes from the retentivity address in EBX into EAX. */
mov %ebx, var(,one) /* Move the contents of EBX into the 4 bytes at retention accost var.
(Note, var is a 32-flake constant). */
mov -4(%esi), %eax /* Motion 4 bytes at memory accost ESI + (-4) into EAX. */
mov %cl, (%esi,%eax,1) /* Move the contents of CL into the byte at accost ESI+EAX. */
mov (%esi,%ebx,4), %edx /* Movement the 4 bytes of information at address ESI+4*EBX into EDX. */

Some examples of invalid address calculations include:

mov (%ebx,%ecx,-1), %eax /* Can just add register values. */
mov %ebx, (%eax,%esi,%edi,ane) /* At most 2 registers in address computation. */

Operation Suffixes

In full general, the intended size of the of the information item at a given retentiveness address 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 retentivity regions could exist inferred from the size of the register operand. When we were loading a 32-flake register, the assembler could infer that the region of retention we were referring to was four bytes wide. When we were storing the value of a i byte register to memory, the assembler could infer that we wanted the address to refer to a single byte in retentivity.

However, in some cases the size of a referred-to retentiveness region is ambiguous. Consider the education mov $ii, (%ebx). Should this instruction move the value 2 into the single byte at address EBX? Perhaps it should movement the 32-bit 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, due west, and l serve this purpose, indicating sizes of 1, 2, and 4 bytes respectively.

For example:

movb $2, (%ebx) /* Move 2 into the unmarried byte at the address stored in EBX. */
movw $2, (%ebx) /* Motility the 16-bit integer representation of two into the 2 bytes starting at the address in EBX. */
movl $2, (%ebx) /* Motion the 32-bit integer representation of ii into the 4 bytes starting at the address in EBX. */

Instructions

Machine instructions mostly fall into three categories: data move, arithmetic/logic, and control-flow. In this department, we volition await at of import examples of x86 instructions from each category. This section should not be considered an exhaustive listing of x86 instructions, but rather a useful subset. For a consummate list, see Intel'southward teaching set up reference.

We apply the post-obit notation:

<reg32> Any 32-flake register (%eax, %ebx, %ecx, %edx, %esi, %edi, %esp, or %ebp)
<reg16> Any 16-bit register (%ax, %bx, %cx, or %dx)
<reg8> Whatsoever 8-bit register (%ah, %bh, %ch, %dh, %al, %bl, %cl, or %dl)
<reg> Whatever register
<mem> A memory accost (e.thousand., (%eax), four+var(,ane), or (%eax,%ebx,ane))
<con32> Any 32-bit firsthand
<con16> Whatsoever 16-bit firsthand
<con8> Any 8-bit immediate
<con> Whatsoever eight-, 16-, or 32-bit immediate

In assembly language, all the labels and numeric constants used equally immediate operands (i.e. non in an accost adding like 3(%eax,%ebx,eight)) are always prefixed past a dollar sign. When needed, hexadecimal note can exist used with the 0x prefix (eastward.m. $0xABC). Without the prefix, numbers are interpreted in the decimal basis.

Data Move Instructions

mov — Move

The mov instruction copies the data detail referred to by its first operand (i.e. register contents, retentivity contents, or a constant value) into the location referred to past its second operand (i.due east. a register or memory). While annals-to-register moves are possible, direct memory-to-retentiveness moves are non. In cases where retentiveness transfers are desired, the source memory contents must first be loaded into a register, then tin 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 $5, var(,one) — store the value 5 into the byte at location var

push — Push on stack

The push pedagogy places its operand onto the top of the hardware supported stack in retentiveness. Specifically, button showtime decrements ESP by 4, then places its operand into the contents of the 32-bit location at accost (%esp). ESP (the stack arrow) is decremented by button since the x86 stack grows downwardly — i.eastward. the stack grows from high addresses to lower addresses.

Syntax
push <reg32>
push <mem>
button <con32>

Examples
button %eax — push button eax on the stack
button var(,one) — push the 4 bytes at accost var onto the stack

pop — Popular from stack

The pop instruction removes the 4-byte data element from the superlative of the hardware-supported stack into the specified operand (i.eastward. register or memory location). Information technology first moves the iv bytes located at memory location (%esp) into the specified annals or memory location, and and so increments ESP by 4.

Syntax
popular <reg32>
pop <mem>

Examples
pop %edi — pop the acme element of the stack into EDI.
pop (%ebx) — pop the tiptop element of the stack into retentiveness at the four bytes starting at location EBX.

lea — Load effective address

The lea instruction places the address specified by its first operand into the annals specified by its second operand. Note, the contents of the retentiveness location are not loaded, just the effective address is computed and placed into the register. This is useful for obtaining a pointer into a retention region or to perform simple arithmetics 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 addition

The add together teaching adds together its 2 operands, storing the upshot in its second operand. Note, whereas both operands may be registers, at most ane operand may exist a memory location.

Syntax
add together <reg>, <reg>
add <mem>, <reg>
add together <reg>, <mem>
add <con>, <reg>
add <con>, <mem>

Examples
add together $10, %eax — EAX is set to EAX + x
addb $ten, (%eax) — add x to the single byte stored at memory accost stored in EAX

sub — Integer subtraction

The sub educational activity stores in the value of its 2d operand the outcome of subtracting the value of its first operand from the value of its second operand. As with add, whereas both operands may be registers, at nigh one operand may exist 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 set to AL - AH
sub $216, %eax — subtract 216 from the value stored in EAX

inc, dec — Increment, Decrement

The inc instruction increments the contents of its operand past one. The december instruction decrements the contents of its operand past i.

Syntax
inc <reg>
inc <mem>
dec <reg>
dec <mem>

Examples
dec %eax — subtract i from the contents of EAX
incl var(,1) — add one to the 32-chip integer stored at location var

imul — Integer multiplication

The imul education has two basic formats: two-operand (first two syntax listings higher up) and three-operand (last two syntax listings above).

The 2-operand form multiplies its two operands together and stores the consequence in the 2nd operand. The upshot (i.e. second) operand must be a register.

The iii operand form multiplies its 2d and third operands together and stores the issue in its last operand. Once again, the outcome operand must be a register. Furthermore, the first operand is restricted to beingness 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 retentivity at location EBX. Store the event in EAX.

imul $25, %edi, %esi — ESI is set up to EDI * 25

idiv — Integer division

The idiv instruction divides the contents of the 64 flake integer EDX:EAX (constructed by viewing EDX as the most significant iv bytes and EAX equally the least significant four bytes) by the specified operand value. The quotient result of the partition is stored into EAX, while the remainder is placed in EDX.

Syntax
idiv <reg32>
idiv <mem>

Examples

idiv %ebx — split the contents of EDX:EAX by the contents of EBX. Place the quotient in EAX and the remainder in EDX.

idivw (%ebx) — divide the contents of EDX:EAS by the 32-bit value stored at the memory location in EBX. Place the quotient in EAX and the remainder in EDX.

and, or, xor — Bitwise logical and, or, and exclusive or

These instructions perform the specified logical performance (logical bitwise and, or, and exclusive or, respectively) on their operands, placing the consequence in the first 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 merely the last 4 bits of EAX.
xor %edx, %edx — set the contents of EDX to zero.

not — Bitwise logical non

Logically negates the operand contents (that is, flips all bit values in the operand).

Syntax
non <reg>
non <mem>

Example
not %eax — flip all the bits of EAX

neg — Negate

Performs the two'south complement negation of the operand contents.

Syntax
neg <reg>
neg <mem>

Example
neg %eax — EAX is set to (- EAX)

shl, shr — Shift left and correct

These instructions shift the bits in their first operand's contents left and right, padding the resulting empty bit positions with zeros. The shifted operand can be shifted up to 31 places. The number of $.25 to shift is specified past the second operand, which can exist either an 8-bit constant or the register CL. In either example, shifts counts of greater and 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 $1, eax — Multiply the value of EAX by ii (if the most significant bit is 0)

shr %cl, %ebx — Store in EBX the floor of issue of dividing the value of EBX by 2 n where north is the value in CL. Caution: for negative integers, it is different from the C semantics of division!

Command Menstruation Instructions

The x86 processor maintains an instruction pointer (EIP) register that is a 32-bit value indicating the location in memory where the electric current educational activity starts. Normally, it increments to indicate to the next instruction in memory begins after execution an education. The EIP register cannot be manipulated directly, merely is updated implicitly by provided control catamenia instructions.

We use the note <label> to refer to labeled locations in the program text. Labels can be inserted anywhere in x86 assembly code text past entering a label proper name followed by a colon. For example,

            mov 8(%ebp), %esi begin:        xor %ecx, %ecx        mov (%esi), %eax          

The second instruction in this code fragment is labeled brainstorm. Elsewhere in the lawmaking, nosotros can refer to the memory location that this teaching is located at in memory using the more convenient symbolic name brainstorm. This label is just a user-friendly way of expressing the location instead of its 32-bit value.

jmp — Jump

Transfers program control menstruum to the didactics at the retention location indicated by the operand.

Syntax
jmp <characterization>

Case
jmp begin — Jump to the instruction labeled begin.

jcondition — Conditional jump

These instructions are conditional jumps that are based on the status of a gear up of condition codes that are stored in a special register chosen the motorcar status word. The contents of the automobile status word include information virtually the last arithmetic performance performed. For case, i bit of this word indicates if the last result was naught. 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 educational activity performs a jump to the specified operand label if the result of the last arithmetic operation was zero. Otherwise, command proceeds to the next instruction in sequence.

A number of the conditional branches are given names that are intuitively based on the last operation performed being a special compare pedagogy, cmp (see below). For instance, conditional branches such as jle and jne are based on first performing a cmp functioning on the desired operands.

Syntax
je <label> (jump when equal)
jne <characterization> (jump when not equal)
jz <label> (jump when last outcome was zero)
jg <label> (jump when greater than)
jge <label> (jump when greater than or equal to)
jl <label> (jump when less than)
jle <label> (spring when less than or equal to)

Example

cmp %ebx, %eax jle washed          

If the contents of EAX are less than or equal to the contents of EBX, jump to the characterization washed. Otherwise, go on to the next instruction.

cmp — Compare

Compare the values of the two specified operands, setting the status codes in the machine status word appropriately. This instruction is equivalent to the sub instruction, except the consequence of the subtraction is discarded instead of replacing the first operand.

Syntax
cmp <reg>, <reg>
cmp <mem>, <reg>
cmp <reg>, <mem>
cmp <con>, <reg>

Example
cmpb $ten, (%ebx)
jeq loop

If the byte stored at the retentiveness 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 telephone call and render. The call instruction showtime pushes the current lawmaking location onto the hardware supported stack in memory (see the push button instruction for details), and and so performs an unconditional jump to the code location indicated by the label operand. Different the simple jump instructions, the telephone call instruction saves the location to render to when the subroutine completes.

The ret instruction implements a subroutine return mechanism. This instruction kickoff pops a code location off the hardware supported in-retentiveness stack (see the pop didactics for details). It then performs an unconditional jump to the retrieved code location.

Syntax
call <label>
ret

Calling Convention

To allow carve up programmers to share lawmaking and develop libraries for employ 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 almost how to phone call and return from routines. For example, given a set of calling convention rules, a developer 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, loftier-level language compilers can be made to follow the rules, thus assuasive mitt-coded assembly language routines and high-level language routines to call one another.

In practice, many calling conventions are possible. We will describe the widely used C linguistic communication calling convention. Following this convention will allow you lot to write assembly language subroutines that are safely callable from C (and C++) lawmaking, and will also enable you to telephone call C library functions from your assembly linguistic communication lawmaking.

The C calling convention is based heavily on the use of the hardware-supported stack. It is based on the push, pop, phone 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 memory on the stack. The vast majority of high-level procedural languages implemented on near processors have used similar calling conventions.

The calling convention is broken into ii sets of rules. The first set of rules is employed by the caller of the subroutine, and the 2nd fix of rules is observed by the writer of the subroutine (the callee). Information technology should be emphasized that mistakes in the observance of these rules quickly consequence in fatal program errors since the stack volition be left in an inconsistent state; thus meticulous care should be used when implementing the call convention in your own subroutines.


Stack during Subroutine Call

[Thank you to James Peterson for finding and fixing the bug in the original version of this effigy!]

A proficient way to visualize the operation of the calling convention is to describe the contents of the nearby region of the stack during subroutine execution. The epitome above depicts the contents of the stack during the execution of a subroutine with three parameters and 3 local variables. The cells depicted in the stack are 32-bit wide retentiveness locations, thus the retentiveness addresses of the cells are 4 bytes apart. The showtime parameter resides at an offset of viii bytes from the base arrow. 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 offset from the base pointer to the starting time parameter. When the ret pedagogy 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:

  1. 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 (so they can be restore after the subroutine returns.
  2. To laissez passer parameters to the subroutine, push them onto the stack before the call. The parameters should be pushed in inverted order (i.due east. last parameter offset). Since the stack grows downwardly, the beginning 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).
  3. To call the subroutine, utilize the telephone call instruction. This educational activity places the return address on pinnacle of the parameters on the stack, and branches to the subroutine code. This invokes the subroutine, which should follow the callee rules below.

Afterwards the subroutine returns (immediately post-obit the call instruction), the caller can await to find the return value of the subroutine in the register EAX. To restore the machine state, the caller should:

  1. Remove the parameters from stack. This restores the stack to its state earlier the call was performed.
  2. Restore the contents of caller-saved registers (EAX, ECX, EDX) by popping them off of the stack. The caller tin can assume that no other registers were modified by the subroutine.

Example

The code below shows a function 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 abiding 216; the third parameter is in the memory location stored in EBX.

push (%ebx)    /* Push last parameter beginning */ push $216      /* Push button the second parameter */ button %eax      /* Push first parameter last */  call myFunc    /* Call the function (assume C naming) */  add $12, %esp          

Note that after the call returns, the caller cleans upwards the stack using the add pedagogy. Nosotros have 12 bytes (three parameters * 4 bytes each) on the stack, and the stack grows downwardly. Thus, to go rid of the parameters, nosotros tin simply add together 12 to the stack pointer.

The result produced by myFunc is at present available for apply in the annals EAX. The values of the caller-saved registers (ECX and EDX), may accept been changed. If the caller uses them afterwards the phone call, it would have needed to save them on the stack earlier the call and restore them after information technology.

Callee Rules

The definition of the subroutine should adhere to the following rules at the starting time of the subroutine:

  1. Push the value of EBP onto the stack, and then copy the value of ESP into EBP using the following instructions:
                  push %ebp     mov  %esp, %ebp            
    This initial activeness maintains the base arrow, EBP. The base arrow is used by convention as a signal of reference for finding parameters and local variables on the stack. When a subroutine is executing, the base pointer holds a re-create of the stack pointer value from when the subroutine started executing. Parameters and local variables will ever be located at known, constant offsets abroad from the base pointer value. We push the quondam base arrow value at the first of the subroutine so that we tin can later restore the appropriate base pointer value for the caller when the subroutine returns. Call back, the caller is not expecting the subroutine to change the value of the base of operations pointer. We so move the stack pointer into EBP to obtain our indicate of reference for accessing parameters and local variables.
  2. Next, allocate local variables by making space on the stack. Recall, the stack grows down, so to make space on the pinnacle of the stack, the stack pointer should exist decremented. The amount past which the stack pointer is decremented depends on the number and size of local variables needed. For example, if 3 local integers (iv bytes each) were required, the stack arrow would need to exist decremented by 12 to brand infinite for these local variables (i.due east., sub $12, %esp). As with parameters, local variables will be located at known offsets from the base of operations arrow.
  3. Adjacent, relieve the values of the callee-saved registers that volition be used by the role. To save registers, push them onto the stack. The callee-saved registers are EBX, EDI, and ESI (ESP and EBP volition also exist preserved by the calling convention, but need not exist pushed on the stack during this stride).

Subsequently these three actions are performed, the trunk of the subroutine may go along. When the subroutine is returns, it must follow these steps:

  1. Go out the return value in EAX.
  2. Restore the one-time values of any callee-saved registers (EDI and ESI) that were modified. The register contents are restored by popping them from the stack. The registers should be popped in the inverse social club that they were pushed.
  3. Deallocate local variables. The obvious way to practice this might be to add the appropriate value to the stack arrow (since the space was allocated by subtracting the needed corporeality from the stack pointer). In practice, a less mistake-prone way to deallocate the variables is to movement the value in the base pointer into the stack pointer: mov %ebp, %esp. This works because the base of operations pointer always contains the value that the stack pointer contained immediately prior to the allocation of the local variables.
  4. Immediately earlier returning, restore the caller's base pointer value by popping EBP off the stack. Recall that the first matter we did on entry to the subroutine was to push button the base arrow to save its one-time value.
  5. Finally, return to the caller by executing a ret instruction. This pedagogy volition find and remove the appropriate return address from the stack.

Note that the callee'south rules fall cleanly into two halves that are basically mirror images of 1 another. The first one-half of the rules employ to the beginning of the function, and are normally said to define the prologue to the function. The latter half of the rules utilise to the terminate of the office, and are thus commonly said to define the epilogue of the function.

Example

Here is an example function definition that follows the callee rules:

            /* First the code department */   .text    /* Define myFunc as a global (exported) function. */   .globl myFunc   .type myFunc, @office myFunc:    /* Subroutine Prologue */   push %ebp      /* Salve the erstwhile base of operations arrow value. */   mov %esp, %ebp /* Set up the new base pointer value. */   sub $4, %esp   /* Make room for ane 4-byte local variable. */   push %edi      /* Save the values of registers that the function */   push %esi      /* will alter. This office uses EDI and ESI. */   /* (no need to save EBX, EBP, or ESP) */    /* Subroutine Body */   mov eight(%ebp), %eax   /* Movement value of parameter 1 into EAX. */   mov 12(%ebp), %esi  /* Motility value of parameter 2 into ESI. */   mov xvi(%ebp), %edi  /* Move value of parameter 3 into EDI. */    mov %edi, -4(%ebp)  /* Move EDI into the local variable. */   add %esi, -four(%ebp)  /* Add ESI into the local variable. */   add -4(%ebp), %eax  /* Add the contents of the local variable */                       /* into EAX (final effect). */    /* Subroutine Epilogue */   popular %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 deportment of saving a snapshot of the stack pointer in EBP (the base pointer), allocating local variables by decrementing the stack pointer, and saving register values on the stack.

In the body of the subroutine we can see the use of the base arrow. Both parameters and local variables are located at abiding offsets from the base arrow for the duration of the subroutines execution. In detail, nosotros notice that since parameters were placed onto the stack before the subroutine was called, they are always located below the base of operations pointer (i.e. at higher addresses) on the stack. The first parameter to the subroutine can e'er be found at retentiveness location (EBP+8), the second at (EBP+12), the third at (EBP+xvi). Similarly, since local variables are allocated later on the base pointer is set up, they always reside higher up the base of operations pointer (i.e. at lower addresses) on the stack. In item, the showtime local variable is always located at (EBP-iv), the second at (EBP-viii), so on. This conventional use of the base pointer allows us to quickly identify the use of local variables and parameters inside a function body.

The function epilogue is basically a mirror epitome of the function prologue. The caller's annals values are recovered from the stack, the local variables are deallocated by resetting the stack pointer, the caller's base pointer value is recovered, and the ret instruction 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 Spring 2006 by David Evans.
It was finally modified by Quentin Carbonneaux to use the AT&T syntax for Yale's CS421.

What Is The Value In The Edx Register (In Decimal),

Source: https://flint.cs.yale.edu/cs421/papers/x86-asm/asm.html

Posted by: corralthiphrisity.blogspot.com

0 Response to "What Is The Value In The Edx Register (In Decimal)"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel