我正在尋找示例如何使用協處理器計算正弦。 我發現功能:彙編計算正弦例子。 nasm 16 dos
CalcSin
fld long [angle] ; st(0) = angle
fsin ; st(0) = sin(angle) (angle is in radians)
fstp long [SinX] ; SinX = sin(angle)
我想畫正弦和我需要的Y ax
和X在bx
。 X將是沒有問題的,因爲我會做循環,但是對於Y我有一個問題。 X將從示例0到350(如像素)。 如果例如sin(30度)是1/2,如何計算它並使像素爲Y. 如何將結果放在一起以獲得良好的座標?
編輯: 我很抱歉,但我當我運行代碼,它讓我沒有竇,但2號線。我不知道我做錯了什麼,現在
segment .data
segment .code
..start:
mov ax, 13h
int 10h ; switch to 320x200 mode
mov ax, 0a000h ; The offset to video memory
mov es, ax ; We load it to ES through AX,
; because immediate operation
; is not allowed on ES
;;;;;;;;;;;;;;;;;;;;;;
DrawWave:
mov ebx, y ; EBX = &y
mov ecx, 0
; let st(1) = 2*PI/640
fldpi ; st(0) = PI
fidiv dword [step] ; st(0)/160 = 0.009817...
fldz ; st(0) = 0.0, st(1) = 0.00045...
.loop:
fld st0 ; duplicate the x on the top
fsin ; st(0) = sin x
fimul dword [imgHeight] ; st(0) = y*240
fiadd dword [imgHeight] ; eliminate negative coordinate by translating the wave vertically
fistp dword [y] ; store y to ´y´
fadd st0, st1 ; add the step value to x, doing the step
;draw pixel at [*EAX:ECX]
push ax
push bx
push cx
call DrawPixel
pop cx
pop bx
pop ax
inc ecx
cmp ecx, 320 ; perform 640 steps to draw a single sine wave
jl .loop
fstp st0 ;clean up
fstp st0 ;clean up
ret
;;;;;;;;;;;;;;;;;;;;;;;;;
xor ah, ah
int 16h ; keyboard (wait for key)
mov ax, 3
int 10h ; go to text mode
mov ax, 4c00h
int 21h ; return to DOS, exit code 0
;;;;;;;;;;;;;;;;;;;;;
; EBX = in &CoordY
; ECX = CoordX
;DrawPixel:
; draw a pixel at [*EBX:ECX]
; ret
DrawPixel:
push dx ; mul changes dx too
mov ax, cx ; ax is X coord copy from cx
mov cx, 320
mul cx ; multiply Y (ax) by 320 (one row)
add ax, bx ; and add X (bx) (result= dx:ax)
mov di, ax
pop dx
mov dl, 4
mov [es:di], dl ; store color/pixel
ret
;CONSTANTS:
step: dw 160 ; 2/320 = 160
imgWidth: dw 320 ; 320px
imgHeight: dw 200/2 ; 200px on half, because Y also gets negative
;VARIABLES:
x: dw 0 ; a tmp place to save X
y: dw 0 ; a tmp place to save Y
'我要畫正弦' - 你是什麼意思?正弦波? – user35443
是的。這將是後來的正弦波動畫。但我想從正弦波 – kkkkk
開始我正在研究我的答案。 – user35443