装配程序中的随机输出。使用系统调用OSX

时间:2018-03-30 00:16:17

标签: macos assembly 64-bit nasm

我四处寻找,但我没有找到任何东西。

代码如下:

  hello_world     db      "Hello World!",10
  .hellolen       equ     $ - hello_world

  get_input       db      "Enter a number: ",10
  .getlen         equ     $ - get_input

  input           times  10 db 0
  .len            equ     $ - input


section .text
global start

; Arguments go in the registers 
;RDI, RSI, RDX, R10, R8, and R9, in order. 
;System call number goes into RAX.

start:
call printHello
call askInput
call takeInput
call printInput
call exit
ret

printHello:
mov rax, 0x2000004
mov rdi, 1
mov rsi, hello_world
mov rdx, hello_world.hellolen
syscall

askInput:
mov rax, 0x2000004               ; System call write = 4                
mov rdi, 1                       ; Write to standard out = 1
mov rsi, get_input               ; The address of enter string
mov rdx, get_input.getlen                      ; The size to write
syscall   

takeInput:
mov rax, 0x2000003               ; System call read = 3
mov rdi, 2                       ;
mov rsi, input                   ; buffer that we are reading to
mov rdx, input.len               ;to create buffer overflow
                                 ;we can input more than
                                 ;input.len
syscall

printInput:
mov rax, 0x2000004               ; System call write = 4                
mov rdi, 1                       ; Write to standard out = 1
mov rsi, input                   ; The address of enter string
mov rdx, input.len                      ; The size to write
syscall 

exit:
mov rax, 0x2000001      ; System call number for exit = 1
mov rdi, 0              ; Exit success = 0
syscall                 ; Invoke the kernel

输出:

Hello World!
Enter a number:
r:                 <- weird random r:
test               <- input by me
test               <- correct value returned

有没有办法用GDB查看这个,以便我可以找出导致这个问题的原因?我不确定如何从故障排除的角度来处理这个问题。谢谢!

我根据评论编辑了代码,但我仍然在输出中得到相同的奇数r:

0 个答案:

没有答案