Assembly 将二进制数读入十进制8086(NASM)的程序集

Assembly 将二进制数读入十进制8086(NASM)的程序集,assembly,x86,nasm,x86-16,Assembly,X86,Nasm,X86 16,我不知道我做错了什么。我需要一个二进制计算器,它的输入格式类似于“00000001b+00000010b…输出也需要是二进制的。。。 运算符可以是+、-、*、/ 我想读取第一个数字并将其转换为十进制…我的代码是这样的 %include "asm_io.inc" segment .text global _asm_main _asm_main: enter 0,0 pusha call read_int cmp al,'b' je vypis

我不知道我做错了什么。我需要一个二进制计算器,它的输入格式类似于“00000001b+00000010b…输出也需要是二进制的。。。 运算符可以是+、-、*、/

我想读取第一个数字并将其转换为十进制…我的代码是这样的

%include "asm_io.inc"

segment .text
    global _asm_main
_asm_main:
    enter 0,0
    pusha


    call read_int
    cmp al,'b'
    je vypis

vypis:
    call print_int


koniec:
    popa                 ; terminate program
    mov EAX, 0
    leave
    ret
当输入以数字1开始时,例如(10101010b),程序工作正常,但当输入以零开始时,程序工作不正常

我的问题我做错了什么,或者如何才能做得更好?


print_int和read_int是已经提供给我们的函数,它们可以100%工作。。。 我可以使用的其他函数有read\u char、print\u char和print\u string

read_int:
    enter   4,0
    pusha
    pushf

    lea eax, [ebp-4]
    push    eax
    push    dword int_format
    call    _scanf
    pop ecx
    pop ecx

    popf
    popa
    mov eax, [ebp-4]
    leave
    ret

print_int:
    enter   0,0
    pusha
    pushf

    push    eax
    push    dword int_format
    call    _printf
    pop ecx
    pop ecx

    popf
    popa
    leave
    ret

在我看来,
read\u int
只是返回一个整数值(在
eax
中),这个整数值是由
scanf
读取的。我不知道为什么您希望该整数的最低有效字节是
'b'
(?)

虽然我不知道您使用的是哪个
scanf
实现,但我还没有看到任何可以直接读取二进制数的实现。不过,自行实现该功能相当容易。
下面是一些C语言的示例代码,显示了以下原理:

char bin[32];
unsigned int i, value;

scanf("%[01b]", bin);  // Stop reading if anything but these characters
                       // are entered.

value = 0;
for (i = 0; i < strlen(bin); i++) {
    if (bin[i] == 'b')
        break;
    value = (value << 1) + bin[i] - '0';
}
// This last check is optional depending on the behavior you want. It sets
// the value to zero if no ending 'b' was found in the input string.
if (i == strlen(bin)) {
    value = 0;
}
char-bin[32];
无符号整数i,值;
scanf(“%[01b]”,bin);//如果没有这些字符,请停止读取
//输入。
数值=0;
对于(i=0;i