Docsity

Interfacing Assembly Language with Language - Lab 7 | ECE 4270, Lab Reports of Computer Architecture and Organization

Toggle sidebar
bg1
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
pf3
pf4

Partial preview of the text

Download Interfacing Assembly Language with Language - Lab 7 | ECE 4270 and more Lab Reports Computer Architecture and Organization in PDF only on Docsity!

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

with underscores, and you must begin all assembler functions and variables that are made

PUBLIC and referenced by C code with underscore. Also remember to put EXTRN

directives in the assembly function for far symbols referenced either outside all segments

or inside the correct segment.

7. Turbo Assembler is normally insensitive to case when handling symbolic names. Since

C is case sensitive, it is desirable to have Turbo Assembler be case sensitive, at least for

those symbols that are shared between assembler and C.

8. For two column vectors A and B, inner and outer product is defined as:

DotP = A '* B = a 1 * b 1 + a 2 * b 2 + a 3 * b 3

3 1 3 2 3 3

2 1 2 2 2 3

1 1 1 2 1 3

a b a b a b

a b a b a b

a b a b a b

OuterP A B

PRELAB

1. Explain the input/output of the matrix.c file.

2. Give an assemble code for calling a .c function defined as ‘test(vector *a, long int

ret)’.

REQUIREMENTS

1. The matrix.c file will be provided in the web page and cannot be changed. You are

asked to write a module in assembly the implements the dot and outer product as separate

functions. Both take pointers to the vector structure, which is simply three 16-bit signed

integers: x, y, and z respectively, and the lvector structure which is a 32 bit version. The

first two pointers correspond to the 16-bit operands, and the third points two the 32 bit

result. The prototype of these assembly functions in C looks as follows:

extern void outP(vector* a, vector* b, lvector* result) ;

extern void dotP(vectora, vector b, long int* result) ;

The module calculates the dot product of a and b, storing the 32-bit scalar product in

result. The module also calcultates the outer product, storing the 32-bit 3 by 3 (nine)

dimensional vector in result. The operands should not be modified.

2. When the C program, matrix.c, calls the assembly routine, it passes the address of the

variable data as a variable. Since we have a small model, the procedures are near ones.

This means that only the offset of the address for variable data gets pushed on the stack.

Upon entry to the assembly routine, the top of the stack looks like the following:

[-216 -48 60]

Dot Product: : - Continue y/n [n]: n C:>

Note: the bold characters indicate the user inputs.