Lab 5
Lab 5
Lab 5
Spring 2011
Department of Computer Science
Texas A&M University
Compiler and assembler: Compiler is the tool that transforms the high level source code to
executable machine code. Whereas assembler is the tool that transform your assembly
language code to machine code. Compilers first transform high level source codes to equivalent
assembly codes, then assembler component of the compiler assembles the assembly code to
corresponding machine formats. Note at this point that the generated machine format code is not
ready to be executed as yet. This is explained in subsequent paragraphs.
Source code in high
level language
(C/ FORTRAN)
gcc
Compilation process
Compilation proper
Activates/drives
driver
Assembly code(s)
Assembling
processor is cumbersome and time consuming, so often a virtual processor is used. This virtual
processor is actually a program which can run on your desktop. It can read the machine language
code specifically written for a target processor and generate the memory and registers state as
the real physical processor would had this same program been executed on it. Sometime such a
program is also called processor emulator or processor simulator or instruction simulator.
Emulators or simulators for the entire system are also available. These simulate or emulate the
entire behavior of the system, processor chips, motherboard and all the IO systems. These are
often used by system developers and designers to evaluate OS, kernel codes and embedded
system applications. Simulators are especially valuable to troubleshoot codes, this is because it is
possible to see and tracks values inside the register and memory locations inside a simulator
while a code is stepped through one instruction at a time. Whereas in real physical processor, the
entire code will get executed so fast that you will not get any opportunity to know what had
happened in between the instructions.
One can have both software and hardware based (FPGA implementations) simulators for a
hardware component like processor. Software based simulators which simulates hardware
systems are slower than the actual hardware, whereas hardware based simulators are faster than
the software simulators. The benefit of software simulators are that they can be readily modified,
and invasively instrumented to note any arbitrary internal state at will. With the present
technology, hardware based simulators do not readily provide such flexibility and richness.
Simulators may only simulate a part of the system functionality or limited behavioral aspects, so
it is important to know what behavior the simulator is actually simulating, and how to interpret
the results.
Specificity of gcc: A platform is a combination of the hardware (processor and the peripherals),
OS and the compiler. gcc is available for only few specific OS like Linux, BSD and Mac OS.
Gcc can only compile, assemble or link codes for specific set of target processors. If no
specific target is mentioned then by default gcc assumes the target to be the host processor on
which it is currently running. Note that gccs assembler can only understand assembly languages
for the target processors. This means vanilla gccs assembler would not be able to understand
Y86 assembly language, unless somebody modifies gcc to do that.
AT&T and Intel assembly syntax: This pertains to source-destination ordering. In Intel syntax
the first operand is the destination, and the second operand is the source whereas in AT&T it is
opposite. Intel syntax assembly will say "Op-code dst src" but AT&T syntax will say it as "Opcode src dst". This is good to know. So you would know what the Y86 toolset and the text book
follows. gccs assembler uses AT&T syntax.
How to embed assembly instruction inside C code: The good news is assembly and C
programming can be mixed with each other provided the compiler supports it. Gcc for Linux
supports IA32 statement embedding in C. The basic form of the embedded statement is either
of the following two alternative (any one can be used)
asm("assembly code"); //Plain old embedding
__asm__("assembly
something else
code");
//Just
in
case
asm
keyword
is
used
for
Examples -
4. To generate the Y86 object code for the Y86 assembly code (for example the file
asum.ys) type the following
./yas asum.ys
This will generate an object file with name asum.yo.
5. To execute this object file generated in the previous step, on the Y86 instruction
simulator (yis) type the following
./yis asum.yo
You will get to see how the values in the registers and memory locations change as result
of this. A trace of the above commands for the program named asum.ys, which can be
found in the example Y86 assembly code folder in the tool installation, is illustrated
below.
[user@linux misc]$ ./yas asum.ys
[user@linux misc]$ ./yis asum.yo
Stopped in 46 steps at PC = 0x3a. Exception 'HLT', CC Z=1 S=0 O=0
Changes to registers:
%eax: 0x00000000
0x0000abcd
%ecx: 0x00000000
0x00000024
%ebx: 0x00000000
0xffffffff
%esp: 0x00000000
0x000000f8
%ebp: 0x00000000
0x00000100
%esi: 0x00000000
0x0000a000
Changes to memory:
0x00f0: 0x00000000
0x00f4: 0x00000000
0x00f8: 0x00000000
0x00fc: 0x00000000
0x00000100
0x00000039
0x00000014
0x00000004
5. Useful resources:
1. Read Bryants book section 4.1, 3.1 to 3.7, 3.15, 4.3.2 to 4.3.5, 7.1 to 7.9, 7.14
2. Complete reading all the necessary reading materials, tips and additional materials given
in the Chap 5 of this lab manual.
6. Exercises to do
6.1 Problem 1: The following C program (code fragment) is given
int i,j;
..
if (i >j) {
i= i+5;
}
else {
i=0;
j++;
}
Activities to do1. Provide the Y86 assembly language code for the C program.
2. Verify this assembly code using either the Y86 tool set.
Tip: See the example Y86 code files. Read Bryants book section 3.1 to 3.6.
3. Report your assumptions used to test this code.
6.2 Problem 2: For the following C program (code fragment)
int j,k;
..
for (int i=0; i <5; i++) {
j = i*2;
k = j+1;
}
Activities to do1. Provide the Y86 assembly language code for the C program.
2. Verify this assembly code using the Y86 tool set and state your assumptions.
6.3 Problem 3: For the following two C programs
//Program 1, file name lab5_prob3_1.c
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("Hello, world\n");
return 0;
}
Activities to do1. Rewrite the above C code such that the very_fast_function() is implemented in Y86
or IA32 assembly language embedded in the C code.
Note: You cannot embed Y86 in GCC, since GCC does not understand Y86. If you use
Y86, you will have to implement the entire program.
Tips: Read Chap 3, sec 3.15 in Bryants book. You can also read the following links http://home.pacbell.net/bill_gi/bill/mentor_asmC.txt
http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html
http://tldp.org/HOWTO/html_single/Assembly-HOWTO/
http://www.cs.virginia.edu/~clc5q/gcc-inline-asm.pdf
http://www.ibm.com/developerworks/library/l-ia.html
http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html
2. Discuss what precautions you need to take about which registers to use or not use. You
can discuss this keeping in mind a more realistic context such as IA32 and Intel
processors if you like. However discussion based on Y86 is are also acceptable if you can
communicate your point and assumptions. Clearly identify which register you would use,
and which register you would not use and why. You can find the clues in the tip.
Tip: http://courses.ece.uiuc.edu/ece390/books/labmanual/c-prog-mixing.html
6.7 Problem 7: Your manager asked to compare the speed performances of two processor based
systems (hardware, OS and compiler together) which he wanted to buy to run his graphical
computer aided design (CAD) application. You figured out that the best way to quickly compare
their performances is to compare the execution time of a code (popularly called benchmarks)
on these two target hardware systems. You found that gcc can generate code for a variety of
processors. You identified that these machines are based on processors which have registers that
can precisely count clock cycles.
Activities to do1. Identify two real-life processors which provide registers to record processor clock cycles.
Report the names of these registers.
2. Identify three target processors for which gcc can generate binary executables or
assembly codes. Tip: Try using the command man gcc on the linux.cs machine.
3. Explain a plan to precisely record execution time of a code using these registers. Provide
the instrumentation code fragment in Y86 assembly language, with clear textual
explanation. Assume that the processor complies with the Y86 ISA. Assume that the
name of this 32 bit clock counter register is %ccnt (this is over and above the Y86
registers mentioned in your text book). This register gives the value number of clock
cycles happened so far and it overflows by rolling over to zero. Assume that the
processor speeds are between 1Ghz and 4 Ghz. You need not verify this code on Y86
simulator.
4. (a) Discuss all the limitations of this approach (from the previous activity). Can you
effectively measure very large execution times? How large executions times (to the
nearest microsecond) can you measure? How accurate can the measurements be? What
are
the
reasons
for
such
inaccuracies?
(b) For measuring very large execution times (say in the order of minutes) what alternate
method would you suggest? Explain that alternate scheme. What is the limitation of this
alternative scheme? (If you use the gettimeofday() approach, you must explain the two
most common errors that could occur if it was run on a modern multiprocessor system
with a multi-tasking , multi-user operating system such as Linux.)
Tip: See the code in problem#1 of Lab1. Also research on the issues of using
gettimeofday() to time your code on a modern multi-core / multi-processor system.
5. Assume that the for loop given in problem # 2 is your benchmark code. Instrument
that given C source code with suitable embedded assembly code fragments identified in
the previous activity # 2 of this problem. (Assume that the C compiler can support Y86
assembly embedding, as gcc does for IA32). You dont have to actually compile/run this
instrumented code with any compiler or simulator toolset for this activity.
6. In lab-1 (and lab-3) we used clock_gettime() to obtain timestamps. The x86architecture contains a hardware register called TSC (TimeStampCounter) which can be
read using the RTDSC assembly instruction. The TSC stores the number of clock-ticks
since the microprocessor was reset. However, using this instruction on modern systems
with more than one core/processor and a multi-tasking operating system is a bad idea.
Why? List at least two issues that can crop up if the rtdsc approach of timing is used.