Linux NASM x86 64位代码显示空输出

时间:2016-04-15 17:50:57

标签: linux assembly nasm x86-64 x87

在linux上运行的我的nasbit 64位代码试图使用horners方法在21x41网格上打印出余弦波。但是,每当我打印所有我得到的是一个空的21x41网格。任何人都可以帮我解决这个问题吗?我的代码如下:

        SECTION .data   ; Data section, initialized variables
nrow:       dq  21  ; 21 rows
ncol:       dq  41  ; 41 columns
fmtc:       db  "%c", 0 ; print one character at a time
fmtend:     db  10, 0   ; end a line
fmtdbg:     db  "i=%ld, j=%ld, k=%ld", 10, 0
star:       db  '*' ; one character '*'
len:        equ     $-star
spc:        db  ' '
af:     dq  1.0, 0.0, -0.5 ; coefficients of polynomial, a_0 first
        dq  0.0, 0.041667, 0.0, -0.001389, 0.0, 0.000025
XF:     dq  0.0 ; computed
Y:      dq  0.0 ; computed
N:      dq  8   ; power of polynomial
X0:     dq  -3.14159 ; start XF
DX0:        dq  0.15708  ; increment for XF  ncol-1  times
one:        dq      1.0
ten:        dq 10.0
none:       dq -1.0
nten:       dq      -10.0
twenty:     dq      20.0
zero:       dq  0.0
newline:    db 10
section .bss
a2:     resb    21*41   ; two dimensional array of bytes
i:      resq    1   ; row subscript
j:      resq    1   ; col subscript
k:      resq    1

        SECTION .text   ; Code section.

        global _start   ; the standard gcc entry point
_start:             ; the program label for the entry point


;;  clear a2 to space
mov     rax,0       ; i=0
mov [i],rax

loopi:
    mov     rax,[i]
    mov     rbx,0       ; j=0
    mov [j],rbx
loopj:
    mov rax,[i]
    mov rbx,[j]
    imul    rax,[ncol]  ; i*ncol
    add     rax, rbx    ; i*ncol + j
    mov     dl, [spc]   ; need just character, byte
    mov     [a2+rax],dl ; store space

    mov rbx,[j]
    inc     rbx     ; j++
    mov [j],rbx
    cmp     rbx,[ncol]      ; j<ncol
    jne     loopj

    mov rax,[i]
    inc     rax     ; i++
    mov [i],rax
    cmp rax,[nrow]  ; i<ncol
    jne     loopi
    ;;  end clear a2 to space

    mov rax, 0      ;i = 0
    mov [i], rax
    mov rbx, 0      ;j = 0
    mov [j], rbx
cos:
    mov rcx,[N]     ; loop iteration count initialization, n
    fld qword [af+8*rcx] ; accumulate value here, get coefficient a_
h5loop:
    fmul    qword [XF]  ; * XF
    fadd    qword [af+8*rcx-8] ; + aa_n-i
    loop    h5loop         ; decrement rcx, jump on non zero
    fstp    qword [Y]      ; store Y


    ;;; ;  compute k
    fld qword [Y]
    fadd qword [one]
    fmul qword [ten]
    fmul qword [none]
    fadd qword [twenty]
    fistp qword [k]

    ;;; ; ; ; rax gets k * ncol + j
    mov rax, [k]
    mov rbx, [j]
    imul rax, [ncol]
    add rax, rbx

;;; ; put "*" in dl, then dl into [a2+rax]
    mov dl, [star]
    mov [a2+rax], dl

;;; ; XF = XF + DX0
    fld qword [XF]
    fadd qword [DX0]
    fistp qword [XF]


    mov rbx, [j]
    inc     rbx     ; j++

    mov [j], rbx
    cmp     rbx,[ncol]  ; j<ncol
    jl     cos

    ;;  print
    mov     rax,0       ; i=0
    mov [i],rax
ploopi:
    mov rax,[i]
    mov     rbx,0       ; j=0
    mov [j],rbx
ploopj:

    mov rax,[i]
    mov rbx,[j]


    ;add rax, a2

    mov dl, [spc]
    ;;  compute double subscript

    imul    rax,[ncol]
    add     rax, rbx

    ;; mov rdi, fmtc
    mov rsi, [a2+rax]
    mov rax, 1
    mov     rdi, 1          ; file handle 1 is stdout
    mov     rsi, [a2+rax]       ;or star for 21x41 stars
    mov rdx, 1      ; number of bytes
    syscall     
    ;; print here

    mov rbx,[j]
    inc     rbx     ; j++
    mov [j],rbx
    cmp     rbx,[ncol]      ; j<ncol
    jne     ploopj

    mov     rdi, fmtend
    mov rax, 1
    mov     rdi, 1          ; file handle 1 is stdout
    mov     rsi, newline        ; address of string to output
    mov rdx, 1          ; number of bytes
    syscall     
    ;; print here

    mov rax,[i]
    inc     rax     ; i++
    mov [i],rax
    cmp rax,[nrow]  ; i<ncol
    jne     ploopi
    ;;  print a2



    mov     eax, 60         ; system call 60 is exit
    xor     rdi, rdi        ; exit code 0
    syscall             ; invoke operating system to exit

很抱歉,如果我的代码很乱。我是汇编语言的新手。谢谢你的帮助:)

0 个答案:

没有答案