如何在x86汇编代码中使用数组来替换单词的字母?

时间:2013-03-30 20:42:46

标签: arrays assembly x86 intel

嘿大家所以我正在处理涉及数组装配的任务。我需要让用户输入一个数字,然后清除屏幕。在那之后,第二个玩家试图猜出这个词。我做了所有这些,但每次第二个玩家试图猜测时我也必须显示一个提示。例如,如果我输入单词hello,程序会在第二个玩家尝试猜测时显示h!l!o。我试过了,但不能让它工作。非常感谢任何帮助,谢谢。

.data

chose:
    .ascii "Enter the Secret Word\n"
chose_length:
    .int 22

lets_play_response:
    .ascii "Try to Guess the Word Entered\n"
l_p_response_length:
    .int 30 

wrong_guess:
    .ascii "Incorrect Guess, Try Again\n"
wrong_guess_length:
    .int 27

correct:
    .ascii "Correct Guess, Good Job\n"
correct_length:
    .int 24

Screen_Clearer:
    .ascii "\x1B[H\x1B[2J"          
Screen_Clearer_length:              
    .int 11

letter:                                             
    .space 15                       
guess:
    .space 15                       

.text
.global _start

_start:
mov $chose, %ecx                
mov chose_length, %edx          
mov $4, %eax                    
mov $1, %ebx                    
int $0x80                       

mov $letter, %ecx               
mov $15, %edx                   
mov $3, %eax                    
mov $0, %ebx                    
int $0x80                       

call Screen_Clear               

mov $lets_play_response, %ecx   
mov l_p_response_length, %edx   
mov $4, %eax                    
mov $1, %ebx                    
int $0x80                       

# Method to Print Word With Every Second Letter Replaced With ! 
# This is the area with the problems everything else works

mov $0, %edi

Loop:
    cmp $4, %edi
    jg End 

    mov $33, letter (%edi)
    add $1, %edi
    jmp Loop

End:    
    mov $letter, %ecx
    mov $4, %eax
    mov $1, %ebx
    ret

# End of Method

call GuessLoop                  

mov $1, %eax                    
int $0x80                   

GuessLoop:                          
    mov $guess, %ecx                
    mov $15, %edx                   
    mov $3, %eax                    
    mov $0, %ebx                    
    int $0x80                       

mov guess, %ecx                 
mov letter, %edx                
cmp %ecx, %edx                  
jne Incorrect                   
je Correct                      

Incorrect:                          
    mov $wrong_guess, %ecx          
    mov wrong_guess_length, %edx    
    mov $4, %eax                    
    mov $1, %ebx                    
    int $0x80                       

    jmp GuessLoop                   

Correct:                            
    mov $correct, %ecx              
    mov correct_length, %edx        
    mov $4, %eax                    
    mov $1, %ebx                    
    int $0x80                       
    ret                             

# Method That Clears the Screen #
Screen_Clear:
    mov $Screen_Clearer, %ecx
    mov Screen_Clearer_length, %edx
    mov $4, %eax
    mov $1, %ebx
    int $0x80
    ret
# End of Method to Clear Screen

1 个答案:

答案 0 :(得分:0)

如果您要使用汇编,则需要了解Addressing ModesAddressing Modes on Google

在此示例中,我使用[Base + Index]模式。您将需要一个变量来保存您的提示字符串。它不是AT& T语法,但它会给你一个想法

%define sys_exit    1
%define sys_write   4
%define sys_read    3
%define stdin       0
%define stdout      1

SECTION .bss    
hint        resb    15
letter      resb    15
leter_len   equ $ - letter

SECTION .text
global _start
_start:

    mov     ecx, letter
    mov     edx, leter_len
    mov     ebx, stdin
    mov     eax, sys_read
    int     80H

    mov     esi, hint
    mov     edi, letter
    xor     ecx, ecx
    dec     eax

.MakeHint:
    mov     dl, byte [edi + ecx]        ; get byte from pointer + index
    cmp     dl, 10                      ; is it linefeed
    je      .ShowIt

    mov     byte[esi + ecx], dl         ; move byte into hint buffer
    inc     ecx                         ; increase index
    cmp     ecx, eax                    ; at the end?
    je      .ShowIt
    mov     byte[esi + ecx], 33         ; move ! to next index
    inc     ecx                         ; increase index
    cmp     ecx, eax                    ; at end?
    jne     .MakeHint

.ShowIt:
    mov     ecx, hint
    mov     edx, leter_len
    mov     ebx, stdout
    mov     eax, sys_write 
    int     80H

    mov     eax, sys_exit                
    xor     ebx, ebx                      
    int     80h