Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: de/asm/okmul.c 1 2 3 4 5 6 7 8 9 /* First attempt. Does not work */ int ok_smul1(int x, int y, int *dest) { int result = 0; *dest = x*y; asm("setae %al"); return result; } 3.15. *EMBEDDING ASSEMBLY CODE IN C PROGRAMS 189 code/asm/okmul.c The strategy here is to exploit the fact that register %eax is used to store the return value. Assuming the compiler uses this register for variable result, the first line will set the register to 0. The inline assembly will insert code that sets the low-order byte of this register appropriately, and the register will be used as the return value. Unfortunately, GCC has its own ideas of code generation. Instead of setting register %eax to 0 at the beginning of the function, the generated code does so at the very end, and so the function always returns 0. The fundamental problem is that the compiler has no way to know what the programmer’s intentions are, and how the assembly statement should interact with the rest of the generated code. By a process of trial and error (we will develop more systematic approaches shortly), we were able to generate working, but less than ideal code as follows: code/asm/okmul.c 1 2 3 4 5 6 7 8 9 10 11 12 /* Second attempt. int dummy = 0; Works in limited contexts */ int ok_smul2(int x, int y, int *dest) { int result; *dest = x*y; result = dummy; asm("setae %al"); return result; } code/asm/okmul.c This code uses the same strategy as before, but it reads a global variable d...
View Full Document

This note was uploaded on 09/02/2010 for the course ELECTRICAL 360 taught by Professor Schultz during the Spring '10 term at BYU.

Ask a homework question - tutors are online