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
EBK COMPUTER SYSTEMS
Additional Engineering Textbook Solutions
Database Concepts (8th Edition)
Degarmo's Materials And Processes In Manufacturing
Starting Out with C++: Early Objects (9th Edition)
Starting Out with C++ from Control Structures to Objects (9th Edition)
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
Java: An Introduction to Problem Solving and Programming (8th Edition)
- Consider the data path below for a single cycle 32-bits MIPS processor Assume that we are executing the following instruction lw $11, 48($10) What is the value of the element pointed by arrow number 1 by in hexadecimal or binary? Note that the PC and the content of registers $10 and $t1 are found in bottom left of the figure below Data Memory Select one a. Ob 01001' O b. 0x000016F0 Oc 06'1000' Od 06/00000 Oe. None of the options Of. 0x000016F4 AM RegDe ALU Address Content 0x000016EC 0x0000ABOD 0x000016F0 0x00A01245 0x000016F4 0x00001A42 0x000171C Ox0008124F 0x0001720 Ox00021345 0x0001724 0x000067AB ALU AL 0x0001734 Ox0000AB35 0x0001738 0x0000FA72 0x000174C 0x0000ABOC Register File and PC Sto Ox000016F0 $t1 = 0x000016F4 PC 0x000014BA0 (Before executing LW)arrow_forwardConsider the data path below for a single cycle 32-bits MIPS processor Assume that we are executing the following instruction ADD $3, $50, $2 What is the value of the element pointed by arrow number 1 by in hexadecimal? Note that the PC and the content of registers $10 and $t1 are found in bottom left of the figure below Select one Oa. OxFFFFFF30 b. None of the options Oc 0xFFFF9820 Od 0x00009620 Oe. 0xFFFFFF48 Address 0x000016EC Content 0x0000ABOD Data Memory 0x000016FO 0x00401245 0x000016F4 0x00001A42 0x0001710 0x0008124F 0x0001720 0x00021345 0x0001724 0x000067AB 11 81 0x0001734 0x0000AB35 0x0001738 0x0000FA72 0x0000ABOC Register File and PC 0x000174C $50 = 0x0000AF00 $12 = 0x000000BA $x3 = 0x00000001 PC-0x000FAC04 (Before executing ADD)arrow_forwardConsider the data path below for a single cycle 32-bits MIPS processor Assume that we are executing the following instruction ADD $3, $50, $s2 What is the value of the element pointed by arrow number 1 by in hexadecimal? Note that the PC and the content of registers are found in bottom left of the figure below Select one a. Ox000FACOB Ob. 0x000F4428 Oc None of the options Od 0x00120C84 O e. 0x000FAC04 Address Ox000016EC Data Memory 0x000016F0 Content 0x0000ABOD C0001245 Ox000016F4 0x00001A42 0x000171C C0008124F 0x0001720 0x00021345 0x0001724 0x000067AB 0x0001734 0x0000AB35 0x0001738 0x0000FA72 0x000174C ОКООООАВОС Register File and PC $50-0x0000AF00 Ss2=0x000000BA Ss3 0x00000001 PC-Ox000FAC04 (Before executing ADD)arrow_forward
- Consider the data path below for a single cycle 32-bits MIPS processor Assume that we are executing the following instruction lw $11, 48($10) What is the value of the element pointed by arrow number 1 by in hexadecimal? Note that the PC and the content of registers $10 and $t1 are found in bottom left of the figure below Data Memory Select one O a. 0x00001720 Ob. 0x000067AB Oc 0x0000FA72 d. 0x00021345 Oe. 0x0000ABOC ALU ALM 18 Address Content 0x000016EC Ox0000ABOD 0x000016F0 0x00A01245 0x000016F4 0x00001A42 0x000171C 0x0008124F 0x0001720 Ox00021345 0x0001724 0x000067AB 0x0001734 Ox0000A835 0x0001738 0x0000FA72 0x000174C 0x0000ABOC Register File and PC $to = 0x000016F0 St1=0x000016F4 PC = 0x000014BA0 (Before executing LW)arrow_forwardPROJECT TOPIC: Fetal ECG visualization and analysis on synthetic dataset, study the available ECG data and create a system to visualize and generate new synthetic data using the knowledge learned in this course. 1.1 Project Report By the end of the project, you will be summarizing the relevant learnings and submit the project report. The report will have the following components. - Cover page and consent form (please use the standard cover page and consent form posted on Blackboard) Table of contents - Introduction Methodology - Results Discussion and conclusion - References Appendices Please properly label and caption all the figures in your report. Any non-original material used in your report needs to come with proper citations, please use IEEE format for all your references. Please format your report in pdf format, 1.5 lines-spaced with 11 pt. font.arrow_forward3. What will be the output of the C program? 1 #include 2 3 { 4 5 6 int f(int x, int *py, int **ppz) int y, z; **ppz += 1; Z = **ppz; 12 int main() 13 { 14 int c, *b, **a; 15 c = 8; 7 *py += 3; 16 b = &c; 8 у = *ру; 17 a = &b; 9 x += 4; 18 10 return x + y + z%;B 19 printf("%d\n", f(c,b,a)); return 0; 11 } 20 } A. 31 B. 32 C. 33 D. 34arrow_forward
- 1. Declare a variable named myPointer as a pointer of type int, initializing the pointer to NULL in the declaration.arrow_forward2. What does the code output (assume we can compile it with no errors)? letterPointer = &userLetter; = userLetter 'A'; *letterPointer = 'C'; printf("%c", userLetter); A. A B. B C. Carrow_forwardA processor fetches the instruction ADD R1, R2, R3, where register R2 holds the v alue 0x08 and register R3 holds the value Describe the sequence of operations (fetch and execute cycles) the processor performs to execute this instruction. Assume this is a single-cycle processor.arrow_forward
- A 24-bit machine has all the registers and instructions shown in Figure 2 (the addresses and opcodes shown are in hexadecimal format): How many bits are required for PC, AC and IR? Draw the program execution of the Remember, the figure shows that there are 5 instructions need to be executed. Thus, yo ur answer should have exactly 10 steps (2 steps per instruction).arrow_forwardConsider a computer with the following characteristics: total of 1Mbyte of m ain memory; a block size of 64 bytes; and a cache size of 128Kbytes.All addresses and integers are represented in hexadecimal format. For the main memory addresses of 0x12354 and 0x420E0, give the corresponding tag, cache line address, and word values for a direct-mapped Given the following memory addresses, identify which addresses that will be stored into the same line in the 0x1F376 0x2A736 0x0A712 For the main memory addresses of 0x31112 and 0x0F342, give the corresponding tag and offset values for a fully-associative For the main memory addresses of 0x11255 and 0x3422E, give the corresponding tag, cache set, and offset values for a two-way set-associativearrow_forwardThe common software interrupt used here is INT 21H for DOS services. It is used for multiple purposes: a) Function 01H: Keyboard input MOV AH, 01h INT 21h b) Function 02H: Display a character, whose ASCII code is in DL. MOV AH, 02h MOV DL, character INT 21H c) Function 09H: Print string, DX contains pointer to the character string end with $ MOV AH, 09h LEA DX, msg put in msg print on screen what you INT 21H d) Function 4CH: Terminate current process MOV AX, 4C00h INT 21H By using the above interrupt function, write the following code: Quiz on Monday 25/11/2024 Write a simple assembly language code to copying string using the LODSB and STOSB instructions, which copies a byte from the source string to the destination string. The string is I am in the Second Stage in IT Department" Quiz on Wednesday 27/11/2024 Write a simple assembly language code to input any random character from screen and exit (return back to the emulator) if you press (Enter -ODH) key? Solve the question on the…arrow_forward
- Computer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Concepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningPrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY