首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 嵌入开发 > 汇编语言 >

VC怎么调用汇编子程序

2012-02-24 
VC如何调用汇编子程序?编写两个汇编语言子程序,分别返回系统的当前日期和时间,编写一个C语言程序,调用这两

VC如何调用汇编子程序?
编写两个汇编语言子程序,分别返回系统的当前日期和时间,编写一个C语言程序,调用这两个汇编语言子程序,显示系统的当前日期和时间。
程序运行时显示的信息格式为:
Current   date   is   10-30-2001.   Current   time   is   10:23:26.12
我的实现如下,为什么不能编译,我已经在VC工程那link了obj文件:
  #include <stdio.h>
#include <iostream.h>
  extern   "C "   int   get();
int   main()
{
   
    get();
   
    return   0;
}

汇编子程序:(单独执行能够成功)
;==========================================   ;get.asm
;A   little   assembly   app   that   shows   the   current   date   and   time.  
;It   can   be   done   a   lot   easier,   but   this   way   you   will  
;see   how   to   do   some   basic   memory   manipulation,   and   how   to   use   'variables '.  
;==========================================  

.model   small  
.stack  

;===========================  
;Data   segment   starts   here  
;===========================  
.data  
date_str   DB   "Current   date   is:   yyyy-mm-dd ",   0Ah,   0Dh,   "$ "  
time_str   DB   "Current   time   is:   hh.mm.ss:xx ",   0Ah,   0Dh,   "$ "  
min_size   DW   ?  
padd_chr   DB   ?  


;===========================  
;Code   segment   starts   here  
;===========================  
.code  
                            PUBLIC   _get
;main  
      _get             PROC  
MOV   AX,   SEG   @data   ;First   we   get   the   data   segment   address  
MOV   DS,   AX   ;and   store   it   into   ds  

MOV   [min_size],   02h   ;Results   should   always   be   at   least   two   digits  
MOV   [padd_chr],   '0 '   ;Use   '0 '   as   padding-character  

MOV   AH,   2Ah   ;Then   we   call   int   21h,2Ah,   which   will   give  
INT   21h   ;us   the   current   date  

LEA   DI,   date_str   ;Then   we   load   the   address   of   the   date_str   string  
ADD   DI,   17   ;and   set   si   to   point   at   the   first   y   in   yyyy-...  


MOV   AX,   CX   ;Next   we   mov   cx   to   ax   and  
CALL   todec   ;call   todec  
INC   DI   ;We   skip   the   '- '   character...  

XOR   AX,   AX   ;Then   we   empty   ax  
MOV   AL,   DH   ;And   set   the   low-byte   of   ax   to   dh  
CALL   todec  
INC   DI   ;Skip   character   in   string...  

XOR   AX,   AX   ;Empty   ax  
MOV   AL,   DL   ;Set   low-byte   to   dl  


CALL   todec   ;Convert   it   to   base10  


LEA   DI,   time_str   ;Now   we   load   the   time_str   string  
ADD   DI,   17   ;And   set   the   correct   pointer   offset  

MOV   AH,   2Ch   ;And   then   we   call   int   21h,2Ch  
INT   21h   ;which   will   give   us   the   current   time  


XOR   AX,   AX   ;Empty   ax  
MOV   AL,   CH   ;Set   low-byte   to   ch  
CALL   todec   ;Convert   it   to   base10  
INC   DI   ;Skip   character  

MOV   AL,   CL   ;Set   low-byte   to   cl  
CALL   todec   ;Convert   to   base10  
INC   DI   ;Skip   character  

MOV   AL,   DH   ;Set   low-byte   to   dh  
CALL   todec   ;Convert   to   base10  
INC   DI   ;Skip   character  

MOV   AL,   DL   ;Set   low-byte   to   dl  
CALL   todec   ;Convert   to   base10  


MOV   DX,   OFFSET   date_str   ;Now   load   offset   of   the   date_str   string   into   dx  
CALL   print   ;And   print   the   (modified)   string  

MOV   DX,   OFFSET   time_str   ;Load   offset   of   the   time_str   string   into   dx  
CALL   print   ;And   print   the   (modified)   string  

MOV   AX,   4C00h   ;Do   a   clean   exit(error   code=00)  
INT   21h  

;===================================================================  
;todec   -   converts   the   contents   of   ax   into   base10   ascii   character(s)  
;   of   length   bx  
;   min_size   defines   minimum   length   of   result,   and   padd_char  
;   defines   the   padding   character.  
;The   result(s)   are   stored   at   ds:di  
;===================================================================  
todec   PROC  
PUSH   AX   ;Save   all   registers  
PUSH   BX  
PUSH   CX  
PUSH   DX  


XOR   CX,CX   ;Empty   the   POP   counter  
MOV   BX,10   ;Base   divisor  
decloop:  
XOR   DX,DX   ;Set   the   high   16-bits   to   0  
DIV   BX   ;Preform   division(dx=remainder,   ax=quotient)  
INC   CX   ;Increase   the   counter  
PUSH   DX   ;and   save   the   remainder  
CMP   AX,0   ;If   the   quotient   !=   0  
JNZ   decloop   ;then   get   one   more   number  


MOV   BX,   [min_size]   ;Load   min_size   value   into   bx  
MOV   DL,   [padd_chr]   ;Load   padd_chr   value   into   dl  
padd_result:  
CMP   CX,   BX   ;Is   cx   > =   min_size?  


JGE   poploop   ;If   so,   proceed  

MOV   BYTE   PTR   DS:[DI],   DL   ;Else   padd   with   padd_chr  
INC   DI   ;and   increase   string   pointer  
DEC   BX   ;decrease   bx  
JMP   padd_result   ;and   test   for   more   padding  


poploop:  
POP   DX   ;Get   the   number   of   the   stack  
ADD   DL, '0 '   ;and   add   '0 '   to   it  
MOV   BYTE   PTR   DS:[DI],   DL   ;Modify   the   string   at   ds:di  
INC   DI   ;Increase   the   string   pointer  
DEC   CX   ;Decrease   the   loop   counter  
JNZ   poploop  


POP   DX   ;Restore   all   registers  
POP   CX  
POP   BX  
POP   AX  
RET   ;And   return   from   call  
todec   ENDP  


;===========================================================  
;print   -   prints   the   string   pointed   to   by   dx   using   int   21h,09  
;===========================================================  
print   PROC  
PUSH   AX   ;Save   ax  
PUSH   DS   ;and   ds   onto   the   stack  

MOV   AX,   @data   ;Then   get   the   address   of   the   data   segment  
MOV   DS,   AX   ;and   store   it   into   ds  
MOV   AX,   0900h  
INT   21h   ;and   then   print   the   message   pointed   to   by   dx  

POP   DS   ;Retrieve   ds  
POP   AX   ;and   ax   from   stack  

RET  
print   ENDP  

_get   ENDP  
END

[解决办法]
0 v8086和win32是不同地

1 建议做成win32的dll形式。
[解决办法]
dll是一种方法.
另外一种办法是写成C的函数.

void get(){
__asm{
...
}

}

热点排行