Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux x86汇编2位键盘输入_Linux_Assembly_Input_X86 - Fatal编程技术网

Linux x86汇编2位键盘输入

Linux x86汇编2位键盘输入,linux,assembly,input,x86,Linux,Assembly,Input,X86,它需要允许输入一个两位数的数字,用于指示名称的打印次数。我不知道如何分离第二个数字,并检查它以确保它在0x30和0x39之间。我还不断得到这个奇怪的盒子,名字里面有0017 .data input_msg_len: .long 26 input_msg: .ascii "Enter a two-digit number: " name: .ascii "Michael Chabon\n" name_len: .long 16

它需要允许输入一个两位数的数字,用于指示名称的打印次数。我不知道如何分离第二个数字,并检查它以确保它在0x30和0x39之间。我还不断得到这个奇怪的盒子,名字里面有0017

    .data   
    input_msg_len:  .long 26
    input_msg:  .ascii "Enter a two-digit number: "
    name:       .ascii "Michael Chabon\n"
    name_len:   .long 16
    max:        .long 0
    count:      .long 0
    tmp:        .long 0
    input_str:  .ascii "??" 

    .text               
    .global _start          
    _start:     
        mov $4, %eax    
        mov $1, %ebx
        mov $input_msg, %ecx
        mov input_msg_len, %edx
        int $0x80

        mov $3, %eax    
        mov $0, %ebx    
        mov $input_str, %ecx 
        mov $2, %edx    
        int $0x80   

        mov $input_str, %eax
        add count, %eax

            mov $input_str, %eax 
        mov (%eax), %bl 
        cmp $0x30, %bl  
        jl  _start      
        cmp $0x39, %bl  
        jg  _start  

        mov count, %eax 
        inc %eax        
        mov %eax, count 

        sub $0x30, %bl
        mov %bl, max

        mov $10, %bl    
        imul    %bl
        mov %bl, max

#Not sure how to check second char in input_str.
#Want to check it then subtract $0x30 and move to tmp before adding tmp to max.

        mov $0, %edi    
    again:
        cmp max, %edi   
        je  end     

        mov $4, %eax    
        mov $1, %ebx    
        mov $name, %ecx
        mov name_len, %edx
        int $0x80       

        inc %edi        
jmp again       


    end:
        mov $1, %eax    
        int $0x80       

提前谢谢

您的代码中有一些错误

下面,该块的前两行是冗余的,因为
mov$input\u str,%eax
无论如何都会覆盖
eax

    mov $input_str, %eax
    add count, %eax

    mov $input_str, %eax
在这里,将
count
加载到
eax
中是没有意义的:

    mov count, %eax 
    inc %eax        
    mov %eax, count
您可以通过以下方式以更短、更清晰的方式完成此操作:

    incl count
然后,下一个错误是您最近将
count
加载到
eax
中,然后将加载到
al
中的
count
的最低8位乘以10,在这段代码中:

    mov (%eax), %bl  // bl = first character
    cmp $0x30, %bl  
    jl  _start      
    cmp $0x39, %bl  
    jg  _start  

    mov count, %eax // eax = count
    inc %eax        // eax++
    mov %eax, count // count = eax

    sub $0x30, %bl  // 0 <= bl <= 9
    mov %bl, max    // max = bl <- you lose this value in the next mov %bl, max

    mov $10, %bl    // bl = 10
    imul    %bl     // ax = 10 * and(count, 0xff) // ax = al*bl (signed multiply)
    mov %bl, max    // max = 10 <- here you overwrite the value of max with 10
然后,可以检查与第一个字符类似的第二个字符:

    pop %eax        // pop eax from stack

    incl %eax

    mov (%eax), %bl  // bl = second character
    cmp $0x30, %bl  
    jl  _start      
    cmp $0x39, %bl  
    jg  _start

    sub $0x30, %bl  // 0 <= bl <= 9

    add %bl, max    // 0 <= max <= 99
pop%eax//pop-eax来自堆栈
包括%eax
mov(%eax),%bl//bl=第二个字符
cmp$0x30,%bl
jl_启动
cmp$0x39,%bl
jg_开始

sub$0x30,%bl//0我假设您在Linux上使用
int80h
系统调用?还有,我假设你用的是汽油?我想是的,不过老实说,教授从来没有告诉我们细节。他只是给了我们一些程序,说“现在做这个”,没有向我们解释任何东西,除了它是一种汇编语言但int80h和GAS似乎是对的,听起来像个糟糕的教授。不管怎样,我会为您正在进行的每个系统调用编写一些小函数。有没有一种不用函数的方法呢?他现在不想让我们使用这些。很好地理解你面前的代码将是一个好的开始。我不知道所有的linux在我的脑海里,所以我想你也不知道。非常感谢!这帮了大忙!:)
    pop %eax        // pop eax from stack

    incl %eax

    mov (%eax), %bl  // bl = second character
    cmp $0x30, %bl  
    jl  _start      
    cmp $0x39, %bl  
    jg  _start

    sub $0x30, %bl  // 0 <= bl <= 9

    add %bl, max    // 0 <= max <= 99