How To Move A 64 Bit Number Into Two Registers
x86 Assembly Guide
Contents: Registers | Retentiveness and Addressing | Instructions | Calling Convention
This is a version adjusted 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 nuts of 32-bit x86 assembly language programming, covering a small merely useful subset of the bachelor 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. Nosotros will uses the standard AT&T syntax for writing x86 assembly code.
The full x86 instruction set is large and complex (Intel's x86 instruction set manuals comprise over 2900 pages), and nosotros practise not embrace it all in this guide. For example, at that place is a sixteen-bit subset of the x86 teaching set up. Using the 16-flake programming model can be quite circuitous. It has a segmented retention model, more restrictions on register usage, so on. In this guide, we will limit our attention to more modern aspects of x86 programming, and delve into the instruction set simply in enough detail to go a basic feel for x86 programming.
Registers
Mod (i.due east 386 and beyond) x86 processors have eight 32-bit general purpose registers, equally depicted in Figure 1. The register 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 every bit the counter since information technology was used to hold a loop index. Whereas most of the registers have lost their special purposes in the modern educational activity set, 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 example, the to the lowest degree meaning 2 bytes of EAX tin be treated equally a 16-scrap register called AX. The least significant byte of AX can be used every bit a unmarried 8-bit annals called AL, while the most significant byte of AX can be used every bit a single 8-flake annals chosen AH. These names refer to the same concrete annals. 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-bit versions of the instruction ready. Nonetheless, they are sometimes user-friendly when dealing with data that are smaller than 32-bits (e.g. i-byte ASCII characters).
Figure one. x86 Registers
Memory and Addressing Modes
Declaring Static Data Regions
You can declare static data regions (coordinating to global variables) in x86 assembly using special assembler directives for this purpose. Data declarations should exist preceded by the .data directive. Following this directive, the directives .byte, .short, and .long can be used to declare one, two, and four byte data locations, respectively. To refer to the accost of the data created, we tin label them. Labels are very useful and versatile in assembly, they give names to memory locations that will exist figured out later past the assembler or the linker. This is similar to declaring variables by name, merely abides by some lower level rules. For example, locations declared in sequence will be located in memory next to ane another.
Case declarations:
.data var: .byte 64 /* Declare a byte, referred to equally location var, containing the value 64. */ .byte 10 /* Declare a byte with no label, containing the value 10. Its location is var + i. */ x: .brusque 42 /* Declare a 2-byte value initialized to 42, referred to as location x. */ y: .long 30000 /* Declare a four-byte value, referred to as location y, initialized to 30000. */
Unlike in high level languages where arrays tin take many dimensions and are accessed past indices, arrays in x86 assembly language are simply a number of cells located contiguously in memory. An array can be alleged by just list the values, equally in the first example below. For the special instance of an array of bytes, string literals can be used. In case a large surface area of retention is filled with zeroes the .zero directive can exist used.
Some examples:
s: .long 1, 2, iii /* Declare three 4-byte values, initialized to i, 2, and 3.
The value at location s + viii will be iii. */barr: .nix 10 /* Declare 10 bytes starting at location barr, initialized to 0. */ str: .string "hello" /* Declare 6 bytes starting at the accost str initialized to
the ASCII graphic symbol values for hi followed by a nul (0) byte. */
Addressing Memory
Modern x86-compatible processors are capable of addressing up to 232 bytes of memory: retention addresses are 32-bits broad. In the examples above, where we 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 addition to supporting referring to memory regions by labels (i.eastward. constant values), the x86 provides a flexible scheme for calculating and referring to retention addresses: up to two of the 32-bit registers and a 32-bit signed abiding tin can be added together to compute a retention address. One of the registers can be optionally pre-multiplied by 2, 4, or viii.
The addressing modes tin 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 memory. This instruction has ii operands: the first is the source and the 2nd specifies the destination.
Some examples of mov instructions using address computations are:
mov (%ebx), %eax /* Load iv bytes from the retention accost in EBX into EAX. */ mov %ebx, var(,1) /* Motion the contents of EBX into the 4 bytes at memory address var.
(Note, var is a 32-scrap constant). */mov -four(%esi), %eax /* Motility iv bytes at memory address ESI + (-4) into EAX. */ mov %cl, (%esi,%eax,1) /* Move the contents of CL into the byte at address ESI+EAX. */ mov (%esi,%ebx,4), %edx /* Move the 4 bytes of data at address ESI+4*EBX into EDX. */
Some examples of invalid address calculations include:
mov (%ebx,%ecx,-1), %eax /* Can only add annals values. */ mov %ebx, (%eax,%esi,%edi,one) /* At almost ii registers in address computation. */
Operation Suffixes
In general, the intended size of the of the data item at a given retentiveness address tin can exist 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 be 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 nosotros were referring to was 4 bytes wide. When we were storing the value of a i byte annals to memory, the assembler could infer that we wanted the accost to refer to a single byte in memory.
However, in some cases the size of a referred-to memory region is ambiguous. Consider the education mov $2, (%ebx). Should this didactics move the value 2 into the single byte at address EBX? Possibly it should move the 32-bit integer representation of 2 into the 4-bytes starting at accost EBX. Since either is a valid possible interpretation, the assembler must be explicitly directed every bit to which is right. The size prefixes b, west, and fifty serve this purpose, indicating sizes of 1, 2, and 4 bytes respectively.
For example:
movb $ii, (%ebx) /* Move 2 into the single byte at the address stored in EBX. */ movw $2, (%ebx) /* Move the sixteen-bit integer representation of 2 into the 2 bytes starting at the address in EBX. */ movl $2, (%ebx) /* Move the 32-bit integer representation of two into the iv bytes starting at the accost in EBX. */
Instructions
Machine instructions generally autumn into 3 categories: data move, arithmetic/logic, and control-flow. In this section, we volition await at important examples of x86 instructions from each category. This department should not exist considered an exhaustive list of x86 instructions, but rather a useful subset. For a complete listing, encounter Intel's teaching set reference.
We use the following 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> Any 8-bit register (%ah, %bh, %ch, %dh, %al, %bl, %cl, or %dl) <reg> Whatever register <mem> A memory address (e.g., (%eax), 4+var(,i), or (%eax,%ebx,1)) <con32> Any 32-bit immediate <con16> Whatever 16-bit firsthand <con8> Any viii-bit immediate <con> Whatsoever viii-, 16-, or 32-chip immediate
In associates language, all the labels and numeric constants used as firsthand operands (i.e. not in an address calculation similar iii(%eax,%ebx,8)) are always prefixed past a dollar sign. When needed, hexadecimal annotation can be used with the 0x prefix (e.g. $0xABC). Without the prefix, numbers are interpreted in the decimal basis.
Data Move Instructions
mov — Move
The mov education copies the data item referred to past its first operand (i.e. annals contents, memory contents, or a abiding value) into the location referred to by its 2d operand (i.e. a register or memory). While register-to-annals moves are possible, straight memory-to-retention moves are non. In cases where memory transfers are desired, the source retentivity contents must offset exist loaded into a register, so can exist 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(,ane) — store the value 5 into the byte at location var
push — Push on stack
The push instruction places its operand onto the top of the hardware supported stack in retention. Specifically, push first decrements ESP by four, and so places its operand into the contents of the 32-bit location at accost (%esp). ESP (the stack pointer) is decremented past push since the x86 stack grows downward — i.e. the stack grows from high addresses to lower addresses.Syntax
push <reg32>
push <mem>
push <con32>Examples
button %eax — push eax on the stack
button var(,1) — push the 4 bytes at accost var onto the stack
pop — Popular from stack
The popular instruction removes the 4-byte data element from the top of the hardware-supported stack into the specified operand (i.east. register or retentiveness location). It first moves the iv bytes located at retention location (%esp) into the specified register or retentiveness location, and then increments ESP past 4.Syntax
Examples
pop <reg32>
pop <mem>
popular %edi — popular the top chemical element of the stack into EDI.
pop (%ebx) — pop the top element of the stack into retentivity at the four bytes starting at location EBX.
lea — Load constructive address
The lea instruction places the address specified by its first operand into the annals 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 simple arithmetic operations.Syntax
lea <mem>, <reg32>
Examples
lea (%ebx,%esi,viii), %edi — the quantity EBX+8*ESI is placed in EDI.
lea val(,ane), %eax — the value val is placed in EAX.
Arithmetic and Logic Instructions
add — Integer addition
The add instruction adds together its two operands, storing the result in its 2d operand. Note, whereas both operands may be registers, at most ane operand may exist a memory location.Syntax
add <reg>, <reg>
add <mem>, <reg>
add <reg>, <mem>
add together <con>, <reg>
add <con>, <mem>
Examples
add together $10, %eax — EAX is ready to EAX + 10
addb $x, (%eax) — add 10 to the single byte stored at memory accost stored in EAX
sub — Integer subtraction
The sub instruction 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. Equally with add, whereas both operands may be registers, at most one operand may be a retentiveness location.Syntax
sub <reg>, <reg>
sub <mem>, <reg>
sub <reg>, <mem>
sub <con>, <reg>
sub <con>, <mem>
Examples
sub %ah, %al — AL is gear up to AL - AH
sub $216, %eax — subtract 216 from the value stored in EAX
inc, dec — Increment, Decrement
The inc didactics increments the contents of its operand past i. The dec instruction decrements the contents of its operand by i.Syntax
inc <reg>
inc <mem>
dec <reg>
dec <mem>Examples
dec %eax — subtract 1 from the contents of EAX
incl var(,1) — add one to the 32-bit integer stored at location var
imul — Integer multiplication
The imul education has ii basic formats: ii-operand (first two syntax listings to a higher place) and three-operand (terminal two syntax listings higher up).The ii-operand class multiplies its two operands together and stores the result in the second operand. The consequence (i.e. second) operand must exist a register.
The three operand grade multiplies its 2nd and third operands together and stores the event in its final operand. Again, the result operand must exist a register. Furthermore, the first operand is restricted to being a constant 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-scrap contents of the retentivity at location EBX. Shop the consequence in EAX.
imul $25, %edi, %esi — ESI is ready to EDI * 25
idiv — Integer division
The idiv teaching divides the contents of the 64 bit integer EDX:EAX (synthetic by viewing EDX as the about significant four bytes and EAX every bit the least significant four bytes) by the specified operand value. The quotient result of the partitioning is stored into EAX, while the residue is placed in EDX.Syntax
idiv <reg32>
idiv <mem>Examples
idiv %ebx — divide the contents of EDX:EAX by the contents of EBX. Place the caliber in EAX and the residue in EDX.
idivw (%ebx) — divide the contents of EDX:EAS by the 32-chip 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 offset 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 4 bits of EAX.
xor %edx, %edx — gear up the contents of EDX to goose egg.
not — Bitwise logical not
Logically negates the operand contents (that is, flips all bit values in the operand).Syntax
not <reg>
not <mem>Example
not %eax — flip all the bits of EAX
neg — Negate
Performs the two'due south complement negation of the operand contents.Syntax
neg <reg>
neg <mem>Instance
neg %eax — EAX is set to (- EAX)
shl, shr — Shift left and right
These instructions shift the bits in their get-go operand's contents left and right, padding the resulting empty chip positions with zeros. The shifted operand can exist 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 $1, eax — Multiply the value of EAX by ii (if the near pregnant bit is 0)
shr %cl, %ebx — Shop in EBX the flooring of upshot of dividing the value of EBX by 2 n where n is the value in CL. Caution: for negative integers, information technology is different from the C semantics of partition!
Control Menstruation Instructions
The x86 processor maintains an instruction pointer (EIP) register that is a 32-scrap value indicating the location in memory where the electric current instruction starts. Usually, it increments to signal to the next instruction in memory begins after execution an instruction. The EIP register cannot be manipulated direct, but is updated implicitly by provided command flow instructions.
We apply the notation <characterization> to refer to labeled locations in the program text. Labels can be inserted anywhere in x86 assembly code text past entering a characterization proper name followed by a colon. For example,
mov 8(%ebp), %esi begin: xor %ecx, %ecx mov (%esi), %eax
The second instruction in this lawmaking fragment is labeled begin. Elsewhere in the code, we can refer to the retentiveness location that this instruction is located at in retentivity using the more convenient symbolic name brainstorm. This label is just a convenient mode of expressing the location instead of its 32-bit value.
jmp — Leap
Transfers plan control menstruation to the instruction at the memory location indicated by the operand.Syntax
jmp <label>Instance
jmp begin — Jump to the education labeled begin.
jcondition — Conditional spring
These instructions are conditional jumps that are based on the status of a set of condition codes that are stored in a special register called the motorcar status discussion. The contents of the auto condition word include data about the last arithmetics functioning performed. For example, one bit of this give-and-take indicates if the last issue was zero. Another indicates if the last outcome was negative. Based on these condition codes, a number of conditional jumps can be performed. For example, the jz education performs a jump to the specified operand label if the result of the last arithmetics operation was zero. Otherwise, command gain to the next instruction in sequence.A number of the conditional branches are given names that are intuitively based on the final functioning performed being a special compare instruction, cmp (come across beneath). For case, conditional branches such as jle and jne are based on offset performing a cmp operation on the desired operands.
Syntax
je <label> (spring when equal)
jne <label> (jump when not equal)
jz <label> (spring when last consequence was zero)
jg <characterization> (jump when greater than)
jge <characterization> (spring when greater than or equal to)
jl <label> (jump when less than)
jle <characterization> (jump when less than or equal to)Case
cmp %ebx, %eax jle doneIf the contents of EAX are less than or equal to the contents of EBX, jump to the label done. Otherwise, proceed to the next instruction.
cmp — Compare
Compare the values of the two specified operands, setting the condition codes in the machine status discussion appropriately. This didactics is equivalent to the sub instruction, except the result 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 $10, (%ebx)jeq loopIf the byte stored at the memory location in EBX is equal to the integer constant x, jump to the location labeled loop.
call, ret — Subroutine call and return
These instructions implement a subroutine telephone call and render. The telephone call instruction first pushes the current code location onto the hardware supported stack in memory (see the push teaching for details), and then performs an unconditional jump to the code location indicated by the label operand. Unlike the simple spring instructions, the telephone call pedagogy saves the location to render to when the subroutine completes.The ret educational activity implements a subroutine return mechanism. This instruction showtime pops a code location off the hardware supported in-retentiveness stack (run into the pop instruction for details). Information technology then performs an unconditional jump to the retrieved code location.
Syntax
phone call <label>
ret
Calling Convention
To allow split up programmers to share lawmaking and develop libraries for use by many programs, and to simplify the utilise of subroutines in general, programmers typically prefer a common calling convention. The calling convention is a protocol most how to call and return from routines. For example, given a set of calling convention rules, a programmer demand non examine the definition of a subroutine to determine how parameters should be passed to that subroutine. Furthermore, given a ready of calling convention rules, loftier-level language compilers tin can be made to follow the rules, thus allowing hand-coded associates linguistic communication routines and loftier-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 to write assembly language subroutines that are safely callable from C (and C++) code, and volition also enable you to call C library functions from your assembly language code.
The C calling convention is based heavily on the utilise of the hardware-supported stack. It is based on the push button, 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 memory on the stack. The vast majority of loftier-level procedural languages implemented on most processors have used similar calling conventions.
The calling convention is cleaved into ii sets of rules. The start set of rules is employed by the caller of the subroutine, and the 2nd set of rules is observed past the writer of the subroutine (the callee). Information technology should be emphasized that mistakes in the observance of these rules speedily outcome in fatal plan errors since the stack will be left in an inconsistent land; thus meticulous intendance should be used when implementing the call convention in your own subroutines.
Stack during Subroutine Phone call
[Thanks to James Peterson for finding and fixing the bug in the original version of this figure!]
A practiced 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 image to a higher place 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 memory addresses of the cells are 4 bytes autonomously. The outset parameter resides at an starting time of 8 bytes from the base of operations pointer. Above the parameters on the stack (and below the base arrow), the telephone call pedagogy placed the render address, thus leading to an extra 4 bytes of first from the base pointer to the outset parameter. When the ret instruction is used to return from the subroutine, information technology volition jump to the return address stored on the stack.
Caller Rules
To brand a subrouting phone call, the caller should:
- Earlier calling a subroutine, the caller should salve 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 later on the subroutine returns, the caller must button the values in these registers onto the stack (and then they can be restore after the subroutine returns.
- To pass parameters to the subroutine, button them onto the stack before the call. The parameters should be pushed in inverted guild (i.e. last parameter offset). Since the stack grows downwards, the start parameter volition 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 telephone call the subroutine, use the call instruction. This instruction places the return address on acme of the parameters on the stack, and branches to the subroutine lawmaking. This invokes the subroutine, which should follow the callee rules beneath.
Afterwards the subroutine returns (immediately following the call instruction), the caller tin expect to discover the return value of the subroutine in the register EAX. To restore the machine state, the caller should:
- Remove the parameters from stack. This restores the stack to its country 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 assume that no other registers were modified past the subroutine.
Example
The lawmaking below shows a office 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 retentivity location stored in EBX.
push (%ebx) /* Push button last parameter first */ button $216 /* Push the second parameter */ push %eax /* Push first parameter last */ telephone call myFunc /* Phone call the function (presume C naming) */ add together $12, %esp
Annotation that after the call returns, the caller cleans up the stack using the add pedagogy. We accept 12 bytes (three parameters * 4 bytes each) on the stack, and the stack grows down. Thus, to get rid of the parameters, nosotros tin can simply add 12 to the stack pointer.
The result produced by myFunc is at present available for use in the register EAX. The values of the caller-saved registers (ECX and EDX), may have 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 later information technology.
Callee Rules
The definition of the subroutine should attach to the post-obit rules at the offset of the subroutine:
- Push the value of EBP onto the stack, and so copy the value of ESP into EBP using the following instructions:
push %ebp mov %esp, %ebp
This initial activity maintains the base of operations arrow, 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 button the onetime base pointer value at the beginning of the subroutine so that we tin can after restore the appropriate base arrow value for the caller when the subroutine returns. Remember, the caller is not expecting the subroutine to change the value of the base arrow. We then movement the stack pointer into EBP to obtain our bespeak of reference for accessing parameters and local variables. - Next, allocate local variables by making space on the stack. Recall, the stack grows downward, so to make space on the elevation of the stack, the stack pointer should be decremented. The amount by which the stack arrow is decremented depends on the number and size of local variables needed. For example, if iii local integers (4 bytes each) were required, the stack pointer would need to be decremented by 12 to make space for these local variables (i.e., sub $12, %esp). As with parameters, local variables volition be located at known offsets from the base pointer.
- Side by side, save the values of the callee-saved registers that will be used past the part. To save registers, push them onto the stack. The callee-saved registers are EBX, EDI, and ESI (ESP and EBP volition likewise be preserved by the calling convention, just need not exist pushed on the stack during this stride).
After these three actions are performed, the trunk of the subroutine may continue. When the subroutine is returns, it must follow these steps:
- Leave the render value in EAX.
- Restore the sometime values of whatsoever 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 order that they were pushed.
- Deallocate local variables. The obvious way to exercise this might exist to add together the appropriate value to the stack pointer (since the space was allocated past subtracting the needed amount from the stack arrow). In practise, a less error-prone manner to deallocate the variables is to movement the value in the base of operations pointer into the stack pointer: mov %ebp, %esp. This works because the base of operations arrow e'er contains the value that the stack pointer independent immediately prior to the resource allotment of the local variables.
- Immediately before returning, restore the caller's base pointer value by popping EBP off the stack. Recall that the first affair we did on entry to the subroutine was to push the base arrow to save its old value.
- Finally, return to the caller by executing a ret instruction. This teaching volition find and remove the appropriate return address from the stack.
Note that the callee's rules autumn cleanly into ii halves that are basically mirror images of one some other. The commencement half of the rules apply to the offset of the office, and are unremarkably said to define the prologue to the office. The latter half of the rules apply to the end of the office, and are thus unremarkably said to define the epilogue of the function.
Example
Here is an example function definition that follows the callee rules:
/* Start the code section */ .text /* Define myFunc as a global (exported) function. */ .globl myFunc .blazon myFunc, @function myFunc: /* Subroutine Prologue */ push %ebp /* Save the quondam base of operations pointer value. */ mov %esp, %ebp /* Set the new base pointer value. */ sub $iv, %esp /* Brand room for 1 iv-byte local variable. */ push button %edi /* Relieve the values of registers that the function */ push %esi /* will change. This part uses EDI and ESI. */ /* (no need to salve EBX, EBP, or ESP) */ /* Subroutine Torso */ mov viii(%ebp), %eax /* Move value of parameter 1 into EAX. */ mov 12(%ebp), %esi /* Move value of parameter 2 into ESI. */ mov 16(%ebp), %edi /* Move value of parameter 3 into EDI. */ mov %edi, -4(%ebp) /* Motion EDI into the local variable. */ add %esi, -4(%ebp) /* Add together ESI into the local variable. */ add -iv(%ebp), %eax /* Add together the contents of the local variable */ /* into EAX (last consequence). */ /* Subroutine Epilogue */ pop %esi /* Recover annals values. */ pop %edi mov %ebp, %esp /* Deallocate the local variable. */ popular %ebp /* Restore the caller's base pointer value. */ ret
The subroutine prologue performs the standard actions of saving a snapshot of the stack arrow in EBP (the base of operations pointer), allocating local variables past decrementing the stack pointer, and saving register values on the stack.
In the body of the subroutine we can run across the use of the base of operations pointer. Both parameters and local variables are located at constant offsets from the base of operations pointer for the duration of the subroutines execution. In particular, we detect that since parameters were placed onto the stack before the subroutine was called, they are always located below the base arrow (i.eastward. at higher addresses) on the stack. The beginning parameter to the subroutine can always be found at retention location (EBP+eight), the second at (EBP+12), the third at (EBP+16). Similarly, since local variables are allocated after the base of operations pointer is set, they always reside above the base of operations pointer (i.east. at lower addresses) on the stack. In item, the outset local variable is ever located at (EBP-iv), the second at (EBP-8), and so on. This conventional use of the base of operations pointer allows usa to quickly identify the utilize of local variables and parameters within a function body.
The function epilogue is basically a mirror image of the function prologue. The caller'southward annals values are recovered from the stack, the local variables are deallocated by resetting the stack arrow, the caller'southward 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 utilise the AT&T syntax for Yale'southward CS421.
Source: https://flint.cs.yale.edu/cs421/papers/x86-asm/asm.html
Posted by: thorntonprour1964.blogspot.com
0 Response to "How To Move A 64 Bit Number Into Two Registers"
Post a Comment