x86字符输出循环

x86字符输出循环,x86,X86,我正在制作一个x86汇编语言程序,我已经将我的名字存储在代码的数据部分,我想创建一个循环,每次输出一个字符。我不知道该怎么办。任何帮助都会很好。我是x86新手。到目前为止,我已经: .DATA name DWORD 4E617465h .CODE main PROC mov eax, name (begin my loop here) 如果将名称存储为一个字符序列,那么这将稍微容易一些(至少在概念上)。然后,您可以从指向字符序列开头的指针开始,打印指向的字符,增加指针,然后继续循

我正在制作一个x86汇编语言程序,我已经将我的名字存储在代码的数据部分,我想创建一个循环,每次输出一个字符。我不知道该怎么办。任何帮助都会很好。我是x86新手。到目前为止,我已经:

.DATA
name DWORD 4E617465h

.CODE
main PROC
     mov eax, name
 (begin my loop here)

如果将名称存储为一个字符序列,那么这将稍微容易一些(至少在概念上)。然后,您可以从指向字符序列开头的指针开始,打印指向的字符,增加指针,然后继续循环,直到到达末尾

在循环条件中,您需要某种方法来确定是否已到达终点。您可以将字符串的长度存储为一个单独的整数常量,也可以在表示字符串结尾的字符序列末尾附加某种类型的sentinel值。并非巧合,这是字符串在C中的表示方式,使用NUL字符(
0
)作为表示字符串结尾的哨兵

比如:

name  DB 'Nate', 00H


main PROC
    mov   edx, OFFSET [name]     ; get pointer to beginning of string

  TopOfLoop:
    movzx eax, BYTE PTR [edx]    ; get the current character

    ; TODO: print the current character in EAX however you want to do it:
    ;       calling the printf() standard-library function, making a BIOS call, etc.

    inc    edx                   ; increment pointer so it points to the to next character

    cmp    BYTE PTR [edx], 0     ; keep looping as long as the next character is not NUL,
    jne    TopOfLoop             ;  which we use to denote the end of the string

    xor    eax, eax              ; clear EAX register so we return 0
    ret                          ; return from main procedure
main ENDP
name  DWORD 4E617465h

main PROC
    mov   edx, DWORD PTR [name]  ; load value into EDX
    bswap edx                    ; reverse the byte order for convenience

  TopOfLoop:
    movzx eax, dl                ; get the current character

    ; TODO: print the current character in EAX however you want to do it:
    ;       calling the printf() standard-library function, making a BIOS call, etc.

    shr   edx, 8                 ; shift-right by 8, lopping off the current character,
                                 ;  and queueing up the next one to process

    test  edx, edx               ; are there any more chars to process?
    jne   TopOfLoop              ; if so, keep looping

    xor   eax, eax               ; clear EAX register so we return 0
    ret                          ; return from main procedure
main ENDP

如果要使用当前代码,其中存储了与ASCII字符序列对应的整数值,则需要更加努力地工作。具体地说,您需要从压缩整数值中一次提取一个字节,但需要按相反的顺序进行,因为x86是小端

4E617465 ==> 4E 61 74 65 ==> E T A N
我宁愿先反转顺序,然后再向前循环,而不是以相反的顺序进行循环。为此,我将使用
BSWAP
指令,但您也可以使用
XCHG
ROR
指令序列手动执行(
BSWAP
既简单又快速)。这将给你:

6574614E ==> 65 74 61 4E ==> N A T E
然后,一旦数字顺序正确,我们就一个接一个地读出它们。每次通过循环时,我们都会将临时值右移8,这将推掉已处理的字符。一旦临时值为0,我们将停止循环,这意味着没有更多的字符(字节)需要处理

比如:

name  DB 'Nate', 00H


main PROC
    mov   edx, OFFSET [name]     ; get pointer to beginning of string

  TopOfLoop:
    movzx eax, BYTE PTR [edx]    ; get the current character

    ; TODO: print the current character in EAX however you want to do it:
    ;       calling the printf() standard-library function, making a BIOS call, etc.

    inc    edx                   ; increment pointer so it points to the to next character

    cmp    BYTE PTR [edx], 0     ; keep looping as long as the next character is not NUL,
    jne    TopOfLoop             ;  which we use to denote the end of the string

    xor    eax, eax              ; clear EAX register so we return 0
    ret                          ; return from main procedure
main ENDP
name  DWORD 4E617465h

main PROC
    mov   edx, DWORD PTR [name]  ; load value into EDX
    bswap edx                    ; reverse the byte order for convenience

  TopOfLoop:
    movzx eax, dl                ; get the current character

    ; TODO: print the current character in EAX however you want to do it:
    ;       calling the printf() standard-library function, making a BIOS call, etc.

    shr   edx, 8                 ; shift-right by 8, lopping off the current character,
                                 ;  and queueing up the next one to process

    test  edx, edx               ; are there any more chars to process?
    jne   TopOfLoop              ; if so, keep looping

    xor   eax, eax               ; clear EAX register so we return 0
    ret                          ; return from main procedure
main ENDP

如果将名称存储为一个字符序列,那么这将稍微容易一些(至少在概念上)。然后,您可以从指向字符序列开头的指针开始,打印指向的字符,增加指针,然后继续循环,直到到达末尾

在循环条件中,您需要某种方法来确定是否已到达终点。您可以将字符串的长度存储为一个单独的整数常量,也可以在表示字符串结尾的字符序列末尾附加某种类型的sentinel值。并非巧合,这是字符串在C中的表示方式,使用NUL字符(
0
)作为表示字符串结尾的哨兵

比如:

name  DB 'Nate', 00H


main PROC
    mov   edx, OFFSET [name]     ; get pointer to beginning of string

  TopOfLoop:
    movzx eax, BYTE PTR [edx]    ; get the current character

    ; TODO: print the current character in EAX however you want to do it:
    ;       calling the printf() standard-library function, making a BIOS call, etc.

    inc    edx                   ; increment pointer so it points to the to next character

    cmp    BYTE PTR [edx], 0     ; keep looping as long as the next character is not NUL,
    jne    TopOfLoop             ;  which we use to denote the end of the string

    xor    eax, eax              ; clear EAX register so we return 0
    ret                          ; return from main procedure
main ENDP
name  DWORD 4E617465h

main PROC
    mov   edx, DWORD PTR [name]  ; load value into EDX
    bswap edx                    ; reverse the byte order for convenience

  TopOfLoop:
    movzx eax, dl                ; get the current character

    ; TODO: print the current character in EAX however you want to do it:
    ;       calling the printf() standard-library function, making a BIOS call, etc.

    shr   edx, 8                 ; shift-right by 8, lopping off the current character,
                                 ;  and queueing up the next one to process

    test  edx, edx               ; are there any more chars to process?
    jne   TopOfLoop              ; if so, keep looping

    xor   eax, eax               ; clear EAX register so we return 0
    ret                          ; return from main procedure
main ENDP

如果要使用当前代码,其中存储了与ASCII字符序列对应的整数值,则需要更加努力地工作。具体地说,您需要从压缩整数值中一次提取一个字节,但需要按相反的顺序进行,因为x86是小端

4E617465 ==> 4E 61 74 65 ==> E T A N
我宁愿先反转顺序,然后再向前循环,而不是以相反的顺序进行循环。为此,我将使用
BSWAP
指令,但您也可以使用
XCHG
ROR
指令序列手动执行(
BSWAP
既简单又快速)。这将给你:

6574614E ==> 65 74 61 4E ==> N A T E
然后,一旦数字顺序正确,我们就一个接一个地读出它们。每次通过循环时,我们都会将临时值右移8,这将推掉已处理的字符。一旦临时值为0,我们将停止循环,这意味着没有更多的字符(字节)需要处理

比如:

name  DB 'Nate', 00H


main PROC
    mov   edx, OFFSET [name]     ; get pointer to beginning of string

  TopOfLoop:
    movzx eax, BYTE PTR [edx]    ; get the current character

    ; TODO: print the current character in EAX however you want to do it:
    ;       calling the printf() standard-library function, making a BIOS call, etc.

    inc    edx                   ; increment pointer so it points to the to next character

    cmp    BYTE PTR [edx], 0     ; keep looping as long as the next character is not NUL,
    jne    TopOfLoop             ;  which we use to denote the end of the string

    xor    eax, eax              ; clear EAX register so we return 0
    ret                          ; return from main procedure
main ENDP
name  DWORD 4E617465h

main PROC
    mov   edx, DWORD PTR [name]  ; load value into EDX
    bswap edx                    ; reverse the byte order for convenience

  TopOfLoop:
    movzx eax, dl                ; get the current character

    ; TODO: print the current character in EAX however you want to do it:
    ;       calling the printf() standard-library function, making a BIOS call, etc.

    shr   edx, 8                 ; shift-right by 8, lopping off the current character,
                                 ;  and queueing up the next one to process

    test  edx, edx               ; are there any more chars to process?
    jne   TopOfLoop              ; if so, keep looping

    xor   eax, eax               ; clear EAX register so we return 0
    ret                          ; return from main procedure
main ENDP

是否有我必须导入的库,以便将其打印到控制台,或者我可以使用单个命令执行此操作?汇编中没有单个命令用于打印内容。你需要一些图书馆的支持。如果您在某种仿真器上编程,您可能能够调用BIOS或DOS中断来输出文本。在DOS中,这将是
int21h
,服务2,
DL
寄存器将包含要打印的字符。如果您在现代操作系统上运行,最简单的方法是链接到C标准库,然后调用类似于
printf
的函数@nateis是否有一个库我必须导入,以便可以将它打印到控制台,或者我可以用一个命令来完成?汇编中没有一个命令可以打印东西。你需要一些图书馆的支持。如果您在某种仿真器上编程,您可能能够调用BIOS或DOS中断来输出文本。在DOS中,这将是
int21h
,服务2,
DL
寄存器将包含要打印的字符。如果您在现代操作系统上运行,最简单的方法是链接到C标准库,然后调用类似于
printf
的函数@内特