CIS-261 Home http://www.c-jump.com/bcc/

Lab M13. Using Assembly with High-Level Languages


  1. Introduction
  2. Procedure Name Coordination
  3. Calling Conventions
  4. Example Calling ASM Procedure from C++
  5. M13_COPYSTR.ASM
  6. M13_main.cpp
  7. Invoking COPYSTR Procedure from C++
  8. Quick Disassembly Examination of Executable Image
  9. Invoking a C Function from Assembly
  10. Final Project: "Running Total" Calculator
  11. Running Total Prototype
  12. Implementation Overview
  13. Things To Do
  14. What to Submit

1. Introduction



2. Procedure Name Coordination



3. Calling Conventions



4. Example Calling ASM Procedure from C++




; M13_COPYSTR.ASM

.586P
.MODEL FLAT, stdcall ; Flat memory model
PUBLIC COPYSTR

_TEXT SEGMENT
; Procedure for copying the null-terminated source string to the target string.
;
; Input:
;   str_dest...Target string [ EBP + 08H ]
;   str_src....Source string [ EBP + 0CH ]
; Output:
;   EAX........address of the target string
;
; WARNING! target string length is unchecked
;
COPYSTR PROC  str_dest: DWORD, str_src: DWORD
        MOV   ESI, str_src  ; DWORD PTR [ EBP + 0CH ]
        MOV   EDI, str_dest ; DWORD PTR [ EBP + 08H ]
L1:
        MOV   AL, BYTE PTR [ESI]
        MOV   BYTE PTR [EDI], AL
        CMP   AL, 0
        JE    L2
        INC   ESI
        INC   EDI
        JMP   L1
L2:
        MOV   EAX, DWORD PTR [ EBP + 08H ]
        RET
COPYSTR ENDP

_TEXT ENDS

END



// M13_main.cpp

#include <iostream>

extern "C" int __stdcall COPYSTR( char*, char const* );

int main()
{
    char destination[ 100 ] = { 0 };
    char const* source = "Hello!";

    COPYSTR( destination, source );
    std::cout << destination;
    return 0;
}

5. Invoking COPYSTR Procedure from C++



6. Quick Disassembly Examination of Executable Image



7. Invoking a C Function from Assembly



8. Final Project: "Running Total" Calculator



9. Running Total Prototype



10. Implementation Overview



11. Things To Do



12. What to Submit