如何使用nasm在Mac OS X x64上制作装配体数组?

时间:2014-09-04 14:53:53

标签: arrays macos assembly 64-bit nasm

我需要在Mac OS X上使用汇编语言在数组中编写数字,但我找不到合适的方法,因为大多数信息都是关于Windows 64位程序的。 有没有一种正确的方法来使用x64汇编?

我试着这样做,但这样的方式不是我需要的。我需要数字,而不是ASCII字符来显示。

global start


section .text

start:
xor     rax, rax
mov     rax, 0x2000004 ; write
mov     rdi, 2 ; stdout
mov     rsi, num1
mov     rdx, 1
syscall

mov     rax, 0x2000004 ; write
mov     rsi, num2
mov     rdx, 1
syscall

mov     rax, 0x2000004 ; write
mov     rsi, num3
mov     rdx, 1
syscall

mov     rax, 0x2000001 ; exit
mov     rdi, 0
syscall


section .data

num1:    db      "5"
num2:    db      "6"
num3:    db      "7"

1 个答案:

答案 0 :(得分:0)

好的,所以我找到了一种如何以相反顺序编写字符数组的方法。

global start


section .text

start:
;---------------------------------------------------
;                   WRITE_STDOUT_SOURCE
;---------------------------------------------------

mov     rbx, source_length  ; rbx will store the remaining length to write of source array
mov     rsi, qword source   ; rsi now point to the beginning (1st element) of source array 
call    write_data          ; calling procedure, that will write down the whole array
;---------------------------------------------------
;                   WRITE_STDOUT_SOURCE
;---------------------------------------------------



;---------------------------------------------------
;                   WRITE_STDOUT_UNINITITALISED_DEST
;---------------------------------------------------
mov     rbx, dest_length  ; rbx  - remaining length of uninitialised array
mov     rsi, qword dest   ; rsi  points to the beginning (1st element dest.array)
call    write_data        ; calling procedure, that will write down the whole array
;---------------------------------------------------
;                   WRITE_STDOUT_UNINITITALISED_DEST
;---------------------------------------------------


;---------------------------------------------------
;                   REWRITE_ARRAY
;---------------------------------------------------

mov     al, [qword source+3]       ; al <- fourth element of source array
mov     [qword dest], al           ; dest[0] <- al = fourth element of source array
mov     al, [qword source+2]       ; al <- third element of source array
mov     [qword dest+1], al         ; dest[1] <- al = third element of source array
mov     al, [qword source+1]       ; al <- second element of source array
mov     [qword dest+2], al         ; dest[2] <- al = second element of source array
mov     al, [qword source]         ; al <- first element of source array
mov     [qword dest+3], al         ; dest[3] <- al = first element of source array
;---------------------------------------------------
;                   REWRITE_ARRAY
;---------------------------------------------------



;---------------------------------------------------
;                   WRITE_STDOUT_DEST
;---------------------------------------------------
mov     rbx, dest_length  ; rbx  - remaining length to write of array
mov     rsi, qword dest   ; rsi  1st element of dest array
call    write_data        ; calling procedure, that will write down the whole array
;---------------------------------------------------
;                   WRITE_STDOUT_DEST
;---------------------------------------------------


;---------------------------------------------------
;                   EXIT
;---------------------------------------------------
mov     rax, 0x2000001 ; exit
mov     rdi, 0         ; exit code 0 
syscall                ; 

;---------------------------------------------------
;                   EXIT
;---------------------------------------------------



;---------------------------------------------------
;             PROCEDURE THAT WRITES ARRAY
;---------------------------------------------------
write_data:
mov     rax, 0x2000004 ; write syscall (number of system call you can see here http://www.opensource.apple.com/source/xnu/xnu-1456.1.26/bsd/kern/syscalls.master, write is 4, but for apple you need to add 0x200000 in front of it
mov     rdi, 1 ; standart stream 1 = stdout 
mov     rdx, 1 ; message length ( we write chars one by one )
syscall        ; write 
inc     rsi    ; move to next element
dec     rbx    ; decrease remaining length of array to write
jnz     write_data ; until the remaining length equals zero, go to the write_data label
ret            ; because it's a procedure, we need to have a returning point from it
;---------------------------------------------------
;             PROCEDURE THAT WRITES ARRAY
;---------------------------------------------------

section .data

  source:    db      '5', '6'   , '7', '8'
  source_length:   equ  $ - source
  dest:        db      '0', '0', '0', '0'
  dest_length:  equ    $ - dest