
ECE 4270/7210 LABORATORY 7
INTERFACING ASSEMBLY LANGUAGE WITH C LANGUAGE
Spring 2009
OBJECTIVE
The interfacing between C language and assembly language is learned in this lab
assignment. A program in C is given, so you are asked to write a program in assembly for
linking with the provided C program. The approach of interfacing subroutines with
parameter passing is the key to perform this work successfully. After finishing this lab,
you should be familiar with the procedure of C called by assembler or assembler called
by C.
BACKGROUND
1. Although it is common to call assembly functions from C to perform a specialized
task, such as hardware access or performance timing minimization, etc., you may
occasionally want to call C functions from assembler. As it turns out, it is actually easier
to call a C function from an assembly function than the reverse, since no stack-frame
handling on the part of the assembler code is required. One case in which you may want
to call a C function from assembler is when you need to perform complex calculations.
This is especially true when mixed integer and floating-point calculations are involved.
2.Upon return from a function, the parameters that were pushed on the stack are still
there, but are no longer of any use. Consequently, immediately following each function
call, Turbo C adjusts the stack pointer back to the value it contained before t he
parameters were pushed, thereby discarding the parameters. Assembly functions can
access parameters passed on the stack relative to the BP register after loading the BP with
SP. However, you must first preserve BP, since the calling C code expects you to return
with BP unchanged.
3.Turbo C passes parameters to functions on the stack. Before calling a function, Turbo C
first pushes the parameters to that function on the stack, starting with the rightmost
parameter and ending with the leftmost parameter.
4.As far as Turbo C is concerned, C-callable assembly functions can do anything they
please, as long as they preserve the following registers: BP, SP, CS, and SS. AX, BX,
CX, DX, ES, and the flags can be changed in any way. It is a good practice to always
preserve SI and DI in your C-callable assembly functions.
5.In general, 8 and 16-bit values are returned in AX, and 32-bit values are returned in
DX:AX. Floating-point values are returned in ST(0), which is the 8087’s top-of-stack
(TOS) register or in the 8087 emulator’s TOS register if the floating-point emulator is
being used.
6. Normally, Turbo C expects all the external labels to start with an underscore character
"_". You must be sure that all assembler references to C functions and variables begin