NASM解析Ascii字符以显示

3

基本上我想做的(这是为了作业)是让用户输入一个字符,然后显示它是否为大写字母/小写字母、0-9十进制数、可打印符号或控制键。

不幸的是,尽管文档告诉我如何操作控制逻辑(if / else,循环等),但实际上知道命令使NASM按照我的意愿执行任务已经让我感到沮丧,因为幻灯片和课堂笔记以及文档并没有完全有益。

控制键我相信是来自DEC 0到32的esc、null、tab等。在我的下面的代码中,我接受来自用户的输入,但是我不认为它接受控制键,我该怎么操作才能实现呢?

segment .data

    msg3 dw '10h'
segment .bss
;
aChar resb 2 ; reserve 10 bytes for aChar

segment .text
    global _start
_start:


    ; ### Code for simple Input ###
    mov eax, 3 ; select kernal call #3
    mov ebx, 0 ; default input device
    mov ecx, aChar ; pointer to aChar'
    int 0x80 ; invoke kernal call to read   


    ; ### Code to spit the input to the screen ###
    mov eax, 4 ; select kernal call #4
    mov ebx, 1      ; default output device
    mov ecx, aChar ; pointer to ID
    int 0x80 ; write id read

    ; termination code
exit: mov ax, 1 ; select system call #1 system exit
    mov bx, 0 ; 0 means normal
    int 0x80 ; invoke kernal call

所以我的想法是,假设我能够接受ASCII控制键。一旦用户输入一个字符,我将把它转换为十六进制或十进制,以更容易地运行if else语句,如果它在某些值之间,则为X,然后显示X,如果它在2个不同的值之间,则为Y等等。

因为小于33的东西显然是控制键(DEL是控制键吗?它是DEC 127),但小于48的任何东西都是符号,但它也必须大于32,或者我是否默认假设如果它大于32但小于48,则假定为符号?令人烦恼的是,所有东西似乎都存在于ASCII图表的不同部分。

这里是将ASCII转换为HEX的代码,我认为使用十进制可能更容易处理,但我不知道如何a)找出我是否在某个注册表中具有ascii字符的二进制值,b)将其转换为十进制,因此既然我有了这个代码,就可以使用它吗?

;Hex equivalent of characters              HEX2CHAR.ASM
;
;        Objective: To print the hex equivalent of
;                   ASCII character code. Demonstrates
;                   the use of xlat instruction.
;            Input: Requests a character from the user.
;           Output: Prints the ASCII code of the
;                   input character in hex.
%include "io.mac"

.DATA
char_prompt    db  "Please input a character: ",0
out_msg1       db  "The ASCII code of '",0
out_msg2       db  "' in hex is ",0
query_msg      db  "Do you want to quit (Y/N): ",0
; translation table: 4-bit binary to hex
hex_table      db  "0123456789ABCDEF"    

.CODE
     .STARTUP
read_char:
     PutStr  char_prompt  ; request a char. input
     GetCh   AL           ; read input character

     PutStr  out_msg1
     PutCh   AL
     PutStr  out_msg2
     mov     AH,AL        ; save input character in AH
     mov     EBX,hex_table; EBX = translation table
     shr     AL,4         ; move upper 4 bits to lower half
     xlatb                ; replace AL with hex digit
     PutCh   AL           ; write the first hex digit
     mov     AL,AH        ; restore input character to AL
     and     AL,0FH       ; mask off upper 4 bits
     xlatb
     PutCh   AL           ; write the second hex digit
     nwln
     PutStr  query_msg    ; query user whether to terminate
     GetCh   AL           ; read response

     cmp     AL,'Y'       ; if response is not 'Y'
     jne     read_char    ; read another character
done:                        ; otherwise, terminate program
     .EXIT

来自“Linux汇编程序指南”。

我的问题是,如果代码将其转换为十六进制,它实际上是否以十六进制的形式存在于内存中?还是仅作为十六进制的ASCII字符表示显示?如果是这样,那对我没有帮助。

所以我的问题按顺序如下:

1. 如何接受控制键的用户输入。 2. 如何解析ASCII,以便我在内存寄存器中拥有一个HEX值,这可能是说“变量”的一种花哨方式,我键入的ASCII字符的HEX值?然后我可以调整并评估以显示我需要显示的内容?

编辑:我想我找到了一个解决方案,但问题是它不起作用,我不知道为什么:

decimal: 
    mov ebx, 30h    ; smallest decimal ASCII 
    mov edx, key
    cmp edx, ebx
    jl  uppercase
    mov ebx, 39h    ; test against 9
    cmp edx, ebx
    jg  exit
    mov eax, 4
    mov ebx, 1
    mov ecx, decimalKey
    mov edx, decimalLen
    int 0x80

它只是每次评估为“大于”,无论我输入什么,都会跳转到“退出”。
1个回答

2

最终弄清楚了。

;
; This is assignment 2, it takes key strokes from the user
; and displays whether it is a control key, upper case,
; lower case, a printable symbol, or a decimal.
; By Blayne Elison Bradley, 9688994
; March 4th 2013

segment .data
    msg db 'Enter a character: ', 0xA ; text message
    len equ $-msg                                                           ; length of msg
    msg2 db 'Your character is: ' ; text message
    len2 equ $-msg2   

    controlKey: db "Control Key", 10
    controlLen: equ $-controlKey

    control2Key: db "ControlKey2", 10
    control2Len: equ $-control2Key

    printableKey: db "Printable", 10
    printableLen: equ $-printableKey

    printable2Key: db "Printable-2", 10
    printable2Len: equ $-printable2Key

    printable3Key: db "Printable-3", 10
    printable3Len: equ $-printable3Key

    printable4Key: db "Printable-4", 10
    printable4Len: equ $-printable4Key

    decimalKey: db "Decimal", 10
    decimalLen: equ $-decimalKey

    upperKey: db "Upper Case", 10
    upperLen: equ $-upperKey

    lowerKey: db "Lower Case", 10
    lowerLen: equ $-lowerKey

    smallerKey: db "Smaller", 10
    smallerLen: equ $-smallerKey

    biggerKey: db "Bigger", 10
    biggerLen: equ $-biggerKey

segment .bss
;
aChar resb 8 ; reserve 8 bytes for aChar
; I changed the above to 8 and that seems to work with the code below, 
; I don't know if its crucial to its execution. 

segment .text
        global _start
_start:

    ; ### Code for Outputting a simple Message ###
    mov eax, 4 ; select kernal call #4
    mov ebx, 1 ; default output device
    mov ecx, msg ; second argument; pointer to message
    mov edx, len ; third argument: length
    int 0x80 ; invoke kernal call to write

    mov eax, 3 ; select kernal call #3
    mov ebx, 0 ; default input device
    mov ecx, aChar ; pointer to aChar'
    int 0x80 ; invoke kernal call to read

control: ; is it a control key?
    mov al,  [aChar] ; 
    cmp al, 1Fh
    jg  printable

    ; Output
    mov eax, 4
    mov ebx, 1
    mov ecx, controlKey
    mov edx, controlLen
    int 0x80
    jmp exit ; because duh

decimal:    
    mov al,  [aChar] ; 
    cmp al, '9'
    jg  printable2

    ; Output
    mov eax, 4
    mov ebx, 1
    mov ecx, decimalKey
    mov edx, decimalLen
    int 0x80    
    jmp exit

printable:
    mov al,  [aChar] ; 
    cmp al, '/'
    jg  decimal

    ; Output
    mov eax, 4
    mov ebx, 1
    mov ecx, controlKey
    mov edx, controlLen
    int 0x80
    jmp exit ; because duh

printable2:
    mov al,  [aChar] ; 
    cmp al, '@'
    jg  uppercase

    ; Output
    mov eax, 4
    mov ebx, 1
    mov ecx, printable2Key
    mov edx, printable2Len
    int 0x80
    jmp exit ; because duh

uppercase:  
    mov al,  [aChar] ; 
    cmp al, 'Z'
    jg printable3

    ; Output
    mov eax, 4
    mov ebx, 1
    mov ecx, upperKey
    mov edx, upperLen
    int 0x80
    jmp exit ; because duh

printable3:
    mov al,  [aChar] ; 
    cmp al, '`'
    jg  lowercase

    ; Output
    mov eax, 4
    mov ebx, 1
    mov ecx, printable3Key
    mov edx, printable3Len
    int 0x80
    jmp exit ; because duh

lowercase:
    mov al,  [aChar] ; 
    cmp al, 7Ah
    jg  printable4

    ; Output
    mov eax, 4
    mov ebx, 1
    mov ecx, lowerKey
    mov edx, lowerLen
    int 0x80
    jmp exit ; because duh

printable4:
    mov al,  [aChar] ; 
    cmp al, '~'
    jg  control2

    ; Output
    mov eax, 4
    mov ebx, 1
    mov ecx, printable4Key
    mov edx, printable4Len
    int 0x80
    jmp exit ; because duh

control2: ; is it a control key?
    mov al,  [aChar] ; 
    cmp al, 7Fh
    jg  exit ; beyond range

    ; Output
    mov eax, 4
    mov ebx, 1
    mov ecx, control2Key
    mov edx, control2Len
    int 0x80
    jmp exit ; because duh

exit:
    mov eax, 1
    xor ebx, ebx
    int 0x80

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接