加减乘除计算器,求大大们指点
本人用php工作,汇编纯属兴趣,拍砖的留点情面呀,哈哈。
另外,我想问个问题,怎么才能定时执行一段代码,就像vb里面的timer一样,因为我想再试试写个贪食蛇什么的。
下面是代码
assume cs:code,ds:data,ss:stackstack segment para stack 'stack' dw 128 dup(0)stack endsdata segment message db 'please input : $' ;提示信息 key_in_buff db 30 , ? , 30 dup(?) ;键盘缓冲 key_in_str1 db 7 dup(' ') ;输入的第一个数字的字符串 key_in_str2 db 7 dup(' ') ;输入的第二个数字的字符串 key_in_flag db ' ' ;四则运算符 key_in_num1 dd 0 ;第一个数字的数值 key_in_num2 dd 0 ;第二个数字的数值 key_out_result dd 0 ;结果数值 key_out_str db 6 dup(' '),'$' ;结果字符串 ;test__ db '-12 + 1235 ' ;测试字符串 flag__ db ' ' ;正负标志data endscode segment;主函数开始start: mov ax,data mov ds,ax mov ax,stack mov ss,ax start__: lea dx,message mov ah,09h int 21h call new_line lea dx,key_in_buff mov ah,0ah int 21h lea dx,key_in_buff[2] lea bx,key_in_str1 call extract_key_input lea bx,key_in_str1 lea di,key_in_num1 call str_to_num lea bx,key_in_str2 lea di,key_in_num2 call str_to_num lea bx,key_in_num1 lea si,key_in_num2 mov al,byte ptr key_in_flag[0] lea di,key_out_result call add_and_so_on lea bx,key_out_result lea di,key_out_str call num_to_str call new_line lea dx,key_out_str mov ah,09h int 21h call new_line jmp start__ mov ah,4ch int 21h ;>>>>>>>>>>>>>>>>>>>>>>>解析输入函数区开始>>>>>>>>>>>>>>>>>>>>>>>> ;入口参数: ; dx: 输入字符串开始地? ; bx: di开始的字符 extract_key_input proc push ax push bx push cx push dx push si push di mov di,dx mov ax,0000h mov si,0000h cl_num1: cmp byte ptr ds:[di],'+' je flag1 cmp byte ptr ds:[di],'-' je flag1 cmp byte ptr ds:[di],'*' je flag1 cmp byte ptr ds:[di],'/' je flag1 go_on: cmp byte ptr ds:[di],' ' je if_jump_flag mov al,byte ptr ds:[di] mov byte ptr ds:[bx][si][0],al mov ah,01h no_jump_flag: inc di inc si jmp cl_num1 flag1: cmp si,0000h je go_on flag: mov si,0000h mov ax,0000h cl_flag: cmp byte ptr ds:[di],' ' je a_empty_loop mov al,byte ptr ds:[di] mov byte ptr ds:[bx][si][13],al inc di jmp num2 a_empty_loop: inc di inc si jmp cl_flag num2: mov si,0000h mov ax,0000h cl_num2: cmp byte ptr ds:[di],' ' je if_jump_end mov al,byte ptr ds:[di] mov byte ptr ds:[bx][si][6],al mov ah,01h no_jump_end: inc di inc si jmp cl_num2 if_jump_flag: ;是否跳到flag cmp ah,00h je no_jump_flag jne flag if_jump_end: ;是否结束 cmp ah,00h je no_jump_end jne extract_key_input_end extract_key_input_end: pop di pop si pop dx pop cx pop bx pop ax ret extract_key_input endp ;>>>>>>>>>>>>>>>>>>>>>>>解析输入函数区结束>>>>>>>>>>>>>>>>>>>>>>>> ;>>>>>>>>>>>>>>>>>>>>>>>字符串转为数值开始>>>>>>>>>>>>>>>>>>>>>>>>> ;入口参数: ; bx:字符串开始地址 ; di:数值地址 str_to_num proc push ax push bx push cx push dx push si push di mov si,0000h ;检查正负 cmp byte ptr ds:[bx][si],'-' je set__ jne unset__ set__: mov al,'-' mov byte ptr flag__[0],al inc bx unset__: ;统计长度,不超过6位 mov cx ,0000h num_count: cmp byte ptr ds:[bx][si],' ' je num_count_end inc cx cmp cx,0006h je num_count_end inc si jmp num_count num_count_end: mov si,cx dec si mov dx,000ah mov ax,0001h mov word ptr ds:[di][0],0000h ;先清零 mov word ptr ds:[di][2],0000h stc get_num: jc jump_first ;ax存放位权 mul dx mov dx,000ah jump_first: clc push ax push dx ;位权给dx mov dx,ax ;ax存放位值 mov ah,00h mov al,byte ptr ds:[bx][si] sub al,30h mul dx add word ptr ds:[di][0],ax adc word ptr ds:[di][2],dx pop dx pop ax dec si loop get_num cmp byte ptr flag__[0],'-' je setfu jne notsetfu setfu: not word ptr ds:[di][0] not word ptr ds:[di][2] add word ptr ds:[di][0],0001h adc word ptr ds:[di][0],0000h notsetfu: ;还原正负标志 mov al,' ' mov byte ptr flag__[0],al pop di pop si pop dx pop cx pop bx pop ax ret str_to_num endp ;>>>>>>>>>>>>>>>>>>>>>>>字符串转为数值结束>>>>>>>>>>>>>>>>>>>>>>>>> ;>>>>>>>>>>>>>>>>>>>>>>>四则运算开始>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ;入口参数: ; bx:第一个数值的地址 ; si:第二个数值的地址 ; al:运算符 ; di:结果值所存地 add_and_so_on proc push ax push bx push cx push dx push si push di cmp al,"+" je cal_add cmp al,"-" je cal_sub cmp al,"*" je cal_mul cmp al,"/" je cal_div mov ax,0000h mov cx,0000h mov dx,0000h ;加 cal_add: mov cx,word ptr ds:[bx][0] add cx,word ptr ds:[si][0] mov dx,word ptr ds:[bx][2] adc dx,word ptr ds:[si][2] jmp cal_end ;减 cal_sub: mov cx,word ptr ds:[bx][0] sub cx,word ptr ds:[si][0] mov dx,word ptr ds:[bx][2] sbb dx,word ptr ds:[si][2] jmp cal_end ;乘 cal_mul: mov ax,word ptr ds:[bx][0] imul word ptr ds:[si][0] mov cx,ax jmp cal_end ;除 cal_div: mov dx,word ptr ds:[bx][2] mov ax,word ptr ds:[bx][0] idiv word ptr ds:[si][0] mov cx,ax jmp cal_end ;结束运算并保存值到指定内存 cal_end: mov word ptr ds:[di][0],cx mov word ptr ds:[di][2],dx pop di pop si pop dx pop cx pop bx pop ax ret add_and_so_on endp ;>>>>>>>>>>>>>>>>>>>>>>>四则运算结束>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ;>>>>>>>>>>>>>>>>>>>>>>>数值主换为字符串开始>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ;入口参数: ; bx:输入数值起始地址? ; di:输出字符起始地址 num_to_str proc push ax push bx push cx push dx push si push di mov ax,word ptr ds:[bx] and ax,1000000000000000b cmp ax,0000000000000000b jne set_fu je no_set_fu set_fu: mov byte ptr flag__[0],'-' not word ptr ds:[bx] add word ptr ds:[bx],0001h no_set_fu: cmp byte ptr flag__[0],'-' je add_f jne n_add_f add_f: mov byte ptr ds:[di],'-' inc di n_add_f: mov cx,0000h mov dl,0ah mov si,0000h ;整除十取余数,再加30h mov ax, word ptr ds:[bx] get_mod: div dl add ah,30h push ax mov ah,00h inc cx cmp al,00h je end_get_mod jmp get_mod end_get_mod: mov si,cx dec si revere_str: pop ax mov ds:[di],ah inc di dec si loop revere_str end_n_t_s: mov byte ptr flag__[0],' ' pop di pop si pop dx pop cx pop bx pop ax ret num_to_str endp ;>>>>>>>>>>>>>>>>>>>>>>>数值主换为字符结束>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ;>>>>>>>>>>>>>>>>>>>>>>>新起一行(回车+换行)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> new_line proc push ax push dx mov dl,0dh mov ah,02h int 21h mov dl,0ah mov ah,02h int 21h pop dx pop ax ret new_line endp ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>code endsend start