Assembler Programming

Lecture 10 Lecture 10

• Mixed language programming. C and Basic to MASM Interface. Mixed language programming

• Combine Basic, C, Pascal with assembler. • Call MASM routines from HLL program. • Call HLL routines from assembler program. • MASM provides some mixed language features. Mixed language programming

*.c *.asm

assembler

*.obj *.obj

*.exe Language option

• .MODEL directive – language option – PASCAL – BASIC –FORTRAN –C – SYSCALL – STDCALL • INVOKE, PROC, PUBLIC and EXTERN use the naming and calling convention of specified language. Naming and calling convention

• naming convention specifies how the compiler alters the name of an identifier before placing it into an object file. • calling convention determines how a language implements a call to a procedure and how the procedure returns to the caller. Naming convention

• How the assembler stores the names of identifiers. – SYSCALL does not change the symbols, – C, STDCALL add an underscore prefix, – PASCAL, BASIC, FORTRAN change symbols to uppercase. Naming convention

Symbol:Symbol: VariableVariable

SYSCALLSYSCALL VariableVariable

CC _Variable_Variable

STDCALLSTDCALL _Variable_Variable

PASCALPASCAL VARIABLEVARIABLE

BASICBASIC VARIABLEVARIABLE

FORTRANFORTRAN VARIABLEVARIABLE C calling convention

• Argument passing – pushed from right to left – caller cleans the stack after the call • Register preservation – must preserve BP, SI, DI, DS, SS and direction flag • Varying number of arguments – first argument in the list is always on the top of the stack Pascal calling convention

• Argument passing – pushed from left to right as they appear in the source code – called routine cleans the stack before return • Register preservation – subroutine must preserve BP, SI, DI, DS, SS and direction flag • Varying number of arguments – not possible in Pascal calling convention SYSCALL calling convention

• Argument passing – pushed from right to left – caller cleans the stack after the call • Register preservation – subroutine must preserve BP, SI, DI, DS, SS • Varying number of arguments – first argument in the list is always on the top of the stack STDCALL calling convention

• Argument passing – pushed from right to left – if procedure accepts variable number of parameters caller cleans the stack after the call, otherwise procedure cleans the stack. • Register preservation – subroutine must preserve PB, SI, DI, DS, SS – direction flag must be returned clear • Varying number of arguments – allowed for procedures declared with VARARG Conventions summarize

Convention C SYSCALL STDCALL BASIC FORTRAN PASCAL Leading + + underscore Capitalize + + + all Arguments + + + pushed left to right Arguments + + + pushed right to left Caller stack + + * cleanup VARARG + + + allowed MASM features

• PROTO directive improves error checking on argument types. • INVOKE pushes arguments onto the stack and converts argument types. • LOCAL following the PROC saves places on the stack for local variables. • PROC sets up the appropriate stack frame. • USES keyword preserves registers. • RET keyword adjusts the stack. The C/MASM interface - data types

C Type MASM Type

unsigned char BYTE

Char SBYTE

unsigned short, unsigned int WORD

int, short SWORD

unsigned long DWORD

Long SDWORD

Float REAL4

Double REAL8

long double REAL10 C/MASM Interface

• Naming restrictions – C is case sensitive, does not convert names to uppercase. – You should assemble MASM modules with /Cx or /Cp option. • Arguments passing – C passes: • arrays by reference, • other variables by value. – To pass variable as reference use the address operator (&) C/MASM Interface

• Array storage – arrays are stored in row-major order – for example the first five elements of an array with four rows and three columns are stored as: A[1, 1], A[1, 2], A[1, 3], A[2, 1], A[2, 2] • String format – stored as arrays of bytes – uses null character as the delimiter C/MASM Interface • Returning values – simple data types in registers: AL, AX, EAX, DX:AX or EDX:EAX – float and double in static variables – structures less than 4 bytes in DX:AX – structures longer than 4 bytes - copy to global variable and return pointer in AX • Compiling and Linking – Use the same memory model for both C and MASM. C/MASM - Example #include#include externextern intint Power2( Power2( intint factor, factor, intint power power );); voidvoid main()main() {{ printf("3printf("3 timestimes 22 toto thethe powerpower ofof 55 isis %d\n",%d\n", Power2(3,Power2(3, 5));5)); }}

.MODEL.MODEL small,small, cc Power2Power2 PROTOPROTO CC factor:SWORD,factor:SWORD, power:SWORDpower:SWORD

.CODE.CODE Power2Power2 PROCPROC CC factor:SWORD,factor:SWORD, power:SWORDpower:SWORD movmov ax,ax, factorfactor ;; LoadLoad Arg1Arg1 intointo AXAX movmov cx,cx, powerpower ;; LoadLoad Arg2Arg2 intointo CXCX shlshl ax,ax, clcl ;; AXAX == AXAX * * (2(2 toto powerpower ofof CX)CX) ;; LeaveLeave returnreturn valuevalue inin AXAX retret Power2Power2 ENDPENDP ENDEND Basic/MASM interface - data types

C Type MASM Type

STRING*1 WORD

INTEGER (X%) SWORD

LONG (X&), CURRENCY SDWORD

SINGLE (X!) REAL4

DOUBLE (X#) REAL8 Basic/MASM Interface

• Naming restrictions – Basic recognizes up to 40 characters. – Basic drops %, &, !, #, @ • Array storage – arrays are stored in column-major order – for example the first five elements of an array defined with DIM Arr%(3,3) are: Arr(0, 0), Arr(1, 0), Arr(2, 0), Arr(0, 1), Arr(1, 1) Basic/MASM Interface

• Arguments passing – Basic passes: • by near reference (2-byte address) – Use DECLARE statement to change arguments passing: • to pass variable by far reference use SEG, • to pass variable by value use BYVAL, • You cannot pass arrays and user types by value. Basic/MASM - Example

DECLAREDECLARE SUBSUB Test(BYVALTest(BYVAL a%, a%, b%,b%, SEGSEG c%)c%) DECLAREDECLARE SUBSUB Test2(a%,Test2(a%, b%,b%, c%)c%)

CALLCALL Test(x%, Test(x%, y%,y%, z%)z%) CALLSCALLS Test(x%,Test(x%, y%,y%, z%)z%)

CALLSCALLS Test2(x%,Test2(x%, y%,y%, z%)z%) Basic/MASM Interface

• String format – Basic maintains 4-byte string descriptor – first 2-byte filed indicates the length of the string – second field contains the offset address

DESCDESC STRUCTSTRUCT lenlen WORDWORD ?? ;; LengthLength ofof stringstring offoff WORDWORD ?? ;; OffsetOffset ofof stringstring DESCDESC ENDSENDS stringstring BYTEBYTE "Text"Text referencedreferenced byby aa descriptor"descriptor" sdescsdesc DESCDESC (LENGTHOF(LENGTHOF string,string, string)string) Basic/MASM Interface • Returning values – 2-byte integers in AX – 4-byte integers in DX:AX – all other types as near offset in AX • Compiling and Linking – Always use medium model in assembly-language procedures linked with Basic modules. Basic/MASM - Example DEFINTDEFINT A-ZA-Z

DECLAREDECLARE FUNCTIONFUNCTION Power2Power2 (A(A ASAS INTEGER,INTEGER, BB ASAS INTEGER)INTEGER) PRINTPRINT "3"3 timestimes 22 toto thethe powerpower ofof 55 isis ";"; PRINTPRINT Power2(3,Power2(3, 5)5)

ENDEND

.MODEL.MODEL mediummedium Power2Power2 PROTOPROTO PASCAL,PASCAL, factor:PTRfactor:PTR WORD, WORD, power:PTRpower:PTR WORD WORD

.CODE.CODE Power2Power2 PROCPROC PASCAL,PASCAL, factor:PTRfactor:PTR WORD, WORD, power:PTRpower:PTR WORD WORD movmov bx,bx, WORDWORD PTRPTR factorfactor ;; BXBX pointspoints toto factorfactor movmov ax,ax, [bx][bx] ;; LoadLoad factorfactor intointo AXAX movmov bx,bx, WORDWORD PTRPTR powerpower ;; BXBX pointspoints toto powerpower movmov cx,cx, [bx][bx] ;; LoadLoad powerpower intointo CXCX shlshl ax,ax, clcl ; ; AXAX == AXAX * * (2(2 toto powerpower ofof CX)CX) retret Power2Power2 ENDPENDP ENDEND Example 1

/*/* ExampleExample to to illustrateillustrate C C andand assembly language interface.interface. TheThe test test functionfunction is is written written in in assembler assembler */ */

#include#include intint main main (void) (void) {{ intint x=25, x=25, y=70;y=70; intint value; value; externextern int int test(int, test(int, int,int, int);int);

valuevalue = = testtest (x,(x, y,y, 5);5); printfprintf („result= („result= %d\n”,%d\n”, value);value); returnreturn 0; 0; }} Example 1

;; AssemblyAssembly program program forfor thethe test test functionfunction ;; calledcalled from from the the C C programprogram

.MODEL.MODEL SMALLSMALL .CODE.CODE PUBLICPUBLIC _test_test _test_test PROCPROC pushpush BP BP movmov BP, BP, SPSP movmov AX, AX, [BP+4][BP+4] ;; getget argument1 argument1 (x)(x) addadd AX, AX, [BP+6][BP+6] ;; addadd argument2 argument2 (y)(y) subsub AX, AX, [BP+8][BP+8] ;; substractsubstract argument3 argument3 (5)(5) fromfrom sum sum poppop BPBP retret ;; stackstack is is cleared cleared by by thethe C C functionfunction _test_test ENDPENDP ENDEND Example 2

/*/* AA stringstring processing processing example example to to illustrateillustrate global global variables. variables. TheThe string_length string_length function function is is written written in in assembler assembler */ */

#include#include #define#define LENGTH LENGTH 256256 charchar string[LENGTH]; string[LENGTH]; intint main main (void) (void) {{ externextern int int string_length string_length (char (char a[]); a[]);

printfprintf („Enter („Enter string: string: ”);”); scanfscanf („%s”,(„%s”, string);string); printfprintf („string („string length= length= %d\n”,%d\n”, string_length());string_length()); returnreturn 0; 0; }} Example 2 ;; string_lengthstring_length function function works works on on thethe global global variable variable ;; defineddefined in in the the C C programprogram .MODEL.MODEL SMALLSMALL .DATA.DATA EXTRNEXTRN _string:byte_string:byte .CODE.CODE PUBLICPUBLIC _string_length_string_length _string_length_string_length PROC PROC movmov AX, AX, 00 ;; AXAX keepskeeps the the character character count count movmov BX, BX, OFFSETOFFSET _string_string ;; loadload BX BX withwith string string address address repeat1:repeat1: cmpcmp BYTE BYTE PTRPTR [BX],[BX], 00 ;; loadload with with NULL NULL charactercharacter jzjz done done incinc AX AX ;; incrementincrement string string length length incinc BX BX ;; incinc BX BX toto pointpoint toto nextnext char char jmpjmp repeat1 repeat1 done:done: retret ;; stackstack is is cleared cleared by by thethe C C _string_length_string_length ENDP ENDP ENDEND