2016-12-16 79 views
0

我正在制作一个x86汇编语言程序,并且已将我的名称存储在代码的数据部分,我想创建一个循环来一次输出一个字符。我迷失在为此而做的事情上。任何帮助都会很棒。我是x86新手。到目前为止,我有:x86字符输出循环

.DATA 
name DWORD 4E617465h 

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

回答

2

如果存储的名称作为字符序列这将是比较容易(至少在概念上)。然后,您可以从指向字符序列开头的指针开始,打印指向的字符,增加指针并继续循环,直到达到结尾。

在条件循环中,您需要一些方法来确定您是否已达到结尾。您可以将字符串的长度存储为单独的整数常量,您可以将某种标记值附加到表示字符串末尾的字符序列的末尾。并非巧合的是,这是字符串在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 

如果你想使用当前的代码,你到哪儿去存储对应字符的ASCII序列的整数值,你需要努力一点更难。具体来说,您需要从您的打包整数值中一次提取一个字节,但是您需要按反向顺序执行,因为x86是小端。

4E617465 ==> 4E 61 74 65 ==> E T A N 

,而不是实际操作的方式以相反的顺序循环,我宁愿第一扭转序列,然后用它循环在前进方向。要做到这一点,我会使用BSWAP指令,但您也可以使用XCHGROR指令(BSWAP既简单又快捷)的顺序手动执行。这会给你:

6574614E ==> 65 74 61 4E ==> N A T E 

然后,一旦数字是按照正确的顺序,我们就看他们,一个接一个。每次通过循环时,我们都会将临时值右移 8,这会推开处理后的字符。一旦临时值为0,我们将停止循环,这意味着没有更多的字符(字节)需要处理。

喜欢的东西:

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 
+0

是有图书馆,我将不得不进口,使这可以打印到控制台或我能做到这一点用一个命令? –

+0

程序集中没有单个命令来打印东西。你需要某种库支持。如果您在某种模拟器上进行编程,则可能可以调用BIOS或DOS中断来输出文本。在DOS中,这将是'INT 21h',服务2和'DL'寄存器将包含要打印的字符。如果你使用的是现代操作系统,最简单的做法是链接到C标准库,然后调用类似'printf'的东西。 @nate –