到目前为止,这是我的代码。
.data
S: .string "-149"
Length: .byte -1
Result: .quad
.text
.globl main
main:
mov S,%rdx #Storage of string, counter, position and result in memory
mov Length, %rcx
mov Result, %rax
mov $10, %r10
mov $30, %r13
mov $-1, %r9
Loop1: #loop string from beginning to end
cmp $0,0(%rdx) #compare base addresss value with null
je Counter_Made #if null, branch to end loop.
add %r14, Length #increment length by for each digit thats not null (creates counter for 2nd loop)
add $1, %rdx #increment base by 1 to move onto next digit in string
jmp Loop1 #reinitiate loop
Counter_Made:
cmp %r15,Length #check if length is zero
je Output #End program, output null result
cmp %r15,Length(%rdx) #Determine negativity/positivity of integer
jl Negative_counter
jmp Positive_loop
Positive_loop:
cmp %r9,Length #End of loop check
je Output #Store result if loop end condition satisfied
mov %r10, Length(%rdx) #Store byte of integer in supplementary register
sub %r13, %r10 #Conversion from 8bitASCII to 2Bit Binary
imul %r11, %r10 #Place holder multiplication
add %r10, %rax #Store cumulative addition in memory
sub %r14, Length #Length decrement
jmp Positive_loop #reloop
Negative_counter:
sub %r14,Length
jmp Negative_loop
Negative_loop:
cmp %r9,Length
je Negative_Complement
mov %r10, Length(%rdx) #Store byte of integer in supplementary register
sub %r13, %r10 #Conversion from 8bitASCII to 2Bit Binary
imul %r10, %r10 #Place holder multiplication
add 0(%rdx), %rax #Store cumulative addition in memory
sub %r14, Length
jmp Negative_loop
Negative_Complement:
not %rdx #Convert to 2's complement with negation and then + 1
add %r14,%rdx
jmp Output
Output:
mov %rdx, Result
ret
#size mismatch for imul
#Specific place in memory to put output or no?
代码应该将表示任何有符号整数的字符串转换为其2的补码值。
我在我的一个循环中收到了一个分段错误,我在这里尝试了多种不同的方法,但无济于事 - 任何人都可以解释我应该如何修复这个段错误?我很难过。
这是GDB错误
Program received signal SIGSEGV, Segmentation fault.
Loop1 () at data.s:18
18 cmp $0,0(%rdx) #compare base addresss value with null
这是第二个错误。
Counter_Made () at data.s:28
28 cmp $0,Length(%rdx) #Determine negativity/positivity of integer, if <0 value is negative
我怀疑它正在尝试解释循环的长度(%rdx)方法。 sub $1,%rdx
答案 0 :(得分:2)
你想要
mov $S,%edx
将字符串的地址加载到%rdx
。这是有效的,因为程序映像始终加载到地址空间的低4 GB。或者,您可以使用%rip
亲属lea
加载地址,即使过程映像是在前4 GB之外加载的:
lea S(%rip),%rdx
但该指令的编码时间稍长(两个额外字节)。
指令
mov S,%rdx
将内存S
点的前八个字节加载到%rdx
,这不是你想要的。