簡體   English   中英

x86-64位匯編Linux輸入

[英]x86-64 Bit Assembly Linux Input

我正在嘗試輸入我的程序...它所做的全部工作並在屏幕上打印一個“ 0”。 我很確定PRINTDECI函數可以工作,我前一陣子就可以了。 我是否只需要遍歷輸入代碼並僅在輸入特定值時退出? 我不確定該怎么做...除非是通過ACSII值吸收的。...無論如何,這是我的代碼(Yasm(nasm克隆),Intel語法):

GLOBAL _start
SECTION .text

PRINTDECI:
 LEA R9,[NUMBER + 18]       ; last character of buffer
 MOV R10,R9                 ; copy the last character address
 MOV RBX,10                 ; base10 divisor

 DIV_BY_10:

 XOR RDX,RDX                ; zero rdx for div
 DIV RBX                    ; rax:rdx = rax / rbx
 ADD RDX,0x30               ; convert binary digit to ascii
 TEST RAX,RAX               ; if rax == 0 exit DIV_BY_10
 JZ CHECK_BUFFER
 MOV byte [R9],DL           ; save remainder
 SUB R9,1                   ; decrement the buffer address
 JMP DIV_BY_10

 CHECK_BUFFER:

 MOV byte [R9],DL
 SUB R9,1

 CMP R9,R10                 ; if the buffer has data print it
 JNE PRINT_BUFFER 
 MOV byte [R9],'0'          ; place the default zero into the empty buffer
 SUB R9,1

 PRINT_BUFFER:

 ADD R9,1                   ; address of last digit saved to buffer
 SUB R10,R9                 ; end address minus start address
 ADD R10,1                  ; R10 = length of number
 MOV RAX,1                  ; NR_write
 MOV RDI,1                  ; stdout
 MOV RSI,R9                 ; number buffer address
 MOV RDX,R10                ; string length
 SYSCALL
RET

_start:
 MOV RCX, SCORE     ;Input into Score
 MOV RDX, SCORELEN
 MOV RAX, 3
 MOV RBX, 0
 SYSCALL

 MOV RAX, [SCORE]
 PUSH RAX           ;Print Score
  CALL PRINTDECI
 POP RAX

 MOV RAX,60         ;Kill the Code
 MOV RDI,0
 SYSCALL

SECTION .bss
 SCORE:         RESQ 1
 SCORELEN EQU $-SCORE

謝謝你的幫助! -凱爾

附帶說明一下,RCX中的指針根據DDD指向了一個非常大的數字……所以我想我必須讓它暫停並等待我鍵入,但是我不知道該怎么做。 ..

在x86_64系統上調用syscall 0 (READ)的“設置”為:

@xenon:~$ syscalls_lookup read
read:
        rax = 0  (0x0)
        rdi = unsigned int fd
        rsi = char *buf
        rdx = size_t count

因此,您的_start代碼應類似於:

_start:
    mov  rax, 0         ; READ
    mov  rdi, 0         ; stdin
    mov  rsi, SCORE     ; buffer
    mov  rdx, SCORELEN  ; length
    syscall

x86_64的寄存器約定和系統調用號與i386 完全不同。

您似乎遇到的一些概念性問題:

  • READ不會對您鍵入的內容進行任何解釋,您似乎希望它可以鍵入數字(例如57),並使其返回值57。不。 它將返回“ 5”,“ 7”,“ ENTER”,“垃圾” ...您的SCORELEN可能為8(resq 1的長度),因此您將以8個字節的速度讀取。 或字符,如果您想稱呼他們。 而且,除非您鍵入EOF字符(^ D),否則您將需要鍵入這8個字符,然后READ調用才能返回您的代碼。

  • 您必須將接收到的字符轉換為值...您可以通過簡單的方法進行操作,並與C庫中的ATOI()鏈接,或者編寫自己的解析器以通過加法和乘法將字符轉換為值(不難,請參見下面的代碼)。

在下面用作參考:

@xenon:~$ syscalls_lookup write
write:
        rax = 1  (0x1)
        rdi = unsigned int fd
        rsi = const char *buf
        rdx = size_t count

嗯...。這么多...我將重寫位:

    global _start
    section .text

PRINTDECI:
; input is in RAX
    lea  r9, [NUMBER + NUMBERLEN - 1 ]  ; + space for \n
    mov  r10, r9            ; save end position for later
    mov  [r9], '\n'         ; store \n at end
    dec  r9
    mov  rbx, 10            ; base10 divisor

DIV_BY_10:
    xor  rdx, rdx       ; zero rdx for div
    div  rbx            : rax = rdx:rax / rbx, rdx = remainder
    or   dl, 0x30       ; make REMAINDER a digit
    mov  [r9], dl
    dec  r9
    or   rax, rax
    jnz  DIV_BY_10

PRINT_BUFFER:
    sub  r10, r9        ; get length (r10 - r9)
    inc  r9             ; make r9 point to initial character
    mov  rax, 1         ; WRITE (1)
    mov  rdi, 1         ; stdout
    mov  rsi, r9        ; first character in buffer
    mov  rdx, r10       ; length
    syscall
    ret

MAKEVALUE:
; RAX points to buffer
    mov  r9, rax        ; save pointer
    xor  rcx, rcx       ; zero value storage

MAKELOOP:
    mov  al, [r9]       ; get a character
    or   al, al         ; set flags
    jz   MAKEDONE       ; zero byte? we're done!
    and  rax, 0x0f      ; strip off high nybble and zero rest of RAX (we're lazy!)
    add  rcx, rcx       ; value = value * 2
    mov  rdx, rcx       ; save it
    add  rcx, rcx       ; value = value * 4
    add  rcx, rcx       ; value = value * 8
    add  rcx, rdx       ; value = value * 8 + value * 2 (== value * 10)
    add  rcx, rax       ; add new digit
    jmp  MAKELOOP       ; do it again

MAKEDONE:
    mov  rax, rcx       ; put value in RAX to return
    ret

_start:
    mov  rax, 0         ; READ (0)
    mov  rdi, 0         ; stdin
    mov  rsi, SCORE     ; buffer
    mov  rdx, SCORELEN  ; length
    syscall

; RAX contains HOW MANY CHARS we read!
; -OR-, -1 to indicate error, really
; should check for that, but that's for
; you to do later... right? (if RAX==-1,
; you'll get a segfault, just so you know!)

    add  rax, SCORE     ; get position of last byte
    movb [rax], 0       ; force a terminator at end

    mov  rax, SCORE     ; point to beginning of buffer
    call MAKEVALUE      ; convert from ASCII to a value

; RAX now should have the VALUE of the string of characters
; we input above. (well, hopefully, right?)

    mov  [VALUE], rax   ; store it, because we can!

; it's stored... pretend it's later... we need value of VALUE!

    mov  rax, [VALUE]   ; get the VALUE
    call PRINTDECI      ; convert and display value

; all done!
    mov  rax, 60        ; EXIT (60/0x3C)
    mov  rdi, 0         ; exit code = 0
    syscall

    section .bss
SCORE:  resb 11   ; 10 chars + zero terminator
SCORELEN equ $-SCORE
NUMBER: resb 19   ; 18 chars + CR terminator
NUMBERLEN equ $-NUMBER

我要說這應該是第一次工作,對我來說這是現成的,尚未經過測試,但這應該很好。 我們最多讀取10個字符,將其終止為零,轉換為值,然后轉換為ASCII並將其寫出。

更准確地說,您應該將寄存器保存到每個子例程中的堆棧中,好吧,某些寄存器,確實如此,只有在您要與庫進行接口連接時...自己做事才能讓您擁有所有想玩的自由使用收銀機,您只需要記住放置在哪里!

是的,有人會說:“為什么不乘10而不是奇怪地加?” ...呃...因為它在寄存器上更容易,而且我不必在rdx:rax中全部設置。 此外,它是可讀性和可理解性的,尤其是注釋。 滾吧! 這不是比賽,是學習!

機器碼很有趣! 雖然要弄亂所有雞蛋,但是這里的編譯器無濟於事!

從技術上講,您應該檢查READ和WRITE的系統調用的返回結果(RAX),適當地處理錯誤,yadda yadda yadda...。學習使用調試器(gdb或其他工具)。

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM