2014-10-05 4 views
0

나는 현재 나의 연구를위한 어셈블리 언어를 배우려고하고있다. 나는 단지 시행 착오를 시도 할 것이라고 생각했지만, 나는 그렇게 할 수있을만큼의 충분한 어셈블리를 이해하지 못한다. 저는 2 개의 숫자를 입력 할 수있는 간단한 프로그램을 작성하고 두 개의 숫자를 함께 인쇄하기 만하면됩니다. 리눅스 x86-64와 개행에 두 개의 숫자 추가하기

난 그냥 입력에 두 숫자를 시도하여 그 둘을 인쇄하는 것보다 시작했지만, 내가 두 번째 숫자를 입력 할 때 나는 세그먼트 오류를 ​​얻을.

내가 알고 싶은 또 다른 것은 내가 가진 이유 "% d"대신 "% d \ n"을 (를) 넣으면 행에 두 번 번호를 입력 할 수 있습니다. 나는 별도의 줄 번호를 인쇄하고 싶지만 그렇게하려고하면 두 번 연속으로 번호를 입력해야합니다. 왜 이런 일이 일어나고 어떻게 해결할 수 있는지 알고 싶습니다.

이전에 물어 본 경우 미안합니다. 이전에이 게시물을 보았을 지 모르지만 다른 사람들이 가지고있는 코드뿐만 아니라 솔루션을 이해하는 것이 어렵다는 것을 알게되었습니다.

.text 

    string: .asciz "Your first program\n" 
    number1: .asciz "%u" 
    number2: .asciz "%d" 

.global main 

main: 

    movq $0, %rax 
    movq $string, %rdi 
    call printf 
    call adding 
    call end 

adding: 

    movq %rsp, %rbp 
    subq $8, %rsp 
    leaq -8(%rbp), %rsi 
    movq $number1, %rdi 
    movq $0, %rax 
    call scanf 
    popq %rsi 
    movq $number1, %rdi 
    movq $0, %rax 
    call printf 
    movq $0, %rdi 
    subq $8, %rsp 
    leaq -8(%rbp), %rsi 
    movq $number2, %rdi 
    movq $0, %rax 
    call scanf 
    popq %rsi 
    movq $number2, %rdi 
    movq $0, %rax 
    call printf 
    movq -16(%rbp), %rax 
    movq %rbp, %rsp 
    popq %rbp 
    movq -16(%rbp), %rax 
    movq %rbp, %rsp 
    popq %rbp 
    ret 

end: 

    movq $0, %rdi 
    call exit 
+0

. format-string을''% d \ n "/"% u \ n "'으로 변경하거나'printf()'다음에'fflush (stdout)'을 호출하십시오. 충돌은'popf % rsi'가 첫 번째'scanf()'다음에 있기 때문에 발생하지만'scanf()'를 두 번 호출하기 전에 다시 스택에 공간을 만들지 않습니다. – EOF

+0

그래서 정확히 편집/삽입해야하는 것은 무엇입니까? – asiannoob

답변

0

는 다음 프로그램에 적용 할 몇 가지 수정 사항은 다음과 같습니다 당신은 당신의 번호를 인쇄 한 후`stdout`을 세척하지 않는

.text 

    string: .asciz "Your first program\n" 
    number1in: .asciz "%u"  #use different format strings for input and output 
    number2in: .asciz "%d" 
    number1out: .asciz "%u\n" #added "\n" to flush stdout. 
    number2out: .asciz "%d\n" #same 

.global main 

main: 
    movq $0, %rax 
    movq $string, %rdi 
    push %rbp   #dummy push to satisfy 16-byte alignment requirements 
    call printf 
    call adding 
    call end 

adding: 
    pushq %rbp   #must preserve %rbp before... 
    movq %rsp, %rbp  #...%rbp is overwritten here 
    subq $16, %rsp  #must subtract 16 from %rsp to satisfy 16-byte alignment 
    leaq -8(%rbp), %rsi 
    movq $number1in, %rdi 
    movq $0, %rax 
    call scanf 
    movq -8(%rbp), %rsi #this line must be edited... 
    movq $number1out, %rdi 
    movq $0, %rax 
    call printf 
    movq $0, %rdi 
    leaq -8(%rbp), %rsi 
    movq $number2in, %rdi 
    movq $0, %rax 
    call scanf 
    movq -8(%rbp), %rsi #as must this... 
    movq $number2out, %rdi 
    movq $0, %rax 
    call printf 
    movq %rbp, %rsp 
    popq %rbp 
    ret 

end: 
    pushq %rbp   #dummy push to satisfy 16-byte alignment 
    movq $0, %rdi 
    call exit 
+0

정확히 언제 2 개의 숫자를 더 했습니까? – asiannoob

+0

특히 내게 당신의 일을하라고 할 때, 나의 관용에 한계가 있어야합니다. segfaulting에서 프로그램을 중단하는 방법을 묻는다면 괜찮습니다. 나에게 당신을 위해 그것을 써달라고 부탁드립니다. – EOF

+0

죄송합니다. 그런 뜻이 아니 었습니다. 나는 너의 도움에 정말로 감사한다. – asiannoob