2014-09-04 60 views
0

我需要在Mac OS X上用匯編語言編寫數字,但是我找不到正確的方法,因爲大多數信息都是關於Windows 64位程序的。 有沒有一種正確的方法來做到這一點與x64大會?如何使用nasm在Mac OS X 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

這裏是着名的問題 - 你有什麼試過? – 2014-09-04 16:21:04

+1

@ l'L'l這是我寫的代碼。 – cre8eve 2014-09-04 16:33:53

回答

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