如何根据用户输入使用NASM汇编器编辑8字符串?

问题描述

我正在为班级分配作业,我必须从用户那里获取8个字符串,并从左侧删除用户请求的尽可能多的字母。必须使用NASM汇编器编写x86-64汇编。我的代码如下:

section .text
      global _start
   
_start:

;PRINT "Enter Text: "
mov edx,lenMsg2
mov ecx,msg2
mov ebx,1
mov eax,4
int 80h

;READ AND STORE USER INPUT INTO str
mov eax,3
mov ebx,2
mov ecx,str
mov edx,100
int 80h

;PRINT "Enter the number of characters to cut off: "
mov edx,lenMsg3
mov ecx,msg3
mov ebx,4
int 80h

;READ AND STORE USER INPUT INTO num
mov eax,num
mov edx,100
int 80h

;PRINT "This is what you entered: "
mov edx,lenMsg4
mov ecx,msg4
mov ebx,4
int 80h

;PRINT str
mov eax,4
mov ebx,1
mov ecx,100
int 80h

;PRINT "Here is your text edited: "
mov edx,lenMsg5
mov ecx,msg5
mov ebx,4
int 80h

;LEFT TruncATE
mov esi,str
add esi,OFFSET_INTO_DATA    ;NEED TO SOMEHOW REPLACE THIS WITH num

;RIGHT TruncATE
mov edi,NUM_CHARS

.loop:
mov ecx,esi
call print_char_32
inc esi
dec edi
jnz .loop

jmp halt

print_char_32:
mov edx,1
mov ebx,4
int 80h
ret

halt:
mov eax,1
int 80h
jmp halt

section .data
;msg  db "qwertyui" <- Test phrase used before str was made
msg2 db "Enter Text: ",0x0A
lenMsg2 equ $ - msg2
msg3 db "Enter the number of characters to cut off: ",0x0A
lenMsg3 equ $ - msg3
msg4 db "This is what you entered: ",0x0A
lenMsg4 equ $ - msg4
msg5 db "Here is your text edited: ",0x0A
lenMsg5 equ $ - msg5

str: db 100
num: db 100

OFFSET_INTO_DATA EQU 5                 ;5 was an test number used to subtract from str
NUM_CHARS EQU 8                        ;This should print the whole string no matter how many 
                                       ;characters the user removed

我希望的结果如下所示:

Enter Text:
*qwertyui*
Enter the number of characters to cut off: 
*5*
Here is what you entered:
qwertyui
Here is your text edited:
yui

代替5,最多8个数字都可以使用。我在顶部附近设置了一些参数,用于读取 num 用户输入;但是,我似乎不能仅将 num 插入 OFFSET_INTO_DATA 所在的位置。以我在 data 部分中设置的方式使用 OFFSET_INTO_DATA 可以使程序平稳运行并提供所需的结果。当我将 num 放在 text 部分中的位置时,它不起作用。相反,插入随机数也不起作用,我不知道从这里去哪里。

我一直在使用 nasm -f elf64 filename.asm ld filename.o -o filename 来编译该程序。我还使用PuTTY运行Linux。我不确定这些最后的信息是否有帮助,但这是我第一次使用此站点,我想尽可能地描述一下我的处境。任何帮助表示赞赏!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)