Concept explainers
For a function with prototype
long decoda2(long x, long y, long z);
GCC generates the following assembly code:
Parameters x, y, and z are passed in registers %rdi, %rsi, and %rdx. The code stores the return value in register %rax.
Write C code for decode2 that will have an effect equivalent to the assembly code shown.
Explanation of Solution
Given assembly code:
x in %rdi, y in %rsi and z in %rdx
decode2:
subq %rdx, %rsi
imulq %rsi, %rdi
movq %rsi, %rax
salq $63, %rax
sarq $63, %rax
xorq %rdi, %rax
ret
Load Effective Address:
- The load effective address instruction “leaq” is a variant of “movq” instruction.
- The instruction form reads memory to a register, but memory is not been referenced at all.
- The first operand of instruction is a memory reference; the effective address is been copied to destination.
- The pointers could be generated for later references of memory.
- The common arithmetic operations could be described compactly using this instruction.
- The operand in destination should be a register.
Data movement instructions:
- The different instructions are been grouped as “instruction classes”.
- The instructions in a class performs same operation but with different sizes of operand.
- The “Mov” class denotes data movement instructions that copy data from a source location to a destination.
- The class has 4 instructions that includes:
- movb:
- It copies data from a source location to a destination.
- It denotes an instruction that operates on 1 byte data size.
- movw:
- It copies data from a source location to a destination.
- It denotes an instruction that operates on 2 bytes data size.
- movl:
- It copies data from a source location to a destination.
- It denotes an instruction that operates on 4 bytes data size.
- movq:
- It copies data from a source location to a destination.
- It denotes an instruction that operates on 8 bytes data size.
- movb:
Comparison Instruction:
- The “CMP” instruction sets condition code according to differences of their two operands.
- The working pattern is same as “SUB” instruction but it sets condition code without updating destinations.
- The zero flag is been set if two operands are equal.
- The ordering relations between operands could be determined using other flags.
- The “cmpl” instruction compares values that are double word.
Unary and Binary Operations:
- The details of unary operations includes:
- The single operand functions as both source as well as destination.
- It can either be a memory location or a register.
- The instruction “incq” causes 8 byte element on stack top to be incremented.
- The instruction “decq” causes 8 byte element on stack top to be decremented.
- The details of binary operations includes:
- The first operand denotes the source.
- The second operand works as both source as well as destination.
- The first operand can either be an immediate value, memory location or register.
- The second operand can either be a register or a memory location.
Corresponding C code:
// Define method decode
long decode(long x, long y, long z)
{
// Declare variable
long tmp = y - z;
//Return
return (tmp * x)^(tmp << 63 >> 63);
}
Explanation:
- The register “%rdi” has value for “x”, register “%rsi” has value for “y” and register “%rdx” has value for “z”.
- The details of assembly code is shown below:
- The instruction “subq %rdx, %rsi” performs operation “y - z” and stores result in register “%rsi”.
- The statement “long tmp = y - z” corresponds to C code.
- The instruction “imulq %rsi, %rdi” multiplies result of operation with “x” and stores result in register “%rdi”.
- The statement “(tmp * x)” corresponds to C code.
- The instruction “movq %rsi, %rax” moves value in register “%rsi” to register “%rax”.
- The instruction “salq $63, %rax” performs left shift on value in register “%rax”.
- The statement “tmp << 63” corresponds to C code.
- The instruction “sarq $63, %rax” performs right shift on value in register “%rax”.
- The statement “tmp << 63 >> 63” corresponds to C code.
- The instruction “xorq %rdi, %rax” performs “XOR” operation on values in registers “%rax” and “%rdi”.
- The statement “return (tmp * x)^(tmp << 63 >> 63)” corresponds to C statement.
- The instruction “subq %rdx, %rsi” performs operation “y - z” and stores result in register “%rsi”.
Want to see more full solutions like this?
Chapter 3 Solutions
Computer Systems: A Programmer's Perspective (3rd Edition)
Additional Engineering Textbook Solutions
Java How to Program, Early Objects (11th Edition) (Deitel: How to Program)
C How to Program (8th Edition)
Computer Science: An Overview (13th Edition) (What's New in Computer Science)
Web Development and Design Foundations with HTML5 (9th Edition) (What's New in Computer Science)
Starting Out With Visual Basic (8th Edition)
Database Concepts (8th Edition)
- Consider the following C function: void f(int **, int a, int b) { if (x != 0) { *x += a; } return; } Write a translation of this function into assembly following the C convention for subprograms. It's ok for this function to destroy the values of registers EAX and EBX, and the function's code can only reference registers EAX, EBX, EBP, and ESP.arrow_forwardPlease Help ASAP!!!arrow_forwardFor each of the following C functions (given as function prototypes), which register is used to pass each of the parameters? Which register(s) is used to pass the return value? Note: uint64_t is unsigned 64-bit integer. a-short add2(short a, short b); b-uint16_t add3(uint8_t a, uint8_t b, uint8_t c); c-unsigned fibonacci(unsigned n); d-char* mystrcmp(char *dst, char *src) e-uint64_t sumOfArray(uint32_t X[], int N);arrow_forward
- Please Help ASAP!!!arrow_forwardIt is the compiler's job to associate program variables with registers. Take, for instance, the assignment statement from our earlier example: f (g + h) (i + j): The variables f, g, h, i, and j are assigned to the registers X19, X20, X21, X22, and X23, respectively. What is the compiled LEGv8 code?arrow_forwardfor (i=0; i<1000; i++){ y[i] = w * x[i] + b[i]; } Above C-code has a for loop which will repeat 1000 times of the operation. This code will be running on very simple machine. Change the code to reduce the number of branch instructions. You can have up to 5 lines (statements) within the for loop. What is the number of branch operations before and after?arrow_forward
- MIS asssembly Write an assembly program for the processor family x86-32 that reads three signed interger numbers from the standard input and writes the greatest of them on the console. Use the C functions scanf and printf for the management of data input and data output. Concepts in practice: Data Transfer, Input-Output Management and Conditional Jumps.arrow_forwardDescription: Implement a transistor-level schematic of a computational unit/ALU that can perform the following operations. XOR ● XNOR Multiplication Addition Subtraction ● Rotate Shift Right Magnitude Comparator ● Equality Comparator The computational unit/ALU has a decoder to decode an instruction and a multiplexer to select outputs of different operations. Data inputs are 4-bit wide. Make sure you test your design for all the operations listed above.arrow_forwardQ: One way to handle parameter conversion in RPC systems is to have each machine send parameters in its native representation, with the other one doing the translation, if need be. The native system could be indicated by a code in the first byte. However, since locating the first byte in the first word is pre- cisely the problem, can this actually work?arrow_forward
- Draw the stack frame just before the RET instruction in the swap function is executed in the code shown below:arrow_forwardWrite a assembly code for 8086/8088 that replace the contents of each cell with the sum of the contents of all the cells in the original array from the left end to the cell in question. Thus, for example, if the array passed to the function looks like this: 5 2 9 3 7 then when the function returns, the array will have been changed so that it looks like this: 5 7 16 19 26arrow_forwardTranslate the following C++ program into MAL (MIPS Assembly Langage). Your solution should have all necessary data allocations, input/output instructions etc, and must compile and run correctly in spim. Make sure your loops are efficient (i.e., they should not have unnecessary branches). Given: max is in $s0, num is in $s1, i is in $s2. (These variables should not be allocated in the .data section.) You may use any other $s? or $t? registers as temporaries. #include using std::cin; using std::cout; int main() { int max, num, i;arrow_forward
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr