| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 609 人关注过本帖
标题:加法代码问题
只看楼主 加入收藏
luosheng01
Rank: 1
等 级:新手上路
帖 子:9
专家分:7
注 册:2011-2-6
结帖率:33.33%
收藏
已结贴  问题点数:20 回复次数:1 
加法代码问题
程序代码:
data segment   ;数据代码段
    buf    db    20,?,20 dup(?)
    A    dw    0
    B    dw    0
    result    dw    ?
    crlf    db 0ah,0dh,'$'
data ends      ;数据代码段结束
code segment   
    assume cs:code,ds:data
start:
    mov ax,data   
    mov ds,ax     
    
    lea dx,buf    
    mov ah,10     
    int 21h       
    inc dx        
    mov di,dx     
    mov cx,[di]   
    and cx,0ffh   
    mov bl,16     
next:
    inc di          
    mov al,[di]   
    cmp al,'+'    
    je    jadd      
    cmp al,'-'    
    je    jsub
    
    cmp al,'Z'
    jle no1       
    sub al,20h
no1:
    cmp al,'9'
    jle no2
    sub al,7
no2:
    sub    al,30h    
    mov bh,al
    mov ax,A
    mul bl        
    mov A,ax
    mov al,bh
    and ax,0ffh   
    add A,ax
    dec cx        
    jmp next      
jadd:
    call proc1    
    mov ax,A
    add ax,B
    jmp exit
jsub:
    call proc1
    mov ax,A
    sub ax,B
    jmp exit
exit:
    mov result,ax
    lea dx,crlf
    mov ah,9
    int 21h
    lea bx,buf
    mov ax,[bx+1]
    and ax,0ffh
    add bx,ax
    mov byte ptr[bx+2],'='   
    mov byte ptr[bx+3],'$'  
    lea dx,buf
    add dx,2
    mov ah,9       
    int 21h
    mov ax,result 
    call DISPLAY    
    mov ah,4ch
    int 21h            
proc1    proc    near
        mov bl,16
        dec cx      
continue:
        inc di    
        mov al,[di]
        cmp al,'Z'    
        jle no5       
        sub al,20h   
no5:
        cmp al,'9'    
        jle no6       
        sub al,7
no6:
        sub    al,30h
        mov bh,al
        mov ax,B
        mul bl       
        mov B,ax
        mov al,bh
        and ax,0ffh   
        add B,ax
        loop continue 
    ret
proc1    endp
DISPLAY    PROC    NEAR   
    MOV CX,0    
    MOV    BX,16
CONTINUE1:
    XOR DX,DX     
    DIV    BX
    PUSH DX       
    INC    CX
    CMP    AX,0
    JNZ    CONTINUE1   
    
NEXT1:
    POP DX         
    AND DX,0FFH
    ADD DL,30H
    CMP DL,'Z'
    JLE no3
    SUB DL,20H     
no3:    
    CMP DL,'9'
    JLE no4
    ADD DL,7
no4:
    MOV AH,02
    INT 21H
    LOOP NEXT1       
    mov ah,01h
    int 21h
DISPLAY    ENDP
code ends       
    end    start









这个代码是我从网上找来的一段代码,但是测试之后发现有点小问题
这个代码实现的是简易计算器功能
但是当测试1111+1111时显示的是222
貌似少了一位
但是我也不知道问题出在哪里
(我只留下了程序中的加法和减法运算)
求解~
搜索更多相关主题的帖子: result 
2011-12-30 11:01
zaixuexi
Rank: 12Rank: 12Rank: 12
来 自:上海
等 级:火箭侠
威 望:8
帖 子:858
专家分:3233
注 册:2010-12-1
收藏
得分:20 
程序代码:
data    segment   ;数据代码段
    buf    db 20,?,20 dup(?)     ;struct inBuffer {
                                  ;    char max_length;
                                  ;    char act_length;
                                  ;    char buf[20];
                                  ;}inbuffer = {20, 0, {0}};
    A      dw 0                  ;short A = 0;
    B      dw 0                  ;short B = 0;
    result dw ?                  ;short result;
    crlf   db 0ah,0dh,'$'        ;const char *crlf = "\r\n";
data    ends      ;数据代码段结束

code    segment  
    assume cs:code,ds:data
start:
    mov  ax,data  
    mov  ds,ax    
    lea  dx,buf   
    mov  ah,10    
    int  21h                      ;(&inbuffer)->buf[0] = '%';
                                  ;itoa((&inbuffer)->max_length, (&inbuffer)->buf+1, 10);
                                  ;(&inbuffer)->buf[2] = 's';
                                  ;scanf((&inbuffer)->buf, (&inbuffer)->buf);
                                  ;(&inbuffer)->act_length = strlen((&inbuffer)->buf);
    inc  dx
    mov  di,dx       
    mov  cx,[di]                  ;short count = (&inbuffer)->act_length;
    and  cx,0ffh                  ;count = MAKEWORD(0, LOBYTE(count));
    mov  bl,16
next:
    inc  di                       ;++p;(;char *p = (&inbuffer)->buf;)
    mov  al,[di]  
    cmp  al,'+'                   ;switch (*p) {
    je   jadd                     ;case '+': goto jadd;
    cmp  al,'-'           
    je   jsub                     ;case '-': goto jsub;
    cmp  al,'Z'                   ;}
    jle  no1                      ;if (*p <= 'Z') goto no1;
    sub  al,20h
no1:
    cmp  al,'9'
    jle  no2                      ;if (*p - 0x20 <= '9') got no2;
    sub  al,7                     ;else char ch = *p - 7;
no2:
    sub  al,30h
    mov  bh,al                    ;ch -= 0x30;
    mov  ax,A
    mul  bl       
    mov  A,ax                     ;A *= 16;
    mov  al,bh
    and  ax,0ffh
    add  A,ax                     ;A += MAKEWORD(0, ch);
    dec  cx                       ;--count;
    jmp  next                     ;goto next;
jadd:
    call proc1                    ;proc1();
    mov  ax,A           
    add  ax,B                     ;short tmp = A + B;
    jmp  exit                     ;goto exit;
jsub:
    call proc1
    mov  ax,A
    sub  ax,B                     ;tmp = A - B;
    jmp  exit
exit:
    mov  result,ax                ;result = tmp;
    lea  dx,crlf
    mov  ah,9
    int  21h                      ;printf("\r\n");
    lea  bx,buf                   ;short *pIn = (short *)&inbuffer;
    mov  ax,[bx+1]
    and  ax,0ffh
    add  bx,ax                    ;pIn += MAKEWORD(0, LOBYTE((char *)pIn++));
    mov  byte ptr[bx+2],'='       ;*((char *)pIn+2) = '=';
    mov  byte ptr[bx+3],'$'       ;*((char *)pIn+3) = '$';
    lea  dx,buf
    add  dx,2                     ;pIn = (short *)((char*)&inbuffer + 2);
    mov  ah,9      
    int  21h                      ;puts(pIn);
    mov  ax,result
    call display                  ;display(result); 
    mov  ah,4ch
    int  21h                      ;exit(0);        
proc1    proc near
    mov  bl,16           
    dec  cx                       ;--count;
continue:
    inc  di                       ;++p;
    mov  al,[di]                  ;ch = *p;
    cmp  al,'Z'
    jle  no5                      ;if (ch == 'Z') goto no5;   
    sub  al,20h                   ;else ch -= 0x20;
no5:
    cmp  al,'9'
    jle  no6                      ;if (ch <= '9') goto no6;   
    sub  al,7                     ;else ch -= 7;
no6:
    sub  al,30h                   ;ch -= 0x30;
    mov  bh,al
    mov  ax,B
    mul  bl      
    mov  B,ax                     ;B *= 16;
    mov  al,bh
    and  ax,0ffh  
    add  B,ax                     ;B += MAKEWORD(0, ch);
    loop continue                 ;while (--count);
    ret                           ;return;
proc1   endp
display  proc near                ;void display(short num);
    mov  cx,0                     ;short i = 0;
    mov  bx,16
continue1:
    xor  dx,dx
    div  bx                       ;short q = MAKEWORD(0, LOBYTE(num)) / 16;
                                  ;short r = MAKEWORD(0, LOBYTE(num)) % 16;   
    push dx                       ;*--sysstack = q;
    inc  cx                       ;i++;
    cmp  ax,0
    jnz  continue1                ;if (r != 0) goto continue1;
next1:
    pop  dx                       ;q = *++sysstack;
    and  dx,0ffh
    add  dl,30h                   ;q = MAKEWORD(0, LOBYTE(q) + 0x30);
    cmp  dl,'Z'
    jle  no3                      ;if (LOBYTE(q) <= 'Z') goto no3;
    sub  dl,20h                   ;else q -= 0x20;
no3:
    cmp  dl,'9'
    jle  no4                      ;if (LOBYTE(q) <= '9') goto no4; 
    add  dl,7                     ;else q += 7;
no4:
    mov  ah,02
    int  21h                      ;putchar(LOBYTE(q));
    loop next1                    ;while (--i);
    mov  ah,01h
    int  21h                      ;getchar();
display endp    
code    ends      
end     start
这个,你在哪里看到的代码,居然可以写的这么混,你自己再看看吧,我是不想看了

技术问题,请不要以短消息方式提问
2012-01-01 15:12
快速回复:加法代码问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021365 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved