如何写nasm文件实现高精度划分

问题描述 投票:0回答:0
  1. 我想用nasm语言实现高精度除法,首先我写了一个C文件,根据它来写我的汇编文件。 这是C文件
#include <cstdio>
#include <cstring>

static const int LEN = 1004;

int a[LEN], b[LEN], c[LEN], d[LEN];
void clear(int a[])
{
    for (int i = 0; i < LEN; ++i)
        a[i] = 0;
}

void read(int a[])
{
    static char s[LEN + 1];
    int x=0;
    int c=getchar();
    while (c!='\n' && x<LEN-1)
    {
        s[x++] = c;
        c = getchar();
    }
    s[x]='\0';
    //scanf("%s", s);
    clear(a);
    int len = strlen(s);
    for (int i = 0; i < len; ++i)
        a[len - i - 1] = s[i] - '0';
}

void print(int a[])
{
    int i;
    for (i = LEN - 1; i >= 1; --i)
        if (a[i] != 0)
            break;
    for (; i >= 0; --i)
        putchar(a[i] + '0');
    putchar('\n');
}


int greater_eq(int a[], int b[], int last_dg, int len)
{
    if (a[last_dg + len] != 0){
        printf("1!");
        return 1;
    }
        
    for (int i = len - 1; i >= 0; --i)
    {
        if (a[last_dg + i] > b[i]){
            printf("2!");
            return 1;}
        if (a[last_dg + i] < b[i]){
            printf("3!");
            return 0;}
    }
    printf("4!");
    return 1;
}

void div(int a[], int b[], int c[], int d[])
{
    clear(c);
    clear(d);

    int la, lb;
    for (la = LEN - 1; la > 0; --la)
        if (a[la - 1] != 0)
            break;
    for (lb = LEN - 1; lb > 0; --lb)
        if (b[lb - 1] != 0)
            break;
    if (lb == 0)
    {
        puts("> <");
        return;
    }

    for (int i = 0; i < la; ++i)
        d[i] = a[i];
    for (int i = la - lb; i >= 0; --i)
    {
        while (greater_eq(d, b, i, lb))
        {
            for (int j = 0; j < lb; ++j)
            {
                d[i + j] -= b[j];
                if (d[i + j] < 0)
                {
                    d[i + j + 1] -= 1;
                    d[i + j] += 10;
                }
            }
            c[i] += 1;
        }
    }
}

int main()
{
    
    read(a);
    read(b);
    div(a, b, c, d);
    print(c);
    print(d);
    
}

于是,我尽量写了nasm文件,读、写、反转字符串的函数我都写成功了,但是其他函数出现了一些错误。具体来说,就是函数geater_eq和div(好吧,我好像什么也没做)。成功函数就不用展示了,就是我的数据声明:

num1 times 100 db '0',0h num2 times 100 db '0',0h

然后是greater_eq

greater_eq:
    ;a[]:num1,b[]:num2,last_ag:eax,len:ebx
    ;return in eax
    push ecx
    push edx
    push esi; preserve values
    mov esi,num1
    add esi,eax
    add esi,ebx ;a[last+len]
    cmp [esi],‘0’
    je compare
    jne greater

compare:
    mov ecx,ebx ;i=len-1
    dec ecx

.loop:
    cmp ecx,0h 
    jl greater
    mov esi,eax
    add esi,ecx
    mov edx,[num1+esi] ;a[last_eg+i]
    cmp edx,[num2+ecx] ;b[i]
    jg greater
    jl lesser
    dec ecx
    jmp .loop

greater:
    pop esi
    pop edx
    pop ecx
    
    mov eax,1
    ret
lesser:
    pop esi
    pop edx
    pop ecx
    mov eax,0
    ret

我改了很多还是有问题,为了功能的划分,可能会出现更多的问题

mydiv:
    mov eax,sinput
    call strlen
    mov ecx,eax
    ;la ecx
    mov eax,dinput
    call strlen
    mov edx,eax
    ;lb edx
    jz rerurn_diff

    mov eax,sinput
    mov ebx,dinput

    mov esi,ecx ;i=0
    sub esi,edx ;i=la-lb
div_loop:
    cmp esi,0  
    jl  return_n  ; i<0结束循环
    
div_continue:
    push eax
    push ebx
    mov eax,esi  ;i
    mov ebx,edx  ;lb
   
    call greater_eq
    mov ebp,eax  ;res
    pop ebx
    pop eax
    
    cmp ebp,0
    jne div_sub
   
    dec esi ;i--
    jmp div_loop

div_sub:
    mov edi,0 ;j=0
div_sub_loop:
    cmp edi,edx ;j>=lb 退出
    jge div_sub_add
    push ebp
    mov ebp,eax
    add ebp,esi
    add ebp,edi; a[i+j] ->byte[eax+esi+edi]
   ;1
  
    
    push ebx
    add ebx,edi
    push eax
    mov ah,byte[ebp]
    mov al,byte[ebx]
    sub ah,al  ;a[i+j] -= b[j]
    mov byte[ebp],ah
    pop eax
    pop ebx

    ;2
    
    cmp byte[ebp],0
    jl div_loop_fix
div_sub_con:
    pop ebp
    inc edi ;j++
    inc byte[c+esi] ;c[i]++
    
    jmp div_continue

div_loop_fix:
    dec byte[ebp+1]
    add byte[ebp],10
    jmp div_sub_con
div_sub_add:
    inc byte[c+esi]
    jmp div_continue

return_n:
    ret
rerurn_diff:
    mov eax,meg6
    ret

所以,如果我能得到正确的答案,那就再好不过了,我敢肯定你不会想像狗屎一样修改我的代码,如果你能给我一些好的建议,我将不胜感激。 (顺便说一句,后天是截止日期,我写了几天。我是中国人所以请原谅我的英语不好,谢谢大家!)

c++ assembly x86 nasm division
© www.soinside.com 2019 - 2024. All rights reserved.