首页 > 解决方案 > 用汇编语言将字符存储在内存位置

问题描述

我正在尝试用汇编语言编写一个程序,它将用 'U' 替换字符串 STRVAR 中的所有字母 'T' 并将新字符串放在 OUTPUT 中。我认为我应该在整个循环过程中将每个字符一个接一个地存储在 OUTPUT 中,尽管在对 mov 进行了几次试验和错误之后,我已经没有关于如何将字符存储在新内存位置的想法。

STRVAR db "ACGTACGTCCCTTT",0
OUTPUT times 21 db 0

section .text
global CMAIN
CMAIN:
    ;write your code here

    lea esi, [STRVAR]
    
L1: 
    mov al, [esi]
    cmp al, 0
    JE FINISH
    cmp al, 'T'
    JE REPLACE
    JNE CONTINUE
    inc esi
    jmp L1
    
REPLACE:
    ;store character here
    inc esi
    jmp L1
    
CONTINUE:
    ;store character here
    inc esi
    jmp L1
    
FINISH:
    
    xor eax, eax
    ret

标签: stringassemblyreplacex86character

解决方案


我遵循了 Jester 分享的信息,最终让程序根据规范中的说明工作。我意识到我需要添加section .data并引入另一个点,在这种情况下,lea edi, [OUTPUT]存储每个字符并使用它来打印一个新字符串。

%include "io.inc"
section .data

STRVAR db "ACGTACGTCCCTTT",0
OUTPUT times 21 db 0

section .text

global CMAIN
CMAIN:
    ;write your code here

    lea esi, [STRVAR]
    lea edi, [OUTPUT]
    
L1: 
    mov al, [esi]
    cmp al, 0
    JE FINISH
    cmp al, 'T'
    JE REPLACE
    JNE CONTINUE
    inc esi
    inc edi
    jmp L1
    
REPLACE:
    mov byte[edi], 'U'
    inc esi
    inc edi
    jmp L1
    
CONTINUE:
    mov byte[edi], al
    inc esi
    inc edi
    jmp L1
    
FINISH:
    mov byte [edi], 0
    PRINT_STRING OUTPUT
    PRINT_DEC 1, [edi] ;check if the terminating 0 is also included
    xor eax, eax
    ret

推荐阅读