banner



What Assembly Chip Stores A Number To Move Into A Register

x86 Associates Guide

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

This guide describes the nuts of 32-bit x86 assembly linguistic communication programming, roofing a modest simply useful subset of the available instructions and assembler directives. There are several different assembly languages for generating x86 machine code. The one we volition utilise in CS216 is the Microsoft Macro Assembler (MASM) assembler. MASM uses the standard Intel syntax for writing x86 assembly code.

The full x86 instruction set is big and complex (Intel'due south x86 pedagogy set manuals incorporate over 2900 pages), and we practice non cover it all in this guide. For instance, at that place is a 16-bit subset of the x86 didactics gear up. Using the 16-bit programming model can be quite circuitous. Information technology has a segmented memory model, more restrictions on register usage, then on. In this guide, we will limit our attention to more than modern aspects of x86 programming, and delve into the instruction set simply in enough detail to go a basic feel for x86 programming.

Resource

  • Guide to Using Assembly in Visual Studio — a tutorial on edifice and debugging assembly code in Visual Studio
  • Intel x86 Didactics Gear up Reference
  • Intel'south Pentium Manuals (the full gory details)

Registers

Modern (i.e 386 and beyond) x86 processors have eight 32-bit general purpose registers, equally depicted in Figure ane. The annals names are mostly historical. For example, EAX used to exist chosen the accumulator since it was used by a number of arithmetic operations, and ECX was known equally the counter since it was used to hold a loop alphabetize. Whereas almost of the registers accept lost their special purposes in the modern instruction prepare, 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 to the lowest degree pregnant 2 bytes of EAX can be treated as a 16-scrap register called AX. The least significant byte of AX tin be used every bit a unmarried viii-bit register called AL, while the most significant byte of AX can be used as a single eight-bit annals chosen 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 hold-overs from older, 16-chip versions of the pedagogy prepare. Nevertheless, they are sometimes user-friendly when dealing with data that are smaller than 32-bits (east.g. 1-byte ASCII characters).

When referring to registers in associates language, the names are non case-sensitive. For example, the names EAX and eax refer to the same register.


Figure 1. x86 Registers

Memory and Addressing Modes

Declaring Static Data Regions

You can declare static data regions (analogous to global variables) in x86 assembly using special assembler directives for this purpose. Data declarations should be preceded by the .Data directive. Following this directive, the directives DB, DW, and DD tin be used to declare one, two, and iv byte information locations, respectively. Alleged locations can exist labeled with names for later reference — this is like to declaring variables past name, but abides by some lower level rules. For example, locations declared in sequence will be located in memory next to ane some other.

Instance declarations:

.DATA
var DB 64 ; Declare a byte, referred to as location var, containing the value 64.
var2 DB ? ; Declare an uninitialized byte, referred to every bit location var2.
DB 10 ; Declare a byte with no label, containing the value x. Its location is var2 + one.
Ten DW ? ; Declare a 2-byte uninitialized value, referred to every bit location X.
Y DD 30000 ; Declare a iv-byte value, referred to as 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 memory. An array tin can be declared past but listing the values, as in the starting time case below. Two other common methods used for declaring arrays of data are the DUP directive and the use of string literals. The DUP directive tells the assembler to duplicate an expression a given number of times. For instance, iv DUP(2) is equivalent to two, 2, two, 2.

Some examples:

Z DD i, 2, 3 ; Declare iii four-byte values, initialized to 1, 2, and 3. The value of location Z + viii will be 3.
bytes DB 10 DUP(?) ; Declare x uninitialized bytes starting at location bytes.
arr DD 100 DUP(0) ; Declare 100 4-byte words starting at location arr, all initialized to 0
str DB 'hello',0 ; Declare 6 bytes starting at the address str, initialized to the ASCII character values for hello and the cypher (0) byte.

Addressing Memory

Modernistic x86-uniform processors are capable of addressing upwardly to 232 bytes of memory: retentivity addresses are 32-bits wide. 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 past labels (i.e. abiding values), the x86 provides a flexible scheme for calculating and referring to memory addresses: up to two of the 32-bit registers and a 32-bit signed constant tin can be added together to compute a memory address. One of the registers can be optionally pre-multiplied by 2, iv, or 8.

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

Some examples of mov instructions using address computations are:

mov eax, [ebx] ; Move the iv bytes in memory at the address contained in EBX into EAX
mov [var], ebx ; Move the contents of EBX into the 4 bytes at retention accost var. (Note, var is a 32-bit constant).
mov eax, [esi-4] ; Move 4 bytes at memory address ESI + (-4) into EAX
mov [esi+eax], cl ; Move the contents of CL into the byte at address ESI+EAX
mov edx, [esi+4*ebx] ; Move the four bytes of data at address ESI+4*EBX into EDX

Some examples of invalid accost calculations include:

mov eax, [ebx-ecx] ; Tin can only add register values
mov [eax+esi+edi], ebx ; At well-nigh two registers in address computation

Size Directives

In general, the intended size of the data item at a given retention address tin can be inferred from the assembly code instruction in which information technology is referenced. For example, in all of the higher up instructions, the size of the memory regions could be inferred from the size of the register operand. When we were loading a 32-chip register, the assembler could infer that the region of retentiveness we were referring to was four bytes wide. When we were storing the value of a one byte register to retention, the assembler could infer that we wanted the accost to refer to a single byte in memory.

Notwithstanding, in some cases the size of a referred-to memory region is ambiguous. Consider the education mov [ebx], two. Should this instruction motility the value 2 into the single byte at accost EBX? Perhaps it should move the 32-chip integer representation of 2 into the 4-bytes starting at address EBX. Since either is a valid possible interpretation, the assembler must exist explicitly directed as to which is right. The size directives BYTE PTR, WORD PTR, and DWORD PTR serve this purpose, indicating sizes of 1, 2, and 4 bytes respectively.

For example:

mov BYTE PTR [ebx], 2 ; Move 2 into the single byte at the address stored in EBX.
mov WORD PTR [ebx], 2 ; Move the 16-bit integer representation of 2 into the 2 bytes starting at the address in EBX.
mov DWORD PTR [ebx], two ; Move the 32-bit integer representation of ii into the iv bytes starting at the address in EBX.

Instructions

Machine instructions by and large autumn into three categories: information movement, arithmetics/logic, and control-menses. In this section, we will wait at important examples of x86 instructions from each category. This section should not be considered an exhaustive listing of x86 instructions, merely rather a useful subset. For a complete list, see Intel's instruction set reference.

Nosotros use the following notation:

<reg32> Any 32-bit register (EAX, EBX, ECX, EDX, ESI, EDI, ESP, or EBP)
<reg16> Any xvi-bit annals (AX, BX, CX, or DX)
<reg8> Any eight-fleck register (AH, BH, CH, DH, AL, BL, CL, or DL)
<reg> Any register
<mem> A memory address (east.thousand., [eax], [var + 4], or dword ptr [eax+ebx])
<con32> Any 32-chip constant
<con16> Whatsoever xvi-bit constant
<con8> Whatsoever eight-bit abiding
<con> Any 8-, xvi-, or 32-chip abiding

Data Movement Instructions

mov — Move (Opcodes: 88, 89, 8A, 8B, 8C, 8E, ...)

The mov instruction copies the data particular referred to past its 2nd operand (i.e. register contents, memory contents, or a constant value) into the location referred to by its first operand (i.e. a register or retentiveness). While register-to-register moves are possible, direct memory-to-retentiveness moves are non. In cases where memory transfers are desired, the source memory contents must first exist loaded into a register, so tin be stored to the destination memory address.

Syntax
mov <reg>,<reg>
mov <reg>,<mem>
mov <mem>,<reg>
mov <reg>,<const>
mov <mem>,<const>

Examples
mov eax, ebx — copy the value in ebx into eax
mov byte ptr [var], 5 — store the value 5 into the byte at location var

push — Push stack (Opcodes: FF, 89, 8A, 8B, 8C, 8E, ...)

The push instruction places its operand onto the pinnacle of the hardware supported stack in retentivity. Specifically, push starting time decrements ESP by four, then places its operand into the contents of the 32-fleck location at address [ESP]. ESP (the stack arrow) 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>
push <mem>
push <con32>

Examples
button eax — button eax on the stack
push [var] — push the four bytes at address var onto the stack

pop — Pop stack

The pop instruction removes the four-byte data element from the top of the hardware-supported stack into the specified operand (i.e. annals or retentiveness location). Information technology starting time moves the 4 bytes located at retention location [SP] into the specified register or memory location, and then increments SP by iv.

Syntax
pop <reg32>
pop <mem>

Examples
pop edi — pop the height element of the stack into EDI.
pop [ebx] — pop the acme chemical element of the stack into retentiveness at the four bytes starting at location EBX.

lea — Load constructive accost

The lea instruction places the accost specified by its second operand into the register specified by its first operand. Note, the contents of the memory location are not loaded, just the effective accost is computed and placed into the register. This is useful for obtaining a arrow into a retentiveness region.

Syntax
lea <reg32>,<mem>

Examples
lea edi, [ebx+4*esi] — the quantity EBX+4*ESI is placed in EDI.
lea eax, [var] — the value in var is placed in EAX.
lea eax, [val] — the value val is placed in EAX.

Arithmetic and Logic Instructions

add together — Integer Addition

The add instruction adds together its two operands, storing the consequence in its showtime operand. Note, whereas both operands may be registers, at almost one operand may be a memory location. Syntax
add together <reg>,<reg>
add together <reg>,<mem>
add <mem>,<reg>
add <reg>,<con>
add <mem>,<con>
Examples
add eax, 10 — EAX ← EAX + 10
add together BYTE PTR [var], 10 — add together 10 to the single byte stored at memory address var

sub — Integer Subtraction

The sub instruction stores in the value of its first operand the event of subtracting the value of its second operand from the value of its first operand. Every bit with add together Syntax
sub <reg>,<reg>
sub <reg>,<mem>
sub <mem>,<reg>
sub <reg>,<con>
sub <mem>,<con>
Examples
sub al, ah — AL ← AL - AH
sub eax, 216 — subtract 216 from the value stored in EAX

inc, december — Increment, Decrement

The inc instruction increments the contents of its operand by one. The december educational activity decrements the contents of its operand by 1.

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

Examples
dec eax — subtract i from the contents of EAX.
inc DWORD PTR [var] — add one to the 32-bit integer stored at location var

imul — Integer Multiplication

The imul pedagogy has two basic formats: two-operand (first two syntax listings above) and iii-operand (terminal two syntax listings to a higher place). The two-operand form multiplies its ii operands together and stores the issue in the first operand. The event (i.e. first) operand must be a register. The three operand form multiplies its second and 3rd operands together and stores the event in its first operand. Again, the effect operand must be a register. Furthermore, the third operand is restricted to existence a constant value. Syntax
imul <reg32>,<reg32>
imul <reg32>,<mem>
imul <reg32>,<reg32>,<con>
imul <reg32>,<mem>,<con>

Examples

imul eax, [var] — multiply the contents of EAX by the 32-bit contents of the memory location var. Shop the result in EAX.

imul esi, edi, 25 — ESI → EDI * 25

idiv — Integer Division

The idiv instruction divides the contents of the 64 fleck integer EDX:EAX (synthetic by viewing EDX every bit the most pregnant four bytes and EAX equally the least pregnant four bytes) by the specified operand value. The quotient result of the sectionalisation is stored into EAX, while the remainder is placed in EDX.

Syntax
idiv <reg32>
idiv <mem>

Examples

idiv ebx — divide the contents of EDX:EAX past the contents of EBX. Identify the quotient in EAX and the remainder in EDX.

idiv DWORD PTR [var] — divide the contents of EDX:EAX by the 32-bit value stored at retention location var. Identify 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 functioning (logical bitwise and, or, and exclusive or, respectively) on their operands, placing the result in the starting time operand location.

Syntax
and <reg>,<reg>
and <reg>,<mem>
and <mem>,<reg>
and <reg>,<con>
and <mem>,<con>

or <reg>,<reg>
or <reg>,<mem>
or <mem>,<reg>
or <reg>,<con>
or <mem>,<con>

xor <reg>,<reg>
xor <reg>,<mem>
xor <mem>,<reg>
xor <reg>,<con>
xor <mem>,<con>

Examples
and eax, 0fH — clear all but the terminal 4 $.25 of EAX.
xor edx, edx — set the contents of EDX to goose egg.

not — Bitwise Logical Not

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

Syntax
not <reg>
non <mem>

Instance
not BYTE PTR [var] — negate all bits in the byte at the memory location var.

neg — Negate

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

Syntax
neg <reg>
neg <mem>

Instance
neg eax — EAX → - EAX

shl, shr — Shift Left, Shift Right

These instructions shift the bits in their starting time operand's contents left and correct, padding the resulting empty flake positions with zeros. The shifted operand can be shifted upwardly to 31 places. The number of bits to shift is specified by the second operand, which tin can be either an 8-bit constant or the register CL. In either example, shifts counts of greater so 31 are performed modulo 32.

Syntax
shl <reg>,<con8>
shl <mem>,<con8>
shl <reg>,<cl>
shl <mem>,<cl>

shr <reg>,<con8>
shr <mem>,<con8>
shr <reg>,<cl>
shr <mem>,<cl>

Examples

shl eax, 1 — Multiply the value of EAX by 2 (if the well-nigh pregnant scrap is 0)

shr ebx, cl — Store in EBX the flooring of consequence of dividing the value of EBX past ii due north wheren is the value in CL.

Command Menstruum Instructions

The x86 processor maintains an pedagogy pointer (IP) annals that is a 32-bit value indicating the location in memory where the current instruction starts. Usually, information technology increments to point to the next instruction in memory begins later execution an pedagogy. The IP register cannot exist manipulated directly, but is updated implicitly by provided command menses instructions.

We utilise the notation <characterization> to refer to labeled locations in the program text. Labels can be inserted anywhere in x86 assembly lawmaking text by entering a label name followed past a colon. For instance,

            mov esi, [ebp+8] begin: xor ecx, ecx        mov eax, [esi]          

The 2nd didactics in this code fragment is labeled begin. Elsewhere in the code, we can refer to the retentivity location that this instruction is located at in memory using the more user-friendly symbolic name begin. This label is just a convenient way of expressing the location instead of its 32-bit value.

jmp — Jump

Transfers program command flow to the instruction at the memory location indicated by the operand.

Syntax
jmp <label>

Case
jmp begin — Leap to the didactics labeled brainstorm.

jcondition — Provisional Spring

These instructions are conditional jumps that are based on the condition of a set of condition codes that are stored in a special register chosen the automobile status word. The contents of the automobile condition word include information nearly the final arithmetic operation performed. For example, i bit of this word indicates if the last result was zero. Some other indicates if the terminal result was negative. Based on these condition codes, a number of conditional jumps tin can be performed. For example, the jz instruction performs a jump to the specified operand characterization if the upshot of the last arithmetics operation was null. Otherwise, control proceeds to the next instruction 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 (run into below). For example, conditional branches such equally jle and jne are based on outset performing a cmp operation on the desired operands.

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

Instance
cmp eax, ebx
jle done

If the contents of EAX are less than or equal to the contents of EBX, jump to the label washed. Otherwise, continue to the adjacent didactics.

cmp — Compare

Compare the values of the ii specified operands, setting the condition codes in the motorcar status give-and-take appropriately. This teaching is equivalent to the sub didactics, except the result of the subtraction is discarded instead of replacing the first operand.

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

Case
cmp DWORD PTR [var], x
jeq loop

If the iv bytes stored at location var are equal to the four-byte integer constant ten, leap to the location labeled loop.

call, ret — Subroutine call and render

These instructions implement a subroutine call and render. The phone call education first pushes the current lawmaking location onto the hardware supported stack in retentiveness (see the push instruction for details), and and so performs an unconditional leap to the code location indicated by the label operand. Unlike the simple jump instructions, the call instruction saves the location to return to when the subroutine completes.

The ret didactics implements a subroutine return mechanism. This educational activity first pops a code location off the hardware supported in-memory stack (meet the pop instruction for details). Information technology then performs an unconditional jump to the retrieved code location.

Syntax
telephone call <label>
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 example, 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 fix of calling convention rules, high-level linguistic communication compilers tin be made to follow the rules, thus allowing manus-coded assembly language routines and high-level language routines to telephone call i another.

In practice, many calling conventions are possible. Nosotros will utilise the widely used C language calling convention. Following this convention will allow y'all to write assembly language subroutines that are safely callable from C (and C++) code, and will too enable you to call C library functions from your associates linguistic communication code.

The C calling convention is based heavily on the employ of the hardware-supported stack. It is based on the push, pop, 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 most processors have used similar calling conventions.

The calling convention is broken into two sets of rules. The beginning set of rules is employed by the caller of the subroutine, and the 2nd ready of rules is observed past the author of the subroutine (the callee). It should be emphasized that mistakes in the observance of these rules chop-chop upshot in fatal program errors since the stack will exist left in an inconsistent land; thus meticulous care should be used when implementing the call convention in your own subroutines.

>
Stack during Subroutine Call
[Thank you to Maxence Faldor for providing a correct effigy and to James Peterson for finding and fixing the problems in the original version of this effigy!]

A proficient 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 broad memory locations, thus the memory addresses of the cells are 4 bytes apart. The first parameter resides at an offset of 8 bytes from the base pointer. In a higher place the parameters on the stack (and below the base pointer), the phone call instruction placed the render accost, thus leading to an extra 4 bytes of kickoff from the base pointer to the offset parameter. When the ret pedagogy is used to return from the subroutine, information technology volition jump to the return accost stored on the stack.

Caller Rules

To brand a subrouting call, the caller should:

  1. Before calling a subroutine, the caller should save the contents of sure registers that are designated caller-saved. The caller-saved registers are EAX, ECX, EDX. Since the chosen 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 pass parameters to the subroutine, push them onto the stack before the call. The parameters should be pushed in inverted club (i.e. last parameter first). Since the stack grows down, the first parameter will exist stored at the lowest accost (this inversion of parameters was historically used to allow functions to be passed a variable number of parameters).
  3. To call the subroutine, use the telephone call instruction. This teaching places the return address on tiptop of the parameters on the stack, and branches to the subroutine code. This invokes the subroutine, which should follow the callee rules below.

After the subroutine returns (immediately following the call instruction), the caller can await to find the return value of the subroutine in the register EAX. To restore the auto state, the caller should:

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

Instance
The code below shows a function call that follows the caller rules. The caller is calling a function _myFunc that takes 3 integer parameters. First parameter is in EAX, the 2d parameter is the constant 216; the third parameter is in memory location var.

push [var] ; Button last parameter outset push 216   ; Push the second parameter push eax   ; Push start parameter last  call _myFunc ; Call the function (presume C naming)  add esp, 12          

Annotation that after the call returns, the caller cleans upwards the stack using the add didactics. We have 12 bytes (iii parameters * 4 bytes each) on the stack, and the stack grows down. Thus, to get rid of the parameters, we tin can but add 12 to the stack arrow.

The outcome produced past _myFunc is now available for use in the register EAX. The values of the caller-saved registers (ECX and EDX), may take been changed. If the caller uses them after the call, information technology would have needed to save 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 beginning of the subroutine:

  1. Push the value of EBP onto the stack, and so copy the value of ESP into EBP using the following instructions:
                  push button ebp     mov  ebp, esp            
    This initial action maintains the base pointer, EBP. The base arrow is used past convention as a betoken of reference for finding parameters and local variables on the stack. When a subroutine is executing, the base pointer holds a copy of the stack pointer value from when the subroutine started executing. Parameters and local variables will always exist located at known, constant offsets abroad from the base arrow value. We push the old base arrow value at the beginning of the subroutine so that we tin can later on 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. Nosotros then movement the stack pointer into EBP to obtain our point of reference for accessing parameters and local variables.
  2. Next, allocate local variables by making space on the stack. Think, the stack grows down, then to make space on the tiptop of the stack, the stack arrow should be decremented. The amount by which the stack pointer is decremented depends on the number and size of local variables needed. For example, if 3 local integers (4 bytes each) were required, the stack pointer would need to be decremented by 12 to brand infinite for these local variables (i.e., sub esp, 12). Equally with parameters, local variables will be located at known offsets from the base of operations pointer.
  3. Side by side, save the values of the callee-saved registers that volition be used past the function. To save registers, push them onto the stack. The callee-saved registers are EBX, EDI, and ESI (ESP and EBP volition also be preserved by the calling convention, but demand not be pushed on the stack during this stride).

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

  1. Leave the return value in EAX.
  2. Restore the quondam values of whatever callee-saved registers (EDI and ESI) that were modified. The register contents are restored past popping them from the stack. The registers should be popped in the changed guild that they were pushed.
  3. Deallocate local variables. The obvious mode to do this might be to add the appropriate value to the stack pointer (since the space was allocated past subtracting the needed amount from the stack pointer). In do, a less error-decumbent way to deallocate the variables is to move the value in the base pointer into the stack pointer: mov esp, ebp. This works because the base arrow always contains the value that the stack arrow contained immediately prior to the allocation of the local variables.
  4. Immediately before returning, restore the caller'south base pointer value past popping EBP off the stack. Recall that the first thing nosotros did on entry to the subroutine was to button the base pointer to save its old value.
  5. Finally, render to the caller past executing a ret instruction. This instruction will observe and remove the appropriate return address from the stack.

Annotation that the callee's rules fall cleanly into two halves that are basically mirror images of one another. The first one-half of the rules utilize to the beginning of the function, and are ordinarily said to define the prologue to the function. The latter one-half of the rules apply to the cease of the office, and are thus commonly said to define the epilogue of the part.

Instance
Hither is an instance role definition that follows the callee rules:

.486 .MODEL Apartment .Lawmaking PUBLIC _myFunc _myFunc PROC   ; Subroutine Prologue   push ebp     ; Save the old base pointer value.   mov ebp, esp ; Set the new base pointer value.   sub esp, four   ; Make room for one 4-byte local variable.   button edi     ; Save the values of registers that the function   push esi     ; will modify. This part uses EDI and ESI.   ; (no need to save EBX, EBP, or ESP)    ; Subroutine Body   mov eax, [ebp+8]   ; Move value of parameter 1 into EAX   mov esi, [ebp+12]  ; Motility value of parameter two into ESI   mov edi, [ebp+16]  ; Move value of parameter 3 into EDI    mov [ebp-4], edi   ; Movement EDI into the local variable   add [ebp-4], esi   ; Add ESI into the local variable   add eax, [ebp-4]   ; Add the contents of the local variable                      ; into EAX (final result)    ; Subroutine Epilogue    pop esi      ; Recover register values   popular  edi   mov esp, ebp ; Deallocate local variables   pop ebp ; Restore the caller'south base pointer value   ret _myFunc ENDP Finish          

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 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 pointer for the duration of the subroutines execution. In particular, we discover that since parameters were placed onto the stack before the subroutine was chosen, they are always located below the base pointer (i.e. at higher addresses) on the stack. The get-go parameter to the subroutine can e'er be found at memory location EBP + 8, the second at EBP + 12, the third at EBP + 16. Similarly, since local variables are allocated later on the base of operations pointer is set up, they always reside above the base pointer (i.e. at lower addresses) on the stack. In particular, the first local variable is ever located at EBP - 4, the second at EBP - viii, and then on. This conventional use of the base pointer allows us to apace identify the use of local variables and parameters within a role torso.

The function epilogue is basically a mirror epitome of the role prologue. The caller's register 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 didactics is used to return to the appropriate code location in the caller.

Using these Materials

These materials are released under a Creative Commons Attribution-Noncommercial-Share Akin 3.0 United States License. Nosotros are delighted when people want to use or adapt the course materials we developed, and you are welcome to reuse and accommodate these materials for any non-commercial purposes (if you would like to utilize them for a commercial purpose, please contact David Evans for more information). If yous do adapt or utilize these materials, please include a credit like "Adjusted from materials developed for University of Virginia cs216 by David Evans. This guide was revised for cs216 by David Evans, based on materials originally created by Adam Ferrari many years ago, and since updated by Alan Batson, Mike Lack, and Anita Jones." and a link dorsum to this page.


Source: https://www.cs.virginia.edu/~evans/cs216/guides/x86.html

Posted by: kirklandshery1960.blogspot.com

0 Response to "What Assembly Chip Stores A Number To Move Into A Register"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel