diff --git a/example/x86/console/ASMTEST3.asm b/example/x86/console/ASMTEST3.asm new file mode 100644 index 0000000000000000000000000000000000000000..b1ac66f4e95add083a594a74328c4bc197903a2f --- /dev/null +++ b/example/x86/console/ASMTEST3.asm @@ -0,0 +1,264 @@ +;西安科技大学--机械工程学院 训练题目 +;三级题目 +;由键盘输入任意字节数的A、B、C、D、E, +;按公式Y=A*B+C/D-E 计算 Y(n),将Y(n)按 +;正负数分别存储于数据区BUF1和BUF2中, +;并将计算结果显示在屏幕上。 +.model small,stdcall +.286 +TRUE EQU -1 +FALSE EQU 0 +NULL EQU 0 +ERROR_INPUT_STRING_NULL EQU 0EF01h +ERROR_INPUT_NOT_A_NUMBER EQU 0EF02h +ERROR_INPUT_NUMBER_OUT_RANGE EQU 0EF03h +ERROR_NEGATIVE_ZERO EQU 0EF04h +ERROR_DIVISOR_ZERO EQU 0EF05h +MaxBit EQU 3 +iCount EQU 80 +KEYBOARD STRUCT + maxInput BYTE iCount + inputCount BYTE ? + buffer BYTE iCount DUP (?) +KEYBOARD ENDS + +cCount EQU 5 +C_ARRAY STRUCT + variable_a WORD ? + variable_b WORD ? + variable_c WORD ? + variable_d WORD ? + variable_e WORD ? + Result DWORD ? +C_ARRAY ENDS + +input_NumString_to_8421BCD2HEX PROTO :WORD,:WORD +BCD2HEX PROTO :WORD +_printf PROTO :WORD +printf EQU invoke _printf, +_scanf PROTO :WORD +scanf EQU invoke _scanf, +calculator_Y PROTO :WORD +sInt2ASCII_String PROTO :WORD,:WORD,:WORD +clear_buf PROTO :WORD,:WORD +include Subproc.INC +.stack 200h +.data + buf1 WORD cCount dup (?) + buf2 WORD cCount dup (?) +b1Count WORD 0 +b2Count WORD 0 + array_buf C_ARRAY cCount dup () + KBbuf KEYBOARD <> + negative_flag WORD FALSE + buf BYTE 20 dup (?) + buf0 BYTE 20 dup (?) + tab_ BYTE 79 dup ('='),0dh,0ah,'$' + msg0 BYTE "FATAL ERROR(0xFFFF):Unknown error!",0dh,0ah,'$' + msg1 BYTE "Xi'an University of Science and Technoloy(xust)",0dh,0ah + BYTE "School of Mechanical Engineering(SoME)",0dh,0ah + BYTE "Assembly test Level.3",0dh,0ah,'$' + msg2 BYTE "Yn=A*B+C/D-E",0dh,0ah + BYTE "1.Input ASCII signal decimal number to 8421BCD code",0dh,0ah + BYTE "2.Convert 8421BCD to HEX and saved memory array buf",0dh,0ah + BYTE "3.Calculate & ouput result",0dh,0ah,'$' + msg3 BYTE "This program written By baidu\tieba\GuDaoTianHeng >-<",0dh,0ah + BYTE "Exit current process[Ctrl + Break]",0dh,0ah + BYTE "Please input different numbers(-999~999):",0dh,0ah,'$' + msg4 BYTE "ERROR(0x0EF01):String NULL,Try again",0dh,0ah,'$' + msg5 BYTE "ERROR(0x0EF02):Not a number,Try again",0dh,0ah,'$' + msg6 BYTE "ERROR(0x0EF03):Number out range(-999~999),Try again",0dh,0ah,'$' + msg7 BYTE "ERROR(0x0EF04):Negative zero(-0),Try again",0dh,0ah,'$' + msg8 BYTE "Array group " +a_group BYTE ' ',0dh,0ah,'$' + msg9 BYTE "Enter Variable " + v_name BYTE ' :','$' + cl_msg BYTE 40 dup (' '),0d,'$' + msg10 BYTE "ERROR(0x0EF05):Divisor zero(i/0?),Try again",0dh,0ah,'$' + msg11 BYTE "calculating.....................",'$' + msg12 BYTE "OK",0dh,0ah,'$' + CR_LF BYTE 0dh,0ah,'$' + msg13 BYTE "Scale Group(n) [ Var A]*[ Var B]+[ Var C]/[ Var D]-[ Var E]= Result Y(n)",0dh,0ah,'$' + msg14 BYTE "Decimal " + BYTE "Group(" +group_n BYTE ?,')',3 dup (' '),'(' + var_a BYTE 6 dup (' '),')*(' + var_b BYTE 6 dup (' '),')+(' + var_c BYTE 6 dup (' '),')/(' + var_d BYTE 6 dup (' '),')-(' + var_e BYTE 6 dup (' '),')=' + BYTE 3 dup (' ') +ResultY BYTE 4 dup (' ') +Results BYTE 6 dup (' '),0dh,0ah,'$' +.CODE +START: + mov ax,@data + mov ds,ax + mov es,ax + printf ADDR msg1 + printf ADDR tab_ + printf ADDR msg2 + printf ADDR tab_ + printf ADDR msg3 + xor cx,cx + xor bx,bx + .while bx < cCount + mov al,'1' + add al,bl + mov a_group,al + printf ADDR msg8 + mov ax,TYPE C_ARRAY + mul bx + lea dx,array_buf.variable_a + add dx,ax + L1: + push bx + push cx + @@: + mov al,'a' + add al,cl + +;If you need dump register,don't comment and continue execute. +; push ax +; push dx +; push di +; mov dl,cl +; lea di,buf0 +; mov WORD ptr ds:[di],"lc" +; mov BYTE ptr ds:[di+2],':' +; add di,3 +; call RHEX2ASCII +; mov WORD ptr ds:[di+2],"$ " +; printf ADDR buf0 +; +; mov dl,bl +; lea di,buf1 +; mov WORD ptr ds:[di],"lb" +; mov BYTE ptr ds:[di+2],':' +; add di,3 +; call RHEX2ASCII +; mov WORD ptr ds:[di+5],0a0dh +; mov BYTE ptr ds:[di+7],'$' +; printf ADDR buf1 +; pop di +; pop dx +; pop ax + + mov v_name,al + printf ADDR msg9 + scanf ADDR KBbuf + printf ADDR CR_LF + invoke input_NumString_to_8421BCD2HEX,ADDR KBbuf,dx + .if ax != 0 + .if ax == ERROR_INPUT_STRING_NULL + printf ADDR msg4 + jmp @b + .elseif ax == ERROR_INPUT_NOT_A_NUMBER + printf ADDR msg5 + jmp @b + .elseif ax == ERROR_INPUT_NUMBER_OUT_RANGE + printf ADDR msg6 + jmp @b + .elseif ax == ERROR_NEGATIVE_ZERO + printf ADDR msg7 + jmp @b + .else + printf ADDR msg0 + jmp _exit + .endif + .endif + .if cx == 3 + push bx + mov bx,dx + mov ax,ds:[bx] + pop bx + .if ax == 0 + mov ax,ERROR_DIVISOR_ZERO + printf ADDR msg10 + jmp @b + .endif + .endif + pop cx + pop bx + add dx,2 + inc cx + .if cx >= 5 + inc bx + xor cx,cx + .else + jmp L1 + .endif + .endw + + printf ADDR msg11 + xor cx,cx + .while cx < cCount + lea bx,array_buf + mov ax,TYPE C_ARRAY + mul cx + add bx,ax + invoke calculator_Y,bx + inc cx + .endw + printf ADDR msg12 + printf ADDR tab_ + printf ADDR msg13 + + mov cx,0 + .while cx < cCount + push cx + mov ax,TYPE C_ARRAY + mul cx + mov bp,ax + lea si,array_buf + xor dx,dx + mov ax,WORD ptr ds:(C_ARRAY ptr [si+bp]).variable_a + invoke sInt2ASCII_String,ADDR var_a,ADDR buf,ADDR buf0 + xor dx,dx + mov ax,WORD ptr ds:(C_ARRAY ptr [si+bp]).variable_b + invoke sInt2ASCII_String,ADDR var_b,ADDR buf,ADDR buf0 + xor dx,dx + mov ax,WORD ptr ds:(C_ARRAY ptr [si+bp]).variable_c + invoke sInt2ASCII_String,ADDR var_c,ADDR buf,ADDR buf0 + xor dx,dx + mov ax,WORD ptr ds:(C_ARRAY ptr [si+bp]).variable_d + invoke sInt2ASCII_String,ADDR var_d,ADDR buf,ADDR buf0 + xor dx,dx + mov ax,WORD ptr ds:(C_ARRAY ptr [si+bp]).variable_e + invoke sInt2ASCII_String,ADDR var_e,ADDR buf,ADDR buf0 + mov dx,WORD ptr ds:(C_ARRAY ptr [si+bp]).Result+2 + mov ax,WORD ptr ds:(C_ARRAY ptr [si+bp]).Result + test ax,01000000000000000b + jz positive + Negative: + lea di,buf2 + add di,b2Count + mov WORD ptr [di],ax + add b2Count,size WORD + jmp @f + positive: + lea di,buf1 + add di,b1Count + mov WORD ptr [di],ax + add b1Count,size WORD + @@: + invoke clear_buf,ADDR ResultY,10 + .if dx !=0 + invoke sInt2ASCII_String,ADDR ResultY,ADDR buf,ADDR buf0 + .else + invoke sInt2ASCII_String,ADDR Results,ADDR buf,ADDR buf0 + .endif + pop cx + inc cx + mov al,'0' + add al,cl + mov group_n,al + printf ADDR msg14 + .endw + printf ADDR tab_ + _exit: + xor al,al + mov ah,4ch ;结束,可以修改al设置返回码 + int 21h + +END START \ No newline at end of file diff --git a/example/x86/console/FILE_IO.asm b/example/x86/console/FILE_IO.asm new file mode 100644 index 0000000000000000000000000000000000000000..ee3f28d4e614a912e0d05d1ed60c0ecf0abc0f1a --- /dev/null +++ b/example/x86/console/FILE_IO.asm @@ -0,0 +1,128 @@ +.model small,stdcall;小内存模式 标准调用 +;常量声明 +NULL EQU 0;定义空常量 +READ_ONLY_FILE EQU 0000000000000001b;定义常数 只读文件 +HIDE_FILE EQU 0000000000000010b;定义常数 隐藏文件 +SYSTEM_FILE EQU 0000000000000100b;定义常数 系统文件 +ARCHIVE_FILE EQU 0000000000100000b;定义常数 归档文件 +;函数原型声明 +_printf PROTO :WORD +printf EQU invoke _printf, +;堆栈段声明 +.stack 200h +;数据段声明 +.data +;变量以及字串声明 + filename BYTE '.\tempfile.txt',NULL;待操作的文件名字串,以空字节结尾 + filetop EQU $ - filename;让编译器取filename字串长度 + hFile WORD ?;handle of open file + fileBuf BYTE 100h dup (?);文件缓冲区 +;错误、消息、提示字串 + msg1 BYTE "Path not find.",0dh,0ah,'$' + msg2 BYTE "Open too much file.",0dh,0ah,'$' + msg3 BYTE "File access denied",0dh,0ah,'$' + msg4 BYTE "Unknow error",0dh,0ah,'$' + msg5 BYTE "FCB invalid.",0dh,0ah,'$' + msg6 BYTE "File out range.",0dh,0ah,'$' + msg8 BYTE "String equal.",0dh,0ah,'$' + msg9 BYTE "Different string.",0dh,0ah,'$' +;代码段声明 +.CODE +START: +.startup;初始化数据段(DS)、堆栈段(SS)段基址及堆栈指针(SP) +open_file:;尝试打开文件 + mov ax,03d02h;DOS功能 3D 尝试打开一个文件 + lea dx,filename;将文件名字串地址装入dx + int 21h;调用DOS中断 + jc access_file_failed;访问文件失败,跳转失败处理。 +open_file_succeed:;文件开启成功 + mov hFile,ax;将中断返回的文件句柄放入文件句柄变量中保存 +read_file:;尝试读文件 + mov bx,hFile;读文件句柄 + mov cx,0100h;置缓冲区长度 + lea dx,fileBuf;装载缓冲区地址 + mov ah,03fh;尝试读一个文件 + int 21h;调用DOS中断 + jc access_file_failed;访问文件失败,跳转失败处理。 +read_file_succeed:;成功打开文件 + .if ax == 0;文件长度=0 则填充文件内容并保存 + mov al,' ';填充256个空格 + push ds + pop es;将附加段段基址初始化 + lea di,fileBuf;指向文件缓冲区 + mov cx,100h;填充计数器赋值256 + cld;步进趋势-自增 + rep stosb;填充缓冲区 + lea si,filename + lea di,fileBuf + add di,10 + mov cx,filetop - 1 + cld + rep movsb;拷贝文件名字串至文件缓冲区偏移+10 + mov bx,hFile + mov cx,0100h + lea dx,fileBuf + mov ah,40h + int 21h;调用DOS中断,写入文件 + jc access_file_failed;访问文件失败,跳转失败处理。 + .else;文件长度不为0,判断文件内字串是否等于文件名 + push ds + pop es + lea si,filename + lea di,fileBuf + add di,10 + mov cx,filetop - 1 + cld + rep cmpsb + jne different + printf ADDR msg8 + jmp close_file + different: + printf ADDR msg9 + .endif +close_file:;关闭文件 + mov bx,hFile + mov ah,3eh + int 21h;调用DOS中断 + jc close_file_faield;关闭文件失败,跳转失败处理。 +close_file_succeed: +;程序结束返回DOS +_exit: + xor al,al +.exit +access_file_failed:;打开文件异常处理 + .if ax == 02h;文件未找到 + mov cx,NULL;ARCHIVE_FILE and 0fh;尝试建立这个文件 + lea dx,filename + mov ah,3ch + int 21h;调用DOS中断 + jc access_file_failed + jmp open_file_succeed + .elseif ax == 03h;路径为不正确 + printf ADDR msg1 + .elseif ax == 04h;打开太多文件 + printf ADDR msg2 + .elseif ax == 05h;访问被拒绝 + printf ADDR msg3 + .elseif ax == 06h + printf ADDR msg5 + .elseif ax == 0ch;输入参数不正确 + jmp @f + .else + @@: + printf ADDR msg4 + .endif + jmp _exit +close_file_faield: + printf ADDR msg4 + jmp _exit +;函数区 +;打印字串 +_printf proc near uses bx cx dx,pString + mov ah,09h + mov dx,pString + int 21h;调用DOS中断 + xor ax,ax + ret +_printf endp +END START \ No newline at end of file diff --git a/example/x86/console/subproc.inc b/example/x86/console/subproc.inc new file mode 100644 index 0000000000000000000000000000000000000000..114bd3cab98c45c7f4550e3c08945bfeab9a9570 --- /dev/null +++ b/example/x86/console/subproc.inc @@ -0,0 +1,496 @@ +input_NumString_to_8421BCD2HEX PROTO :WORD,:WORD +BCD2HEX PROTO :WORD +_printf PROTO :WORD +_scanf PROTO :WORD +_clear_KBbuf PROTO :WORD +.CODE +input_NumString_to_8421BCD2HEX PROC near USES si di bx cx dx,NumString,Var;int input_NumString_to_8421BCD2HEX(int pNumString*,int pVar*); + local negative_flag:WORD;BOOL negative_flag; + local local_buf[4]:BYTE + mov si,NumString + .if BYTE ptr ds:(KEYBOARD ptr [si]).inputCount == NULL;if (KBbuf.inputCount == 1){ + input_NULL: + mov ax,ERROR_INPUT_STRING_NULL;return ERROR_INPUT_STRING_NULL;} + ret + .else;else{ + xor cx,cx;int c = 0; + mov cl,ds:(KEYBOARD ptr [si]).inputCount;c = KBbuf.inputCount; + mov bx,0;int b = 0; + .if [ds:(KEYBOARD ptr [si]).buffer+bx] == '-';if (KBbuf.buffer[b] == "-"){ + ngin: + inc bx;b++; + dec cx;c--; + mov negative_flag,TRUE;negative_flag = TRUE;} + .else;else{ + mov negative_flag,FALSE;negative_flag = FALSE;} + .if [ds:(KEYBOARD ptr [si]).buffer+bx] == '+';if (KBbuf.buffer[b] == "+"){ + inc bx;b++; + dec cx;c--; + .endif;} + .endif;} + .endif;} + + .while [ds:(KEYBOARD ptr [si]).buffer+bx] == '0' + .if cx == 1 + .break + .endif + inc bx + dec cx + .endw + + .if cx > MaxBit + mov ax,ERROR_INPUT_NUMBER_OUT_RANGE + ret + .endif + + lea di,ss:local_buf + push es + push ss + pop es + push di + push cx + mov cx,4 + mov al,0 + cld + rep stosb + pop cx + pop di + + + push di + mov ax,4 + sub ax,cx + add di,ax + ASCII2BCD: + .if [ds:(KEYBOARD ptr [si]).buffer+bx] < '0' + jmp not_a_mumber + .elseif [ds:(KEYBOARD ptr [si]).buffer+bx] > '9' + not_a_mumber: + mov ax,ERROR_INPUT_NOT_A_NUMBER + pop di + pop es + ret + .endif + xor ax,ax + mov al,[ds:(KEYBOARD ptr [si]).buffer+bx] + sub al,'0' + cld + stosb + inc bx + loop ASCII2BCD + pop di + pop es + mov ax,WORD ptr ss:[di+2] + shl al,4 + or al,ah + mov dx,WORD ptr ss:[di] + shl dl,4 + or dl,dh + mov ah,dl + invoke BCD2HEX,ax + .if negative_flag == TRUE + .if ax != 0 + neg ax + .else + mov ax,ERROR_NEGATIVE_ZERO + ret + .endif + .endif + mov di,Var + mov ds:[di],ax + xor ax,ax + ret +input_NumString_to_8421BCD2HEX ENDP + +BCD2HEX PROC USES cx dx bx,indec + xor ax,ax + xor dx,dx + mov ax,indec + mov cx,1 + mov dx,ax + and dx,0fh + l2: + push cx + shr ax,4 + push ax + mov ax,0ah + jmp next1 + l1: + mov bl,0ah + mul bl + next1: + loop l1 + pop bx + push bx + and bx,0fh + push dx + mul bx + pop dx + add dx,ax + pop ax + pop cx + inc cx + cmp cx,4 + jne l2 + mov ax,dx + ret +BCD2HEX ENDP + +_printf proc near uses bx cx dx,pString + mov ah,09h + mov dx,pString + int 21h + xor ax,ax + ret +_printf endp + +_scanf proc near uses bx cx dx,pKBbuf + mov ah,0ah + mov dx,pKBbuf + invoke _clear_KBbuf,dx + int 21h;scanf(pNumString*); + xor ax,ax + ret +_scanf endp + +_clear_KBbuf proc near,pKBbuf + pusha + xor cx,cx + mov di,pKBbuf + mov cl,(KEYBOARD ptr [di]).maxInput + add di,2 + mov al,0 + cld + rep stosb + popa + ret +_clear_KBbuf endp + +calculator_Y proc near uses si bx cx dx,pArray + local i:DWORD + mov si,pArray + mov ax,(C_ARRAY ptr [si]).variable_a + mov bx,(C_ARRAY ptr [si]).variable_b + imul bx + mov WORD ptr i,ax + mov WORD ptr i+2,dx + mov ax,(C_ARRAY ptr [si]).variable_c + cwd + mov bx,(C_ARRAY ptr [si]).variable_d + idiv bx + cwd + add WORD ptr i,ax + adc WORD ptr i+2,dx + mov ax,(C_ARRAY ptr [si]).variable_e + cwd + sub WORD ptr i,ax + sbb WORD ptr i+2,dx + mov ax,WORD ptr i + mov dx,WORD ptr i+2 + mov WORD ptr (C_ARRAY ptr [si]).Result,ax + mov WORD ptr (C_ARRAY ptr [si]).Result+2,dx + xor ax,ax + ret +calculator_Y endp + +;============================================================= +;子程序:转换16位HEX码[8bit]为扩展的BCD码[24bit] +;子程序调用名:HEX2BCD16 +;入口参数:ds:[si]=需要转换HEX变量存储地址,16bit +;出口参数:ds:[di]=转换后的BCD变量存储地址,24bit +HEX2BCD16 proc near +;------------------------------------------------------------- + pusha + mov ax,[si] + xor dx,dx + mov bx,0ah + div bx + mov bp,dx + xor dx,dx + div bx + mov cl,04h + shl dx,cl + and dx,00f0h + add bp,dx + xor dx,dx + div bx + mov cl,08h + shl dx,cl + and dx,0f00h + add bp,dx + xor dx,dx + div bx + mov cl,0ch + shl dx,cl + and dx,0f000h + add bp,dx + xor dx,dx + div bx + mov ax,bp + mov [di],byte ptr dl + inc di + mov [di],byte ptr ah + inc di + mov [di],byte ptr al + popa + ret +HEX2BCD16 endp +;============================================================= + +HEX2BCD32 proc near;转换的数值必须小于正整数 655359 否则会发生除法错溢出 + pusha + mov al,0 + mov cx,5 + cld + rep stosb + dec di + mov ax,[si] + mov dx,[si+2] + mov cx,0ah + .while ax !=0 + mov cx,0ah + call divdw + push cx + mov cx,0ah + call divdw + shl cl,4 + pop bx + or cl,bl + mov [di],cl + dec di + .endw + popa + ret +HEX2BCD32 endp + +;============================================================= +;子程序:转换内存中8位HEX码为扩展的ASCII码 +;子程序调用名:HEX2ASCII +;入口参数:ds:[si]=需要转换HEX变量存储地址,8bit +; ds:[di]=转换后的ASCII变量存储地址,16bit +;出口参数:ds:[di]=转换后的ASCII变量存储地址,16bit +HEX2ASCII proc near +;------------------------------------------------------------- + pusha + MOV DL,byte ptr[si] + MOV CL,4 + SHR DL,CL;右移4位获得高4位 + ADD DL,030H;0~9加30H,A~F加37H + CMP DL,'9';将双分支转化为单分支 + JBE NEXT1 + ADD DL,7 +NEXT1:MOV byte ptr[di],DL + INC DI + MOV DL,byte ptr[si] + AND DL,0FH;高4位清0获得低4位 + ADD DL,30H + CMP DL,'9' + JBE NEXT2 + ADD DL,7 +NEXT2:MOV byte ptr[di],DL + popa + ret +HEX2ASCII endp +;============================================================= + +;============================================================= +;子程序:转换寄存器中的8位HEX码为扩展的ASCII码 +;子程序调用名:RHEX2ASCII +;入口参数:dl +;出口参数:ds:[di]=转换后的ASCII变量存储地址,16bit +RHEX2ASCII proc near +;------------------------------------------------------------- + pusha + MOV DH,DL + MOV CL,4 + SHR DL,CL;右移4位获得高4位 + ADD DL,030H;0~9加30H,A~F加37H + CMP DL,'9';将双分支转化为单分支 + JBE NEXT1 + ADD DL,7 +NEXT1:MOV byte ptr[di],DL + INC DI + MOV DL,DH + AND DL,0FH;高4位清0获得低4位 + ADD DL,30H + CMP DL,'9' + JBE NEXT2 + ADD DL,7 +NEXT2:MOV byte ptr[di],DL + popa + ret +RHEX2ASCII endp +;============================================================= + +;============================================================= +;子程序:将指定内存的ASCII数字修正成输出格式 +;子程序调用名:FORMATNumString +;入口参数:CX=输入数字字符串长度 dl=对齐方式 0f0H=左对齐 0fh=右对齐 [SI]=数字字符串源地址,[di]=数字字符串目的地址 +;出口参数:[di]中的字符串修正成为 需要的输出格式 CX=修正后的有效数字串长度 +FORMATNumString proc near +;------------------------------------------------------------- + push ax + push bx + push dx + push di + push si + push bp + mov dh,"0" + cmp dh,[si] + jne FIXALL + push si + push di + push cx + mov al," " + cld + rep stosb + pop cx + pop di + pop si + push cx + mov ax,si + xchg si,di + push ax + mov al,"0" + cld + repz scasb + pop ax + pop cx + push di + sub di,ax + mov ax,di + pop di + cmp dl,0f0h + je left + add si,ax + dec si + left: + sub cx,ax + xchg si,di + dec si + inc cx + push cx + cld + rep movsb + jmp FIXOK +FIXALL: + push cx + cld + rep movsb + FIXOK:pop cx + pop bp + pop si + pop di + pop dx + pop bx + pop ax + ret +FORMATNumString endp +;============================================================= +sInt2ASCII_String proc near uses cx dx si di,pVar,buf,buf0 + local negative_flag:WORD + mov negative_flag,FALSE + .if dx == 0 + test ax,01000000000000000b + jz be_positive16 + neg ax + not negative_flag + be_positive16: + mov si,buf0 + mov di,buf + mov WORD ptr [si],ax + call HEX2BCD16 + xchg si,di + mov cx,3 + @@: + call HEX2ASCII + inc si + add di,2 + loop @b + mov cx,6 + mov dl,0fh + mov si,buf0 + mov di,pVar + call FORMATNumString + .if negative_flag == TRUE + not negative_flag + mov ax,6 + sub ax,cx + dec ax + add di,ax + mov BYTE ptr es:[di],'-' + .endif + .else + test dx,01000000000000000b + jz be_positive32 + not dx + not ax + inc ax + adc dx,0 + not negative_flag + be_positive32: + mov si,buf0 + mov di,buf + mov WORD ptr [si],ax + mov WORD ptr [si+2],dx + call HEX2BCD32 + xchg si,di + mov cx,5 + @@: + call HEX2ASCII + inc si + add di,2 + loop @b + mov cx,10 + mov dl,0fh + mov si,buf0 + mov di,pVar + call FORMATNumString + .if negative_flag == TRUE + not negative_flag + mov ax,10 + sub ax,cx + dec ax + add di,ax + mov BYTE ptr es:[di],'-' + .endif + .endif + ret +sInt2ASCII_String endp + +;子程序名称:divdw +;功能:进行不会产生溢出的除法运算,被除数为dword型 +; 除数为word型,结果为dword型 +;参数: (ax)=dword型数据的低16位 +; (dx)=dword型数据的高16位 +; (cx)=除数 +;返回: (dx)=结果的高16位,(ax)=结果的低16位 +; (cx)=余数 +;计算公式:X/N=int(H/N)*2^16+[rem(H/N)*2^16+L]/N +divdw proc + jcxz divdw_return ;除数cx为0,直接返回 + push bx ;作为一个临时存储器使用,先保存bx的值 + + push ax ;保存低位 + mov ax, dx ;把高位放在低位中 + mov dx, 0 ;把高位置0 + div cx ;执行H/N,高位相除的余数保存在dx中 + mov bx, ax ;把商保存在bx寄存器中 + pop ax ;执行rem(H/N)*2^16+L + div cx ;执行[rem(H/N)*2^16+L]/N,商保存在ax中 + mov cx, dx ;用cx寄存器保存余数 + mov dx, bx ;把bx的值复制到dx,即执行int(H/N)*2^16 + ;由于[rem(H/N)*2^16+L]/N已保存于ax中, + ;即同时完成+运算 + pop bx ;恢复bx的值 + divdw_return: + ret +divdw endp + +clear_buf proc near uses ax cx di,pBuf,scCount + mov cx,scCount + mov di,pBuf + mov al,' ' + rep stosb + ret +clear_buf endp \ No newline at end of file