The samples below include one C file and three different assembly files.
The three assembly files demonstrate how to pass variables by reference in
small model and large model for MS-DOS and in flat model for Windows NT.
Link only the appropriate assembly module to the C module.
Note MASM 6.1 or later and the C/C++ 32-bit compiler that ships with
Visual C++, 32-bit Edition, are required to build the flat model Windows NT
version.
; Filename: MASMSUB.ASM
; Assemble options needed for MASM: /MX
; Assemble options needed for ML: /c /Cx
.MODEL small, C
.286
.CODE
MasmSub PROC uses si, \
arraychar:PTR, \
arrayshort:PTR, \
arraylong:PTR
mov si, arraychar ; Load SI with the address of the char array.
mov BYTE PTR [si], "x" ; Since a char is 1 byte long, each
mov BYTE PTR [si+1], "y" ; successive element can be accessed
mov BYTE PTR [si+2], "z" ; by adding 1 more to si.
mov si, arrayshort ; Load SI with the address of the short array.
add WORD PTR [si], 7 ; Since a short is 2 bytes long, each
add WORD PTR [si+2], 7 ; successive element can be accessed
add WORD PTR [si+4], 7 ; by adding 2 more to si.
mov si, arraylong ; Load SI with the address of the long array.
add WORD PTR [si], 1 ; Since a long is 4 bytes long, each
adc WORD PTR [si+2], 0 ; successive element in the array
add WORD PTR [si+4], 1 ; can be accessed by adding 4 more
adc WORD PTR [si+6], 0 ; to si (or 4 more to si+2 to access
add WORD PTR [si+8], 1 ; the high word of each element).
adc WORD PTR [si+10], 0
ret
MasmSub ENDP
END
; Filename: MASMSUB.ASM
; Assemble options needed for MASM: /MX
; Assemble options needed for ML: /c /Cx
.MODEL large, C
.286
.CODE
MasmSub PROC uses es si, \
arraychar:PTR, \
arrayshort:PTR, \
arraylong:PTR
les si, arraychar ; Load ES:SI with the address of the char array.
mov BYTE PTR es:[si], "x" ; Since a char is 1 byte long, each
mov BYTE PTR es:[si+1], "y" ; successive element can be accessed
mov BYTE PTR es:[si+2], "z" ; by adding 1 more to si.
les si, arrayshort ; Load ES:SI with the address of the short array.
add WORD PTR es:[si], 7 ; Since a short is 2 bytes long, each
add WORD PTR es:[si+2], 7 ; successive element can be accessed
add WORD PTR es:[si+4], 7 ; by adding 2 more to si.
les si, arraylong ; Load ES:SI with the address of the long array.
add WORD PTR es:[si], 1 ; Since a long is 4 bytes long, each
adc WORD PTR es:[si+2], 0 ; successive element in the array
add WORD PTR es:[si+4], 1 ; can be accessed by adding 4 more
adc WORD PTR es:[si+6], 0 ; to si (or 4 more to si+2 to access
add WORD PTR es:[si+8], 1 ; the high word of each element).
adc WORD PTR es:[si+10], 0
ret
MasmSub ENDP
END
; Filename: MASMSUB.ASM
; Assemble options needed for ML: /c /Cx /coff
.386
.MODEL flat, C
.CODE
MasmSub PROC uses esi, \
arraychar:PTR, \
arrayshort:PTR, \
arraylong:PTR
mov esi, arraychar ; Load ESI with the address of the char array.
mov BYTE PTR [esi], "x" ; Since a char is 1 byte long, each
mov BYTE PTR [esi+1], "y" ; successive element can be accessed
mov BYTE PTR [esi+2], "z" ; by adding 1 more to esi.
mov esi, arrayshort; Load ESI with the address of the short array.
add WORD PTR [esi], 7 ; Since a short is 2 bytes long, each
add WORD PTR [esi+2], 7 ; successive element can be accessed
add WORD PTR [esi+4], 7 ; by adding 2 more to esi.
mov esi, arraylong ; Load ESI with the address of the long array.
inc DWORD PTR [esi] ; Since a long is 4 bytes long, each
inc DWORD PTR [esi+4] ; successive element can be accessed
inc DWORD PTR [esi+8] ; by adding 4 more to esi.
ret
MasmSub ENDP
END