2012-03-10 14 views
8

मैं x86_64 असेंबली सीखने की कोशिश कर रहा हूं और मैं आज मानक इनपुट आउटपुट का प्रयास कर रहा था और इस पोस्ट पर ठोकर खाई Learning assembly - echo program name एसटीडीआईएन (SYSCALL निर्देश का उपयोग करके) से इनपुट पढ़ने के लिए मैं वही कैसे करूं? विशेष रूप से अगर मुझे पता है कि इनपुट हमेशा एक पूर्णांक होगा और मैं इसे एक रजिस्टर में पढ़ना चाहता हूं?x86_64 असेंबली में STDIN से इनपुट कैसे पढ़ा जाए?

संपादित करें: नीचे @Daniel Kozar का जवाब मुझे लिनक्स पर syscall अनुदेश के साथ कैसे STDIN और STDOUT सामान काम समझने में मदद की। मैंने एक छोटा प्रोग्राम लिखने का प्रयास किया, जो कंसोल इनपुट से एक संख्या पढ़ता है और उस संख्या से संबंधित एसीआईआई चरित्र को प्रिंट करता है। कहें कि क्या आप इनपुट के रूप में 65 टाइप करते हैं, आपको आउटपुट के रूप में ए प्राप्त करना चाहिए। और एक नया लाइन चरित्र भी। अगर सब पर है, यह किसी और किसी भी एक :-)

section .text 
    global _start 

_start: 
    mov rdi, 0x0  ; file descriptor = stdin = 0 
    lea rsi, [rsp+8] ; buffer = address to store the bytes read 
    mov rdx, 0x2  ; number of bytes to read 
    mov rax, 0x0  ; SYSCALL number for reading from STDIN 
    syscall   ; make the syscall 

    xor rax, rax  ; clear off rax 
    mov rbx, [rsp+8] ; read the first byte read into rsp+8 by STDIN call to rbp 
    sub rbx, 0x30  ; Since this is read as a character, it is obtained as ASCII value, so subtract by 0x30 to get the number 
    and rbx, 0xff  ; This ensures that everything other than the last byte is set to 0 while the last byte is as is 
    mov rax, rbx  ; move this value to rax since we want to store the final result in rax 
    shl rbx, 0x1  ; We need to multiply this by 10 so that we can add up all the digits read so multiplying the number by 2 and then by 8 and adding them up, so multiply by 2 here 
    shl rax, 0x3  ; multiply by 8 here 
    add rax, rbx  ; add 8 times multiplied value with 2 times multiplied value to get 10 times multiplied value 
    mov rbx, [rsp+9] ; now read the next byte (or digit) 
    sub rbx, 0x30  ; Again get the digit value from ASCII value of that digit's character 
    and rbx, 0xff  ; clear higher bytes 
    add rax, rbx  ; Add this to rax as unit's place value 
    mov [rsp+8], rax ; Move the entire byte to rax 
    mov rdi, 0x1  ; file descriptor = stdout 
    lea rsi, [rsp+8] ; buffer = address to write to console 
    mov rdx, 0x1  ; number of bytes to write 
    mov rax, 0x1  ; SYSCALL number for writing to STDOUT 
    syscall   ; make the syscall 

    xor rax, rax  ; clear off rax 
    mov rax, 0xa  ; move the new line character to rax 
    mov [rsp+8], rax ; put this on the stack 
    mov rdi, 0x1  ; file descriptor = stdout 
    lea rsi, [rsp+8] ; buffer = address to write to console 
    mov rdx, 0x1  ; number of bytes to write 
    mov rax, 0x1  ; SYSCALL number for writing to STDOUT 
    syscall   ; make the syscall 

    mov rdi, 0  ; set exit status = 0 
    mov rax, 60  ; SYSCALL number for EXIT 
    syscall   ; make the syscall 

संपादित 2 में मदद करता है: यहाँ है मेरी, मानक इनपुट से एक अहस्ताक्षरित 32-बिट दशमलव पूर्णांक पढ़ संगणना के लिए पूर्णांक के रूप में संग्रहीत और फिर लिखने के लिए प्रयास वह वापस बाहर निकलने के लिए। सब से

section .text 
     global _start 

_start: 
;Read from STDIN 
     mov rdi, 0x0  ; file descriptor = stdin = 0 
     lea rsi, [rsp+8] ; buffer = address to store the bytes read 
     mov rdx, 0xa  ; number of bytes to read 
     mov rax, 0x0  ; SYSCALL number for reading from STDIN 
     syscall   ; make the syscall 


; Ascii to decimal conversion 
     xor rax, rax  ; clear off rax 
     mov rbx, 0x0  ; initialize the counter which stores the number of bytes in the string representation of the integer 
     lea rsi, [rsp+8] ; Get the address on the stack where the first ASCII byte of the integer is stored. 

rnext: 
     mov rcx, [rsi] ; Read the byte on the stack at the address represented by rsi 
     cmp rcx, 0xa  ; Check if it is a newline character 
     je return  ; If so we are done 
     cmp rbx, 0xa  ; OR check if we have read 10 bytes (the largest 32 bit number contains 10 digits, so we will have to process at most 10 bytes 
     jg return  ; If so we are done 
     sub rcx, 0x30  ; For the byte read, subtract by 0x30/48 to get the value from the ASCII code. 0 == 0x30 in ASCII, 1 == 0x31 in ASCII and so on. 
     and rcx, 0xff  ; Clear off the higher order bytes to ensure there is no interference 
     mov rdx, rax  ; We need to multiple this by 10 to get the next byte which goes to the unit's place and this byte becomes the ten's value. So make a copy 
     shl rax, 0x3  ; Multiply the original by 8 (Shift left by 3 is multiply by 8) 
     shl rdx, 0x1  ; Multiply the copy by 2 (Shift left by 1 is multiply by 2) 
     add rax, rdx  ; Add these a * 8 + a * 2 to get a * 10. 
     add rax, rcx  ; Add the digit to be at the units place to the original number 
     add rsi, 1  ; Advance the memory address by 1 to read the next byte 
     inc rbx   ; Increment the digit counter 
     jmp rnext   ; Loop until we have read all the digits or max is reached. 

return: 
     push rax   ; Push the read number on to the stack 

; write New Line 
     mov rax, 0xa  ; move the new line character to rax 
     mov [rsp+8], rax ; put this on the stack 
     mov rdi, 0x1  ; file descriptor = stdout 
     lea rsi, [rsp+8] ; buffer = address to write to console 
     mov rdx, 0x1  ; number of bytes to write 
     mov rax, 0x1  ; SYSCALL number for writing to STDOUT 
     syscall   ; make the syscall 


; Convert from Decimal to bytes 
     xor rdx, rdx  ; Clear rdx which stores obtains a single digit of the number to convert to ASCII bytes 
     mov r8, 0x0  ; Initialize the counter containing the number of digits 

     pop rax   ; Pop the read number from the stack 
     mov rbx, 0xa  ; We store the divisor which is 10 for decimals (base-10) in rbx. rbx will be the divisor. 

wnext: 
     div rbx   ; Divide the number in rdx:rax by rbx to get the remainder in rdx 
     add rdx, 0x30 ; Add 0x30 to get the ASCII byte equivalent of the remainder which is the digit in the number to be written to display. 
     push rdx   ; Push this byte to the stack. We do this because, we get the individial digit bytes in reverse order. So to reverse the order we use the stack 
     xor rdx, rdx  ; Clear rdx preparing it for next division 
     inc r8   ; Increment the digits counter 
     cmp rax, 0x0  ; Continue until the number becomes 0 when there are no more digits to write to the console. 
     jne wnext  ; Loop until there aren't any more digits. 

popnext: 
     cmp r8, 0x0  ; Check if the counter which contains the number of digits to write is 0 
     jle endw   ; If so there are no more digits to write 
     mov rdx, 0x1  ; number of bytes to write 
     mov rsi, rsp  ; buffer = address to write to console 
     mov rdi, 0x1  ; file descriptor = stdout 
     mov rax, 0x1  ; SYSCALL number for writing to STDOUT 
     syscall   ; make the syscall 
     dec r8   ; Decrement the counter 
     pop rbx   ; Pop the current digit that was already written to the display preparing the stack pointer for next digit. 
     jmp popnext  ; Loop until the counter which contains the number of digits goes down to 0. 

endw: 
; write New Line 
     xor rax, rax  ; clear off rax 
     mov rax, 0xa  ; move the new line character to rax 
     mov [rsp+9], rax ; put this on the stack 
     mov rdi, 0x1  ; file descriptor = stdout 
     lea rsi, [rsp+9] ; buffer = address to write to console 
     mov rdx, 0x1  ; number of bytes to write 
     mov rax, 0x1  ; SYSCALL number for writing to STDOUT 
     syscall   ; make the syscall 

; Exit 
     mov rdi, 0  ; set exit status = 0 
     mov rax, 60  ; SYSCALL number for EXIT 
     syscall   ; make the syscall 
+1

आप किस ओएस का उपयोग कर रहे हैं? विंडोज? डॉस? लिनक्स? – Gabe

+0

'syscall' का उपयोग ऑपरेटिंग सिस्टम निर्भर है। – hirschhornsalz

+0

मैं लिनक्स का उपयोग कर रहा हूं। वह सटीक कोड मेरे लिए काम करता है। –

उत्तर

5

पहले: कोई चर विधानसभा में देखते हैं। किसी प्रकार के डेटा के लिए सिर्फ लेबल हैं। डेटा डिज़ाइन द्वारा, अनियमित है - कम से कम वास्तविक असेंबलरों में, एचएलए (उदा। एमएएसएम) नहीं।

मानक इनपुट से पढ़ना सिस्टम कॉल read का उपयोग कर हासिल किया जाता है। मुझे लगता है कि आपने पहले से ही पोस्ट का उल्लेख किया है और आप जानते हैं कि x64 लिनक्स में सिस्टम कॉल कैसे कॉल करें। यह मानते हुए कि आप NASM (या उसके सिंटैक्स जैसा दिखते हैं) का उपयोग कर रहे हैं, और आप stdin से buffer पर इनपुट स्टोर करना चाहते हैं, जहां आपने BUFSIZE मेमोरी बाइट आरक्षित किया है, सिस्टम कॉल निष्पादित करने से यह इस तरह दिखेगा:

xor eax, eax ; rax <- 0 (write syscall number) 
xor edi, edi ; rdi <- 0 (stdin file descriptor) 
mov rsi, buffer ; rsi <- address of the buffer 
mov edx, BUFSIZE ; rdx <- size of the buffer 
syscall ; execute 

लौटने पर, rax में सिस्कल का परिणाम होगा। यदि आप यह कैसे काम करते हैं, इसके बारे में अधिक जानना चाहते हैं, तो कृपया man 2 read से परामर्श लें।

असेंबली भाषा में एक पूर्णांक पार्स करना इतना आसान नहीं है, हालांकि। चूंकि read केवल मानक इनपुट पर दिखाई देने वाले सादे बाइनरी डेटा देता है, तो आपको पूर्णांक मान को स्वयं परिवर्तित करने की आवश्यकता होती है। ध्यान रखें कि कीबोर्ड पर जो भी आप टाइप करते हैं उसे एएससीआईआई कोड के रूप में एप्लिकेशन में भेजा जाता है (या कोई अन्य एन्कोडिंग जिसका आप उपयोग कर सकते हैं - मैं यहां ASCII मान रहा हूं)। इसलिए, आपको डेटा को ASCII-एन्कोडेड दशमलव से बाइनरी में कनवर्ट करने की आवश्यकता है।

एक सामान्य अहस्ताक्षरित int करने के लिए इस तरह की संरचना परिवर्तित कुछ इस तरह दिखाई देगा सी में एक समारोह:

unsigned int parse_ascii_decimal(char *str,unsigned int strlen) 
{ 
    unsigned int ret = 0, mul = 1; 
    int i = strlen-1; 
    while(i >= 0) 
    { 
     ret += (str[i] & 0xf) * mul; 
     mul *= 10; 
     --i; 
    } 
    return ret; 
} 

विधानसभा को यह परिवर्तित (और हस्ताक्षर किए संख्या का समर्थन करने के लिए प्रदान) के लिए एक व्यायाम के रूप में छोड़ दिया जाता है पाठक। :)

अंतिम लेकिन कम से कम नहीं - write syscall के लिए आपको किसी दिए गए फ़ाइल डिस्क्रिप्टर को लिखे जाने वाले डेटा के साथ हमेशा एक बफर में एक पॉइंटर पास करने की आवश्यकता होती है। इसलिए, यदि आप एक नई लाइन आउटपुट करना चाहते हैं, तो न्यूलाइन अनुक्रम वाले बफर बनाने के अलावा कोई अन्य तरीका नहीं है।

+0

आप कमाल हैं! धन्यवाद! और हाँ, मैं NASM का उपयोग कर रहा हूँ। तो एक बफर आवंटित करने के बजाय मैं सीधे ढेर पढ़ने के लिए पढ़ सकते हैं? जैसा कि mov आरएसआई कहते हैं, [आरएसपी +8]? मैं अभ्यास बीटीडब्ल्यू पर काम करूंगा ;-) –

+0

'mov rsi, [rsp + 8] 'वास्तविक सामग्री को स्टैक से रजिस्टर में ले जायेगा। आप जो चाहते हैं वह पता है, जिस स्थिति में 'ली आरएसआई, [आरएसपी +8] 'ठीक काम करेगा। और हां, आप अपनी सभी पढ़ने/लेखन आवश्यकताओं के लिए भी ढेर का उपयोग कर सकते हैं। –

संबंधित मुद्दे