Assembly 为什么';在我的asm代码中输入呼叫工作?
我用汇编语言尝试了以下数据块传输代码。代码在非重叠情况下运行良好,但我无法键入输入以获取“count”值Assembly 为什么';在我的asm代码中输入呼叫工作?,assembly,x86-64,nasm,Assembly,X86 64,Nasm,我用汇编语言尝试了以下数据块传输代码。代码在非重叠情况下运行良好,但我无法键入输入以获取“count”值 section .data name db 'John' ;initialize name array name_len equ $-name ;initialize name length surname db 'Smith' ;initialize surname array sur_len equ $-surname ;int
section .data
name db 'John' ;initialize name array
name_len equ $-name ;initialize name length
surname db 'Smith' ;initialize surname array
sur_len equ $-surname ;intialize surname length
tot equ name_len + sur_len ;initialize total length
msg1 db "Initial name is: " ;message 1
msg1_len equ $-msg1 ;length of message
msg2 db 0xa,"Non-overlapped transfer: " ;message2
msg2_len equ $-msg2
msg3 db "Overlapped String transfer: "
msg3_len equ $-msg3
endl db 0xa
endlen equ $-endl
steps db 0 ;Stores the value length-count
msg4 db "Enter 1,2 or 3"
msg4_len equ $-msg4
msg5 db "Enter the count "
msg5_len equ $-msg5
section .bss
choice resb 1 ;for non-overlapped or overlapped
a resb 1 ;storing final length in overlapped case
count resb 1 ;Count is the number of letters which are overlapped
section .text
global _start ;linker
_start:
beg:
mov eax,4
mov ebx,1
mov ecx,msg4
mov edx,msg4_len
int 0x80
mov eax,4
mov ecx,endl
mov edx,endlen
int 0x80
mov eax,3
mov ebx,1
mov ecx,choice
mov edx,1
int 0x80
cmp byte[choice],0x33
je exit
cmp byte[choice],0x32
je overcall
cmp byte[choice],0x31
je noncall
noncall:
call non
jmp exit
overcall:
mov eax,4
mov ebx,1
mov ecx,msg5
mov edx,msg5_len
int 0x80
mov eax,3
mov ebx,1
mov ecx,count ;I can't input any value for count in the terminal
mov edx,1
int 0x80
sub byte[count],0x30
mov al,name_len
add al,sur_len
sub al,[count]
mov [a],al
call over
jmp exit
exit:
mov eax,1 ;syscall for exit
int 0x80 ;call kernel
non:
mov eax,4 ;syscall for print
mov ecx,msg1 ;move address of msg1 to ecx
mov edx,msg1_len ;move msg1 length to edx
int 0x80 ;call kernel
mov eax,4 ;syscall for print
mov ecx,name ;move address of name to ecx
mov edx,name_len ;move name length to edx
int 0x80 ;call kernel
mov esi,name ;move address of name to esi
mov edi,surname ;move surname address to edi
add esi,name_len ;add name_length to move to last byte
mov ebx,sur_len ;move length of surname to ebx
up:
mov eax,[edi] ;move character of surname to eax
mov [esi],eax ;move character to last of name
inc esi ;increment esi
inc edi ;increment edi
dec ebx ;decrement count
jnz up ;jumpif not zero to up
mov eax,4 ;syscall for print
mov ecx,msg2 ;move address of name to ecx
mov edx,msg2_len ;move name length to edx
int 0x80 ;call kernel
mov eax,4 ;syscall for print
mov ecx,name ;move address of name to ecx
mov edx,tot ;move total length to edx
int 0x80 ;call kernel
mov eax,4
mov ecx,endl
mov edx,endlen
int 0x80
ret
over:
mov eax,4
mov ecx,msg3
mov edx,msg3_len
int 0x80
xor rax,rax
mov rsi,name
mov rdi,surname
mov al,name_len
sub al,[count]
mov [steps],al
add rsi,rax
mov ecx,sur_len
xor rax,rax
upp:
mov al,[rdi]
mov [rsi],al
inc rsi
inc rdi
dec ecx
jnz upp
mov eax,4
mov ecx,name
mov edx,[a]
int 0x80
mov eax,4
mov ecx,endl
mov edx,endlen
int 0x80
ret
在选择选项2后,程序只是打印一些垃圾
以下是输出:
Enter 1,2 or 3
2
Enter the count Overlapped String transfer: JohnSmithInitial name is:
Non-overlapped Smithfer: Overlapped String transfer:
*Enter 1,2 or 3Enter the count 2/�
如果我只是初始化计数值,而不是从用户处接受它,代码就可以正常工作了您实际上键入了
2
,然后键入enter
,然后将该enter(换行符)保留在输入缓冲区中,因此读取计数将返回该值。可能不会导致您在该代码中出现问题,但是您标记了这个x86-64(实际上使用了64位寄存器好几次),但是代码的其余部分都使用32位系统调用,甚至32位寻址模式!!当您尝试以这种方式使用任何堆栈地址时,这将立即中断,因为(与静态数据不同),堆栈内存的地址高于2^32,超出了内存的低4GiB。寻址模式将出现故障,int 0x80将返回-EFAULT
@Jester在这种情况下,我应该如何给出输入?@AnishKoulgi读取,直到遇到换行符或输入结束。