【发布时间】:2020-02-03 17:33:46
【问题描述】:
在了解了 MARS 中的 .macros 后,我决定制作一些以使我的源代码更具可读性。
这种宏的一个例子是我的 string length 宏:
#Gets the length of a string from an address
#Result is in the second operand
#Doesn't count any null-terminators or newlines at the end of the input
#Call example: strlen MyString,$s0
.macro strlen(%str, %i)
la $t0,%str
__strlen_loop:
lb $t1,0($t0)
#Exit the loop if this byte is null ('\0') or a newline ('\n')
beqz $t1,__strlen_end
beq $t1,'\n',__strlen_end
inc $t0 #Increment-by-one macro
j __strlen_loop
__strlen_end:
la $t1,%str
sub %i,$t0,$t1
.end_macro
此宏有效,但它依赖于预定义的地址才能工作 (%str)。
为了尝试解决这个问题,我为 value 字符串宏创建了以下 string length:
#Gets the string length of a value string (stores the string in .data)
#Call example: strlen_v "Hello World!",$s0
.macro strlen_v(%str,%i)
#Create a label for the string
.data
__strlen_v_label: .asciiz %str
.text
#Get the length
strlen __strlen_v_label,%i
.end_macro
不幸的是,还有另一个问题。第二个宏将值字符串存储在.data 中,它只有0x30000 字的空间(地址0x10010000 到地址0x10040000)。
最好将值字符串存储在 堆 上,因为它有更多空间并且我将能够更有效地管理内存。
是否可以使用.data不在堆上存储编译时值字符串?我发现的唯一示例是用户输入字符串和已存储在.data 中的字符串,这两者都不适用于此问题。
【问题讨论】: