我正在发布关于同一程序的新帖子 - 很抱歉,但我认为我的问题与前一个问题大不相同。我的程序在开始时有 2 个参数 - 重复次数和一个字符串。重复次数决定了字符串中最后一个单词应该打印多少次。例如:
./a.out 3 "ab cd"
显示在输出中
cdcdcd
我已经使用调用 printf 制作了(在 Stack 用户的帮助下 :-))一个工作程序。它仅适用于 0-9 次重复,但它并不像主要内容那么重要 - 我的问题是如何用 sys_write 调用替换这个“调用 printf”。
我得到了我必须使用它来编译它的信息
-nostdlib
选项,但如果我的代码不正确也没关系。我尽了最大努力,我还在这里找到了一些有关可能方法的信息,但我无法使其正常工作。
打印新行效果很好,但我不知道如何处理与 sys_write 连接的参数 #2 中的字符串。如果有经验的人能找到一些时间并指出我需要在代码中更改的内容,那就太好了。通过“调用 printf”版本花了一些时间,但后来我能够进行实验,现在我完全迷失了。这里是:
.intel_syntax noprefix
.globl _start
.text
_start:
push ebp
mov ebp, esp
mov ecx, [ebp + 4] # arg1 int ECX
mov ebx, [ebp + 8] # arg2 string EBX
xor eax, eax
# ARG1 - FROM STRING TO INT
atoi:
movzx edx, byte ptr [ecx]
cmp edx, '0'
jb programend
sub edx, '0'
mov ecx, edx
## =========================== FUNCTION =========================== ##
# SEARCH FOR END OF STRING
findend:
mov dl, byte ptr [ebx + eax] # move through next letters
cmp dl, 0
jz findword
inc eax
jmp findend
# SEARCH FOR LAST SPACE
findword:
dec eax
mov dl, byte ptr [ebx + eax]
cmp dl, ' '
jz foundwordstart
jmp findword
# REMEMBER SPACE POSITION, CHECK COUNTER >0
foundwordstart:
push eax # remember space position
cmp ecx, 0 # check if counter > 0
jz theend
jmp foundword
# PRINT LAST WORD
foundword:
inc eax
mov dl, byte ptr [ebx + eax]
cmp dl, 0
jz checkcount
push ecx
push eax # save current position in word
push edx
push ebx
lea ecx, [ebx+eax] # char * to string
mov eax, 4 # sys_write
mov edx, 1; # how many chars will be printed
mov ebx, 1 # stdout
int 0x80
pop ebx
pop edx
pop eax
pop ecx
jmp foundword
# decrease counter and restore beginning of last word
checkcount:
dec ecx # count = count-1
pop eax # restore beginning of last word
jmp foundwordstart
theend:
pop eax # pop the space position from stack
jmp programend
# END OF PROGRAM
programend:
pop ebp
# new line
mov eax,4
mov ebx,1
mov ecx,offset msgn
mov edx,1
int 0x80
# return 0
mov eax, 1
mov ebx, 0
int 0x80
.data
msgn: .ascii "\n"
对我来说,我可以运行它也很奇怪:
mov ecx, [ebp + 12]
mov ecx, [ecx + 8]
add ecx, eax # char * to string
mov eax, 4 # sys_write
mov edx, 1; # how many chars will be printed
mov ebx, 1 # stdout
int 0x80
并且效果很好-但前提是我不使用-nostdlib(当然我必须将_start更改为main)...