High Level Language Program Interfacing-Microprocessor and Assembly Language Programming-Lecture Notes, Study notes of Microprocessor and Assembly Language Programming

This lecture handout was provided at Quaid-i-Azam University for Microprocessor and Assembly Language Programming course by Prof. Saleem Raza. Its main points are: Language, Functions, Convention, Pascal, Microprocessor, Interfacing, Registers, Return

Typology: Study notes

2011/2012

Uploaded on 08/04/2012

saqqi
saqqi 🇵🇰

4

(33)

40 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
To interface an assembly routine with a high level language program
means to be able to call functions back and forth. And to be able to do so
requires knowledge of certain behavior of the HLL when calling functions.
This behavior of calling functions is called the calling conventions of the
language. Two prevalent calling conventions are the C calling convention and
the Pascal calling convention.
What is the naming convention
C prepends an underscore to every function or variable name while Pascal
translates the name to all uppercase. C++ has a weird name mangling
scheme that is compiler dependent. To avoid it C++ can be forced to use C
style naming with extern “C” directive.
How are parameters passed to the routine
In C parameters are pushed in reverse order with the rightmost being
pushed first. While in Pascal they are pushed in proper order with the
leftmost being pushed first.
Which registers must be preserved
Both standards preserve EBX, ESI, EDI, EBP, ESP, DS, ES, and SS.
Which registers are used as scratch
Both standards do not preserve or guarantee the value of EAX, ECX, EDX,
FS, GS, EFLAGS, and any other registers.
Which register holds the return value
Both C and Pascal return upto 32bit large values in EAX and upto 64bit
large values in EDX:EAX.
Who is responsible for removing the parameters
In C the caller removes the parameter while in Pascal the callee removes
them. The C scheme has reasons pertaining to its provision for variable
number of arguments.
CALLING C FROM ASSEMBLY
For example we take a function divide declared in C as follows.
int divide( int dividend, int divisor );
To call this function from assembly we have to write.
push dword [mydivisor]
pf3

Partial preview of the text

Download High Level Language Program Interfacing-Microprocessor and Assembly Language Programming-Lecture Notes and more Study notes Microprocessor and Assembly Language Programming in PDF only on Docsity!

To interface an assembly routine with a high level language program

means to be able to call functions back and forth. And to be able to do so

requires knowledge of certain behavior of the HLL when calling functions.

This behavior of calling functions is called the calling conventions of the

language. Two prevalent calling conventions are the C calling convention and

the Pascal calling convention.

What is the naming convention

C prepends an underscore to every function or variable name while Pascal

translates the name to all uppercase. C++ has a weird name mangling

scheme that is compiler dependent. To avoid it C++ can be forced to use C

style naming with extern “C” directive.

How are parameters passed to the routine

In C parameters are pushed in reverse order with the rightmost being

pushed first. While in Pascal they are pushed in proper order with the

leftmost being pushed first.

Which registers must be preserved

Both standards preserve EBX, ESI, EDI, EBP, ESP, DS, ES, and SS.

Which registers are used as scratch

Both standards do not preserve or guarantee the value of EAX, ECX, EDX,

FS, GS, EFLAGS, and any other registers.

Which register holds the return value

Both C and Pascal return upto 32bit large values in EAX and upto 64bit

large values in EDX:EAX.

Who is responsible for removing the parameters

In C the caller removes the parameter while in Pascal the callee removes

them. The C scheme has reasons pertaining to its provision for variable

number of arguments.

CALLING C FROM ASSEMBLY

For example we take a function divide declared in C as follows.

int divide( int dividend, int divisor );

To call this function from assembly we have to write.

push dword [mydivisor]

push dword [mydividend] call _divide add esp, 8 ; EAX holds the answer

Observe the order of parameters according to the C calling conventions

and observe that the caller cleared the stack. Now take another example of a

function written in C as follows.

void swap( int* p1, int* p2 ) { int temp = *p1; *p1 = *p2; *p2 = temp; }

To call it from assembly we have to write this.

[section .text] extern _swap x: dd 4 y: dd 7

push dword y push dword x call _swap ; will only retain the specified registers add esp, 8

Observe how pointers were initialized appropriately. The above function

swap was converted into assembly by the gcc compiler as follows.

; swap generated by gcc with no optimizations (converted to Intel syntax) ; 15 instructions AND 13 memory accesses _swap: push ebp mov ebp, esp sub esp, 4 ; space created for temp

mov eax, [ebp+8] mov eax, [eax] mov [ebp-4], eax ; temp = *p

mov edx, [ebp+8] mov eax, [ebp+12] mov eax, [eax] mov [edx], eax ; *p1 = *p

mov edx, [ebp+12] mov eax, [ebp-4] mov [edx], eax ; *p2 = temp

leave ;;;;; EQUIVALENT TO mov esp, ebp AND pop ebp ;;;;; ret

If we turn on optimizations the same function is compiled into the following

code.