How To Multiply A Register By An Integer Number Mips
x86 Assembly Guide
Contents: Registers | Memory and Addressing | Instructions | Calling Convention
This guide describes the basics of 32-bit x86 assembly language programming, covering a small but useful subset of the available instructions and assembler directives. There are several dissimilar assembly languages for generating x86 machine code. The one we will use 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 up is large and circuitous (Intel's x86 didactics set up manuals comprise over 2900 pages), and we do not encompass it all in this guide. For example, there is a sixteen-bit subset of the x86 educational activity prepare. Using the 16-fleck programming model tin be quite complex. It has a segmented retentivity model, more than restrictions on register usage, and and then on. In this guide, we will limit our attention to more mod aspects of x86 programming, and delve into the pedagogy set just in plenty particular to get a bones feel for x86 programming.
Resources
- Guide to Using Assembly in Visual Studio — a tutorial on building and debugging assembly code in Visual Studio
- Intel x86 Instruction Fix Reference
- Intel's Pentium Manuals (the total gory details)
Registers
Modern (i.e 386 and beyond) x86 processors have 8 32-bit general purpose registers, as depicted in Figure 1. 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 equally the counter since it was used to hold a loop alphabetize. Whereas about of the registers take lost their special purposes in the modern instruction set, past convention, 2 are reserved for special purposes — the stack pointer (ESP) and the base arrow (EBP).
For the EAX, EBX, ECX, and EDX registers, subsections may be used. For case, the least pregnant 2 bytes of EAX tin can be treated as a 16-bit register chosen AX. The to the lowest degree significant byte of AX can exist used as a single 8-flake register called AL, while the almost meaning byte of AX can exist used as a single 8-bit register called AH. These names refer to the same physical register. When a 2-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-fleck versions of the education set up. However, they are sometimes convenient when dealing with information that are smaller than 32-bits (eastward.grand. 1-byte ASCII characters).
When referring to registers in associates linguistic communication, the names are not example-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 lot can declare static data regions (coordinating to global variables) in x86 associates using special assembler directives for this purpose. Data declarations should be preceded by the .Data directive. Following this directive, the directives DB, DW, and DD tin can be used to declare 1, two, and four byte data locations, respectively. Declared locations tin be labeled with names for afterward reference — 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 side by side to one some other.
Example declarations:
.Information var DB 64 ; Declare a byte, referred to equally location var, containing the value 64. var2 DB ? ; Declare an uninitialized byte, referred to equally location var2. DB 10 ; Declare a byte with no label, containing the value 10. Its location is var2 + one. 10 DW ? ; Declare a ii-byte uninitialized value, referred to as location 10. Y DD 30000 ; Declare a 4-byte value, referred to equally location Y, initialized to 30000.
Unlike in high level languages where arrays tin have many dimensions and are accessed by indices, arrays in x86 associates language are simply a number of cells located contiguously in memory. An array tin can be alleged past just listing the values, as in the first case below. Two other common methods used for declaring arrays of information are the DUP directive and the utilise of string literals. The DUP directive tells the assembler to duplicate an expression a given number of times. For example, 4 DUP(ii) is equivalent to ii, ii, 2, two.
Some examples:
Z DD i, two, 3 ; Declare iii 4-byte values, initialized to i, 2, and 3. The value of location Z + 8 will be iii. bytes DB 10 DUP(?) ; Declare ten 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 half-dozen bytes starting at the address str, initialized to the ASCII character values for hello and the null (0) byte.
Addressing Memory
Modern x86-compatible processors are capable of addressing up to ii32 bytes of retentivity: memory addresses are 32-bits wide. In the examples in a higher place, where nosotros used labels to refer to retentivity regions, these labels are actually replaced past the assembler with 32-bit quantities that specify addresses in memory. In addition to supporting referring to memory regions by labels (i.e. constant values), the x86 provides a flexible scheme for computing and referring to memory addresses: up to 2 of the 32-fleck registers and a 32-bit signed constant can be added together to compute a memory accost. One of the registers can be optionally pre-multiplied by 2, 4, or 8.
The addressing modes can be used with many x86 instructions (nosotros'll draw them in the adjacent department). Here nosotros illustrate some examples using the mov instruction that moves data between registers and retentiveness. This didactics has two operands: the first is the destination and the second specifies the source.
Some examples of mov instructions using address computations are:
mov eax, [ebx] ; Move the 4 bytes in retentiveness at the address contained in EBX into EAX mov [var], ebx ; Move the contents of EBX into the four bytes at retention address var. (Note, var is a 32-chip constant). mov eax, [esi-4] ; Movement iv bytes at memory address ESI + (-4) into EAX mov [esi+eax], cl ; Motion the contents of CL into the byte at address ESI+EAX mov edx, [esi+four*ebx] ; Motion the 4 bytes of data at address ESI+four*EBX into EDX
Some examples of invalid address calculations include:
mov eax, [ebx-ecx] ; Tin can merely add together register values mov [eax+esi+edi], ebx ; At virtually 2 registers in address computation
Size Directives
In full general, the intended size of the data particular at a given memory address can exist inferred from the assembly code instruction in which it is referenced. For example, in all of the higher up instructions, the size of the memory regions could exist inferred from the size of the annals operand. When we were loading a 32-bit register, the assembler could infer that the region of memory we were referring to was 4 bytes broad. When we were storing the value of a i byte register to retention, the assembler could infer that we wanted the address to refer to a single byte in memory.
Nevertheless, in some cases the size of a referred-to retention region is ambiguous. Consider the instruction mov [ebx], 2. Should this instruction move the value two into the unmarried byte at address EBX? Perhaps it should move 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 exist explicitly directed equally to which is correct. The size directives BYTE PTR, WORD PTR, and DWORD PTR serve this purpose, indicating sizes of one, 2, and 4 bytes respectively.
For example:
mov BYTE PTR [ebx], two ; Move 2 into the single byte at the address stored in EBX. mov Discussion PTR [ebx], 2 ; Move the 16-bit integer representation of two into the 2 bytes starting at the address in EBX. mov DWORD PTR [ebx], 2 ; Move the 32-bit integer representation of two into the 4 bytes starting at the accost in EBX.
Instructions
Machine instructions by and large fall into three categories: data motility, arithmetics/logic, and command-catamenia. In this section, nosotros volition expect at important 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 complete listing, encounter Intel's instruction set reference.
We utilize the following annotation:
<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 eight-chip register (AH, BH, CH, DH, AL, BL, CL, or DL) <reg> Any register <mem> A retentivity accost (e.g., [eax], [var + iv], or dword ptr [eax+ebx]) <con32> Any 32-flake constant <con16> Any 16-bit constant <con8> Whatsoever viii-chip constant <con> Any 8-, 16-, or 32-bit constant
Data Move Instructions
mov — Move (Opcodes: 88, 89, 8A, 8B, 8C, 8E, ...)
The mov instruction copies the data item referred to by its second operand (i.due east. register contents, memory contents, or a constant value) into the location referred to past its commencement operand (i.e. a register or memory). While register-to-register moves are possible, direct memory-to-memory moves are not. In cases where retention transfers are desired, the source retentiveness contents must showtime be loaded into a register, and then tin can be stored to the destination memory accost.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
button — Push stack (Opcodes: FF, 89, 8A, 8B, 8C, 8E, ...)
The push instruction places its operand onto the top of the hardware supported stack in memory. Specifically, push beginning decrements ESP by iv, then places its operand into the contents of the 32-flake location at address [ESP]. ESP (the stack pointer) is decremented by push since the x86 stack grows down - i.e. the stack grows from high addresses to lower addresses. Syntax
push <reg32>
push <mem>
push <con32>Examples
push eax — push eax on the stack
push [var] — push the 4 bytes at address var onto the stack
popular — Pop stack
The pop pedagogy removes the four-byte data element from the top of the hardware-supported stack into the specified operand (i.e. register or memory location). It kickoff moves the 4 bytes located at retentiveness location [SP] into the specified register or retentiveness location, and then increments SP by 4.Syntax
Examples
popular <reg32>
pop <mem>
pop edi — pop the top element of the stack into EDI.
popular [ebx] — pop the peak element of the stack into memory at the four bytes starting at location EBX.
lea — Load effective address
The lea pedagogy places the address specified by its 2nd operand into the register specified past its start operand. Note, the contents of the memory location are not loaded, but the effective address is computed and placed into the annals. This is useful for obtaining a pointer 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 — Integer Improver
The add didactics adds together its 2 operands, storing the result in its get-go operand. Note, whereas both operands may be registers, at nigh ane operand may be a memory location. Syntax
add together <reg>,<reg>
add <reg>,<mem>
add <mem>,<reg>
add <reg>,<con>
add together <mem>,<con>
Examples
add eax, x — EAX ← EAX + ten
add together BYTE PTR [var], 10 — add 10 to the single byte stored at memory address var
sub — Integer Subtraction
The sub instruction stores in the value of its offset operand the result of subtracting the value of its second operand from the value of its start operand. As 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, dec — 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 — decrease one from the contents of EAX.
inc DWORD PTR [var] — add ane to the 32-flake integer stored at location var
imul — Integer Multiplication
The imul educational activity has two bones formats: ii-operand (first two syntax listings in a higher place) and three-operand (last two syntax listings above). The ii-operand form multiplies its two operands together and stores the event in the kickoff operand. The outcome (i.eastward. first) operand must be a annals. The three operand form multiplies its second and third operands together and stores the issue in its outset operand. Again, the result operand must be a annals. Furthermore, the 3rd operand is restricted to being 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 past the 32-bit contents of the retention location var. Store the result in EAX.
imul esi, edi, 25 — ESI → EDI * 25
idiv — Integer Sectionalisation
The idiv instruction divides the contents of the 64 flake integer EDX:EAX (synthetic past viewing EDX as the almost significant four bytes and EAX every bit the to the lowest degree meaning four bytes) by the specified operand value. The quotient result of the sectionalisation is stored into EAX, while the residual is placed in EDX.Syntax
idiv <reg32>
idiv <mem>Examples
idiv ebx — carve up the contents of EDX:EAX by the contents of EBX. Place the quotient in EAX and the remainder in EDX.
idiv DWORD PTR [var] — separate the contents of EDX:EAX by the 32-bit value stored at retention location var. Place the quotient in EAX and the residue in EDX.
and, or, xor — Bitwise logical and, or and exclusive or
These instructions perform the specified logical operation (logical bitwise and, or, and sectional or, respectively) on their operands, placing the outcome in the first 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 only the last 4 bits of EAX.
xor edx, edx — gear up the contents of EDX to zero.
not — Bitwise Logical Not
Logically negates the operand contents (that is, flips all scrap values in the operand).Syntax
not <reg>
not <mem>Example
non BYTE PTR [var] — negate all bits in the byte at the memory location var.
neg — Negate
Performs the two's complement negation of the operand contents.Syntax
neg <reg>
neg <mem>Instance
neg eax — EAX → - EAX
shl, shr — Shift Left, Shift Correct
These instructions shift the bits in their first operand's contents left and right, padding the resulting empty flake positions with zeros. The shifted operand tin can be shifted up to 31 places. The number of bits to shift is specified by the second operand, which tin can be either an 8-chip constant or the register CL. In either case, shifts counts of greater and 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 most significant bit is 0)
shr ebx, cl — Store in EBX the floor of upshot of dividing the value of EBX by 2 north wherenorthward is the value in CL.
Control Flow Instructions
The x86 processor maintains an instruction arrow (IP) register that is a 32-fleck value indicating the location in retentiveness where the electric current didactics starts. Normally, it increments to point to the next instruction in memory begins later on execution an didactics. The IP register cannot be manipulated direct, merely is updated implicitly past provided control catamenia instructions.
We utilize the notation <label> to refer to labeled locations in the program text. Labels tin can exist inserted anywhere in x86 assembly code text by entering a label name followed by a colon. For example,
mov esi, [ebp+8] begin: xor ecx, ecx mov eax, [esi]
The second instruction in this lawmaking fragment is labeled begin. Elsewhere in the code, we tin can refer to the retentiveness location that this didactics is located at in retention using the more convenient symbolic proper name begin. This label is just a convenient way of expressing the location instead of its 32-bit value.
jmp — Jump
Transfers programme control flow to the education at the retentivity location indicated by the operand.Syntax
jmp <label>Case
jmp begin — Jump to the instruction labeled begin.
jstatus — Provisional Jump
These instructions are conditional jumps that are based on the status of a fix of condition codes that are stored in a special annals chosen the car condition word. The contents of the machine status discussion include data about the last 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 status 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 issue of the concluding arithmetics operation was zero. Otherwise, command proceeds to the next instruction in sequence.A number of the provisional branches are given names that are intuitively based on the last functioning performed being a special compare instruction, cmp (run across below). For example, conditional branches such as jle and jne are based on outset performing a cmp operation on the desired operands.
Syntax
je <label> (jump when equal)
jne <label> (jump when not equal)
jz <label> (jump when last result was cypher)
jg <label> (jump when greater than)
jge <characterization> (spring when greater than or equal to)
jl <label> (jump when less than)
jle <label> (leap when less than or equal to)Example
cmp eax, ebx
jle washedIf the contents of EAX are less than or equal to the contents of EBX, jump to the label done. Otherwise, continue to the next instruction.
cmp — Compare
Compare the values of the 2 specified operands, setting the condition codes in the auto status word appropriately. This instruction is equivalent to the sub instruction, except the issue of the subtraction is discarded instead of replacing the first operand.Syntax
cmp <reg>,<reg>
cmp <reg>,<mem>
cmp <mem>,<reg>
cmp <reg>,<con>Instance
cmp DWORD PTR [var], 10
jeq loopIf the 4 bytes stored at location var are equal to the 4-byte integer constant 10, jump to the location labeled loop.
telephone call, ret — Subroutine telephone call and return
These instructions implement a subroutine call and return. The phone call instruction first pushes the current code location onto the hardware supported stack in retentivity (run across the push instruction for details), and so performs an unconditional jump to the code location indicated by the label operand. Unlike the unproblematic jump instructions, the call instruction saves the location to return to when the subroutine completes.The ret pedagogy implements a subroutine return mechanism. This instruction offset pops a code location off the hardware supported in-retention stack (see the popular didactics for details). Information technology so performs an unconditional jump to the retrieved lawmaking location.
Syntax
call <label>
ret
Calling Convention
To allow divide programmers to share code and develop libraries for apply past many programs, and to simplify the utilize 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 set up of calling convention rules, a programmer need non examine the definition of a subroutine to determine how parameters should exist passed to that subroutine. Furthermore, given a ready of calling convention rules, high-level language compilers tin can be made to follow the rules, thus assuasive hand-coded assembly language routines and high-level linguistic communication routines to call one some other.
In do, many calling conventions are possible. We will utilize the widely used C language calling convention. Post-obit this convention will allow you to write assembly language subroutines that are safely callable from C (and C++) code, and will also enable you to call C library functions from your associates language code.
The C calling convention is based heavily on the use of the hardware-supported stack. It is based on the push, popular, 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 virtually processors have used similar calling conventions.
The calling convention is broken into two sets of rules. The outset set of rules is employed by the caller of the subroutine, and the 2nd set of rules is observed by the writer of the subroutine (the callee). It should be emphasized that mistakes in the observance of these rules quickly result in fatal plan errors since the stack will be left in an inconsistent state; thus meticulous care should exist used when implementing the phone call convention in your own subroutines.
Stack during Subroutine Call
[Thanks to Maxence Faldor for providing a correct figure and to James Peterson for finding and fixing the bug in the original version of this effigy!]
A good way to visualize the operation of the calling convention is to draw the contents of the nearby region of the stack during subroutine execution. The epitome above depicts the contents of the stack during the execution of a subroutine with three parameters and three local variables. The cells depicted in the stack are 32-bit wide memory locations, thus the retentiveness addresses of the cells are 4 bytes apart. The first parameter resides at an offset of 8 bytes from the base pointer. Above the parameters on the stack (and below the base pointer), the call instruction placed the return accost, thus leading to an extra iv bytes of offset from the base pointer to the first parameter. When the ret education is used to return from the subroutine, information technology will bound to the return address stored on the stack.
Caller Rules
To make a subrouting call, the caller should:
- Earlier 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 immune to alter these registers, if the caller relies on their values subsequently the subroutine returns, the caller must push button the values in these registers onto the stack (so they can be restore later the subroutine returns.
- To pass parameters to the subroutine, push them onto the stack earlier the call. The parameters should be pushed in inverted order (i.e. terminal parameter first). Since the stack grows downward, the outset parameter will be stored at the lowest address (this inversion of parameters was historically used to allow functions to be passed a variable number of parameters).
- To call the subroutine, utilise the call teaching. This instruction 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.
Later on the subroutine returns (immediately following the phone call instruction), the caller can expect to discover the return value of the subroutine in the register EAX. To restore the motorcar 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 code below shows a role phone call that follows the caller rules. The caller is calling a office _myFunc that takes three integer parameters. First parameter is in EAX, the 2nd parameter is the abiding 216; the third parameter is in retentivity location var.
push [var] ; Push terminal parameter first push 216 ; Push the second parameter push eax ; Button commencement parameter last telephone call _myFunc ; Telephone call the function (assume C naming) add esp, 12
Note that after the phone call returns, the caller cleans up the stack using the add together teaching. Nosotros have 12 bytes (3 parameters * four bytes each) on the stack, and the stack grows downwards. Thus, to become rid of the parameters, we can just add together 12 to the stack pointer.
The result produced by _myFunc is now 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 later on the phone 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:
- Push the value of EBP onto the stack, and then copy the value of ESP into EBP using the following instructions:
push ebp mov ebp, esp
This initial action maintains the base of operations pointer, EBP. The base pointer is used by convention equally a point of reference for finding parameters and local variables on the stack. When a subroutine is executing, the base arrow holds a re-create of the stack pointer value from when the subroutine started executing. Parameters and local variables volition always be located at known, abiding offsets away from the base pointer value. We push the old base arrow value at the beginning of the subroutine and then that nosotros tin subsequently restore the appropriate base of operations pointer value for the caller when the subroutine returns. Recollect, the caller is not expecting the subroutine to modify the value of the base pointer. Nosotros then move the stack pointer into EBP to obtain our point of reference for accessing parameters and local variables. - Next, allocate local variables by making infinite on the stack. Call back, the stack grows downward, then to brand space on the top 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 instance, if 3 local integers (4 bytes each) were required, the stack arrow would need to exist decremented by 12 to make space for these local variables (i.e., sub esp, 12). As with parameters, local variables will be located at known offsets from the base of operations pointer.
- Next, salve the values of the callee-saved registers that will exist used by the part. To save registers, push them onto the stack. The callee-saved registers are EBX, EDI, and ESI (ESP and EBP will likewise be preserved past the calling convention, but demand not be pushed on the stack during this stride).
After these 3 actions are performed, the body of the subroutine may proceed. When the subroutine is returns, it must follow these steps:
- Leave the return value in EAX.
- Restore the erstwhile values of any callee-saved registers (EDI and ESI) that were modified. The annals contents are restored by popping them from the stack. The registers should exist popped in the inverse order that they were pushed.
- Deallocate local variables. The obvious way to exercise this might be to add together the appropriate value to the stack pointer (since the space was allocated by subtracting the needed amount from the stack arrow). In do, a less mistake-prone style 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 pointer always contains the value that the stack pointer contained immediately prior to the allocation of the local variables.
- Immediately earlier returning, restore the caller'due south base arrow value by popping EBP off the stack. Recall that the first affair we did on entry to the subroutine was to push button the base pointer to salve its erstwhile value.
- Finally, return to the caller by executing a ret education. This pedagogy will find and remove the advisable return address from the stack.
Note that the callee's rules fall cleanly into two halves that are basically mirror images of one another. The get-go half of the rules utilise to the beginning of the function, and are normally said to define the prologue to the role. The latter half of the rules use to the end of the role, and are thus commonly said to define the epilogue of the function.
Example
Here is an example part definition that follows the callee rules:
.486 .MODEL FLAT .CODE PUBLIC _myFunc _myFunc PROC ; Subroutine Prologue button ebp ; Save the old base pointer value. mov ebp, esp ; Ready the new base pointer value. sub esp, 4 ; Brand room for one 4-byte local variable. push edi ; Save the values of registers that the function push button esi ; will change. This function uses EDI and ESI. ; (no need to save EBX, EBP, or ESP) ; Subroutine Body mov eax, [ebp+8] ; Motility value of parameter one into EAX mov esi, [ebp+12] ; Move value of parameter ii into ESI mov edi, [ebp+xvi] ; Motility value of parameter 3 into EDI mov [ebp-iv], edi ; Motion EDI into the local variable add [ebp-iv], 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 pop edi mov esp, ebp ; Deallocate local variables pop ebp ; Restore the caller'south base arrow 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 past decrementing the stack arrow, and saving register values on the stack.
In the trunk of the subroutine nosotros can see the utilize of the base arrow. Both parameters and local variables are located at constant offsets from the base pointer for the duration of the subroutines execution. In particular, we 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 outset parameter to the subroutine can always be plant at memory location EBP + 8, the 2nd at EBP + 12, the third at EBP + 16. Similarly, since local variables are allocated after the base pointer is set, they e'er reside above the base pointer (i.east. at lower addresses) on the stack. In particular, the first local variable is always located at EBP - iv, the second at EBP - eight, and so on. This conventional use of the base pointer allows us to chop-chop identify the use of local variables and parameters within a function body.
The office epilogue is basically a mirror image of the office prologue. The caller'due south annals values are recovered from the stack, the local variables are deallocated by resetting the stack arrow, the caller's base pointer value is recovered, and the ret instruction is used to return to the appropriate lawmaking location in the caller.
Using these Materials
These materials are released under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Us License. We are delighted when people desire to employ or adapt the course materials we developed, and y'all are welcome to reuse and suit these materials for any not-commercial purposes (if you lot would like to use them for a commercial purpose, please contact David Evans for more information). If yous do adapt or use these materials, please include a credit like "Adapted from materials developed for Academy of Virginia cs216 by David Evans. This guide was revised for cs216 past David Evans, based on materials originally created by Adam Ferrari many years ago, and since updated past 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: chiltonowayll.blogspot.com
0 Response to "How To Multiply A Register By An Integer Number Mips"
Post a Comment