简体   繁体   中英

How do I ignore line breaks in input using NASM Assembly?

Learning NASM Assembly, I am trying to make a program that reads two one-digit number inputs.

I have two variables declared in the .bss :

num1 resb 1
num2 resb 1

Then, I ask the user to write the numbers like this:

; Get number 1
mov EAX,3
mov EBX,1
mov ECX,num1
mov EDX,1
int 0x80

; Get number 2
mov EAX,3
mov EBX,1
mov ECX,num2
mov EDX,1
int 0x80

Since I am only interested in one-digit number inputs, I set EDX to 1 . This way, whatever the user types, only the first character will be stored in my variable (right?).

The problem is that everything that follows after that first character will be used for the future reads. If you type 5 and then press ENTER , 5 will be stored in num1 just fine, but the line break you generated by pressing ENTER will carry on to the next read instruction, which will be stored in num2 . Clearly that's not what I was intending (I want the user to type a number, press ENTER, type another number, and press ENTER).

I am not entirely sure how to work around this in the simplest way possible.

The dumbest idea was to put a "dummy" read instruction between num1 and num2 , which will capture the line break (and do nothing with it). This is obviously not good.

Here's a very basic way of reading input until you get digits you want. It will skip anything but digits. This approach is fine if it provides the functionality you want. If you need different behavior depending upon other non-numeric input, then you need to specify that behavior. Then that behavior can be programmed as well.

    ; Get number 1
    mov   ECX,num1
    call  GetNumber

    ; Get number 2
    mov   ECX,num2
    call  GetNumber
    ...

GetNumber:
    pusha              ; save regs
get:
    mov   EAX,3        ; system call for reading a character
    mov   EBX,0        ; 0 is standard input
    mov   EDX,1        ; number of characters to read
    int   0x80         ; ECX has the buffer, passed into GetNumber
    cmp   byte [ecx],0x30
    jlt   get          ; Retry if the byte read is < '0'
    cmp   byte [ecx],0x39
    jgt   get          ; Retry if the byte read is > '9'

    ; At this point, if you want to just return an actual number,
    ; you could subtract '0' (0x30) off of the value read
    popa               ; restore regs
    ret

Meddling with stdin to disable I_CANON will work, but may be the "hard way". Using a two byte buffer and doing mov edx, 2 will work if the pesky user is well behaved - either clear the second byte, or just ignore it.

Sometimes the pesky user is not well behaved. Dealing with "garbage input" or other error conditions generally takes much more code than just "doing the work"! Either deal with it, or be satisfied with a program that "usually" works. The second option may be sufficient for beginners.

The pesky user might just hit "enter" without entering a number. In this case, we want to either re-prompt, or perhaps print "Sorry you didn't like my program" and exit. Or he/she might type more than one character before hitting "enter". This is potentially dangerous! If a malicious user types "1rm -rf .", you've just wiped out your entire system! Unix is powerful, and like any powerful tool can be dangerous in the hands of an unskilled user.

You might try something like (warning: untested code ahead!)...

 section .bss num1 resb 1 num2 resb 1 trashbin resb 1 section .text re_prompt: ; prompt for your number ; ... ; get the number (character representing the number!) mov ecx, num1 reread: mov edx, 1 mov ebx, 0 ; 1 will work, but 0 is stdin mov eax, 3 ; sys_read int 0x80 cmp byte [ecx], 10 ; linefeed jz got_it mov ecx, trashbin jmp reread got_it: cmp byte [num1], 10 ; user entered nothing? jz re_prompt ; or do something intelligent ; okay, we have a character in num1 ; may want to make sure it's a valid digit ; convert character to number now? ; carry on 

You may need to fiddle with that to make it work. I probably shouldn't post untested code (you can embarrass yourself that way!). "Something like that" might be easier for you than fiddling with termios . The second link Michael gave you includes the code I use for that. I'm not very happy with it (sloppy!), but it "kinda works". Either way, have fun! :)

You will have to deal with canonical disabling, raw keyboard. This is how linux manages entering console password for exampe without showing it.

The assembly to do this is nicely described here:

http://asm.sourceforge.net/articles/rawkb.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM