Advertisement
Ham62

circle.asm

Oct 20th, 2018
475
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;cpu 8086   ; no shl ax, n :((
  2. cpu 186
  3. org 100h
  4.  
  5. %define _START_ANGLE   (32 - 1)
  6.  
  7. %define _HALF_ROTATION  32  ; These costants are doubled to account for
  8. %define _FULL_ROTATION  64  ; WORD adjustment with pointers
  9. %define _QUART_ROTATION 16  
  10.  
  11. %define _SCREEN_WIDTH 320
  12. %define _X_CENTER 160
  13. %define _Y_CENTER 100
  14.  
  15. %define _RADIUS 75
  16.  
  17.  
  18. start:
  19.     MOV AX, 0013h ; Screen 13h
  20.     INT 10h
  21.  
  22.     MOV CX, _START_ANGLE
  23.  
  24. NextAngle:
  25.     MOV DI, cosTable ; Set DI to start of cos table
  26.  
  27.     MOV AX, CX       ; Set AX to current angle
  28.     SHL AX, 1        ; angle*2 because table is WORDs
  29.  
  30. CalcCos:
  31.    ADD DI, AX
  32.  
  33.    MOV AX, _RADIUS   ; Set AX to radius of circle
  34.    MOV DX, [DI]      ; read cos(x) into DX
  35.  
  36.    IMUL DX           ; x-component = (radius*cos(x)) >> 6
  37.    SAR AX, 6         ; Need to preserve the sign
  38.  
  39.    MOV BX, AX        ; Store x-component in BX
  40.  
  41. SetSinPtr:
  42.    ADD DI, _QUART_ROTATION ; sin(x) = cos(PI/2 + x)
  43.  
  44.    ; Make sure we don't overrun the table
  45.    CMP DI, cosTable+(_FULL_ROTATION-1)
  46.    JG .FixOverrun
  47.    JMP CalcSin
  48.  
  49. .FixOverrun:
  50.    SUB DI, _FULL_ROTATION ; Adjust angle so it's within table
  51.  
  52. CalcSin:
  53.    MOV AX, _RADIUS  ; Set AX to radius of circle
  54.    MOV DX, [DI]     ; Read sin(x) into DX
  55.  
  56.    IMUL DX          ; y-component = (radius*sin(x)) >> 6
  57.    SHR AX, 6
  58.  
  59.  
  60. DrawPoint:
  61.     MOV DX, 0A000h  ; Screen is ES:SI
  62.     MOV ES, DX
  63.  
  64.     ADD AX, _Y_CENTER ; y-coord = (y_center + y-component)
  65.     ADD BX, _X_CENTER ; x-coord = (x_center + x-component)
  66.  
  67.     MOV SI, AX        ; SI = AX*256 + AX*64 [30 cycles]
  68.     SHL AX, 6         ; AX*64
  69.     SHL SI, 8         ; AX*256
  70.     ADD SI, AX        ; Store result (SI = Y*320)
  71.     ADD SI, BX        ; X + (Y*320)
  72.  
  73.     ;MOV DX, 320       ; ScreenPtr = X + (Y*320) [83 cycles]
  74.     ;MUL DX            ; (Y*320)
  75.     ;ADD AX, BX        ; X + (Y*320)
  76.     ;MOV SI, AX        ; used for mul method
  77.  
  78.     MOV [ES:SI], BYTE 0Ah
  79.  
  80.     DEC CX            ; Done point, decrement counter
  81.     JL end            ; If counter less than 0, goto quit
  82.     JMP NextAngle     ; Not at cos(0), draw next point
  83.  
  84.  
  85. end:
  86.     ret
  87.  
  88. cosTable: incbin "cosTable.bin"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement