【发布时间】:2015-05-16 14:38:42
【问题描述】:
我在将字符串从一个内存地址按字节移动到另一个内存地址时遇到问题。在这里待了几个小时并尝试了一些不同的策略。我是 Intel 组件的新手,所以我需要一些技巧和见解来帮助我解决问题。
getText 例程应该将 n(在 %rsi 中)字节从 ibuf 传输到 %rdi 中的地址。 counterI 是用于指示从何处开始传输的偏移量,在例程结束后,它应该指向下一个未传输的字节。如果没有 n 个字节,它应该取消传输并在 %rax 中返回实际传输的字节数。
getText:
movq $ibuf, %r10
#in rsi is the number of bytes to be transfered
#rdi contains the memory adress for the memory space to transfer to
movq $0, %r8 #start with offset 0
movq $0, %rax #zero return register
movq (counterI), %r11
cmpb $0, (%r10, %r11, 1) #check if ibuf+counterI=NULL
jne MOVE #if so call and read to ibuf
call inImage
MOVE:
cmpq $0,%rsi #if number of bytes to read is 0
je EXIT #exit
movq counterI, %r9
movq $0, %r9 #used for debugging only shold not be 0
movb (%r10, %r9, 1), %bl #loads one byte to rdi from ibuf
movb %bl, (%rdi, %r8, 1)
incq counterI #increase pointer offset
decq %rsi #dec number of bytes to read
incq %r8 #inc offset in write buffert
movq %r8, %rax #returns number of bytes wrote to buf
movq (counterI), %r9
cmpb $0, (%r10, %r9,1) #check if ibuf+offset is NULL
je EXIT #if so exit
cmpq $0, %rsi #can be cleaned up later
jne MOVE
EXIT:
movb $0, (%rdi, %r8, 1) #move NULL to buf+%r8?
ret
【问题讨论】:
-
这个函数的语义应该是什么?
counterI起什么作用?inImage是什么?为什么要将读取的字节进行零扩展,然后写入整个 qword? (这将在末尾写入 7 个额外的零,通常不需要)此外,如果您要复制字符串,则不必在终止符之前停止 然后添加额外的终止符,您可以复制它并然后检测它是否是终止符。你也弹出了一些东西而没有推送一些东西/改变了 rsp,所以它会弹出返回地址,然后返回到遗忘状态。 -
getText 应该将最多 n 个字节传输到寄存器 %rdi 中的地址。 counterI 是 ibuf 中的当前偏移量。 inImage 是一个从标准输入写入字符串的函数,在本例中为 ibuf。问题是我尝试使用 movb 但当我尝试将其放入 %r# 寄存器时它会抱怨。我更改了推送 %r12 并没有注意到弹出:D 我更关注从一个内存地址移动到另一个正确的问题。