佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1627|回复: 18

谁要.ASM源代码?

  [复制链接]
发表于 5-3-2011 01:56 AM | 显示全部楼层 |阅读模式
这是Pong游戏,两个人同时用键盘玩。
为了参加比赛,所以指令占用的BYTES越少越好。
  1. ;
  2. ;  PONG.ASM
  3. ;
  4. ;  Coded by Boo Khan Ming (Wu), from Malaysia
  5. ;  for Hugi Size Coding Compo 3 (August 1998)
  6. ;
  7. ;
  8. ;  Size Distribution                    178
  9. ;  哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪
  10. ;  Ball (clear+draw)                      8
  11. ;  Ball (move+reflect+check for end)     41
  12. ;  Bars (clear+draw)                     27
  13. ;  Bars (move+kb handler)                38
  14. ;  Misc (init+exit+delay)                46
  15. ;  Text                                  18
  16. ;
  17. ;
  18. ;  This is my first game written in Assembly language.
  19. ;  Please do not expect much from me.
  20. ;  Anyway, I've put great effort in finding the most suitable algorithm,
  21. ;  and this is being my third algorithm.
  22. ;
  23. ;  哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪
  24. ;
  25. ;  There are mainly two unique technique or algorithm employed here and
  26. ;  should be outlined to make you easier to follow the code.
  27. ;
  28. ;  For keystroke checking, the most essential part is the conversion
  29. ;  performed by AAA (or DAA) instruction.
  30. ;  The player side and the left or right keys are then determined from
  31. ;  bit 0 and bit 1 in accumulator (AL) respectively.
  32. ;
  33. ;  For ball movement, the horizontal and vertical edge detection are
  34. ;  determined from two counters respectively.
  35. ;  This is concluded from that the number of movement from left edge to
  36. ;  right edge and vice versa are equal. Similarly, the number of movement
  37. ;  from top edge to bottom edge and vice versa are equal too.
  38. ;  Therefore, when the counter are decremented to zero, an edge collision
  39. ;  should be encountered logically.
  40. ;
  41. ;
  42. ;  Contact e-mail: bookm@tm.net.my
  43. ;  Homepage URL  : http://www.geocities.com/SiliconValley/Horizon/3409/
  44.   

  45.         .MODEL tiny
  46.         .286

  47.         .DATA
  48.         Table           DW      0,0
  49.         Message         DB      "Player 2 has won.$"

  50.         .CODE
  51.         .STARTUP

  52.         mov     al, 03h                 ; set text mode        
  53.         int     10h
  54.         push    0b800h                  ; startup initialization
  55.         pop     es
  56.         cli
  57.         lea     si, Table               ; data table starting offset
  58.         mov     di, 324                 ; ball parameters
  59.         mov     bp, 320
  60.         mov     bl, 162
  61.         mov     cl, 77
  62.         mov     al, 22
  63.         
  64. Start:
  65.         pusha

  66.         mov     ax, 09dch               ; draw new ball
  67.         stosw

  68.         mov     cl, 75                  ; perform vertical retrace
  69.         mov     dx, 03dah               
  70. Delay:
  71.         in      al, dx
  72.         xor     al, cl
  73.         and     al, 8
  74.         jnz     Delay
  75.         loop    Delay

  76.         call    Draw                    ; clear old paddles

  77.         in      al, 60h                 ; check keystroke
  78.         test    al, 80h
  79.         jnz     Key2
  80.         cmp     al, 1                   ; (required) to set AF?
  81.         aaa
  82.         mov     cl, 10
  83.         mov     bl, al
  84.         and     bx, 0001h               ; determine player side
  85.         shl     bl, 1
  86.         test    al, 02h                 ; determine left or right keys
  87.         jnz     Key1
  88.         neg     cl
  89. Key1:
  90.         mov     ax, [si+bx]             ; move paddle
  91.         add     al, cl
  92.         cmp     al, 90h                 ; left and right edge check
  93.         jae     Key2
  94.         mov     [si+bx], ax
  95. Key2:
  96.         mov     ax, 0adbh               ; draw new paddles
  97.         call    Draw

  98.         popa

  99.         stosw                           ; clear old ball

  100.         dec     di                      ; move ball
  101.         dec     di
  102.         add     di, bx
  103.         dec     cx                      ; horizontal edges check
  104.         jnz     Move1
  105.         neg     bx                      ; horizontal reflection
  106.         add     bx, bp
  107.         mov     cl, 79
  108. Move1:
  109.         dec     ax                      ; vertical edges check
  110.         jnz     Move3
  111. Move2:
  112.         neg     bp                      ; vertical reflection
  113.         add     bx, bp
  114.         mov     al, 0dbh                ; vertical collision check
  115.         scasb
  116.         jnz     Stop
  117.         mov     al, 22
  118.         dec     di
  119.         add     di, bp                  ; vertical reflection correction
  120. Move3:
  121.         jmp     SHORT Start

  122. Stop:
  123.         shr     di, 11                  ; update player ID
  124.         sub     [si+11], di
  125.         mov     al, 03h                 ; reset text mode
  126.         int     10h
  127.         mov     ah, 09h                 ; display message
  128.         lea     dx, Message
  129.         int     21h
  130.         ret
  131. Draw:
  132.         mov     cl, 10                  ; draw paddles (procedure)
  133.         push    cx
  134.         mov     di, [si+2]
  135.         rep     stosw
  136.         pop     cx
  137.         mov     di, [si]
  138.         add     di, 0f00h
  139.         rep     stosw
  140.         ret

  141.         END
复制代码
回复

使用道具 举报


ADVERTISEMENT

发表于 7-3-2011 02:17 AM | 显示全部楼层
厉害下 , 一直想学asm 不过总是感觉很难
回复

使用道具 举报

发表于 7-3-2011 01:07 PM | 显示全部楼层
厉害下 , 一直想学asm 不过总是感觉很难
A-B-C. 发表于 7-3-2011 02:17 AM



    樓主我現在忘記的七七八八了。

據説Visual Studio .NET也有Assembly Language呢,A-B-C.您不妨一試?
回复

使用道具 举报

发表于 8-3-2011 12:27 AM | 显示全部楼层
樓主我現在忘記的七七八八了。

據説Visual Studio .NET也有Assembly Language呢,A-B-C.您不妨 ...
FlierMate.. 发表于 7-3-2011 01:07 PM



    哈哈 , 我也想啊 不过还是专注与C++比较好
回复

使用道具 举报

发表于 8-3-2011 01:18 AM | 显示全部楼层
哈哈 , 我也想啊 不过还是专注与C++比较好
A-B-C. 发表于 8-3-2011 12:27 AM



    C++已經很不錯了。
回复

使用道具 举报

发表于 8-3-2011 01:21 AM | 显示全部楼层
C++已經很不錯了。
FlierMate.. 发表于 8-3-2011 01:18 AM



    恩 , 不过我还是比较喜欢ASM , 看了王爽的汇编教程 , 不过一直都好像懂一点又不懂一点酱
回复

使用道具 举报

Follow Us
发表于 8-3-2011 02:02 AM | 显示全部楼层
恩 , 不过我还是比较喜欢ASM , 看了王爽的汇编教程 , 不过一直都好像懂一点又不懂一点酱
A-B-C. 发表于 8-3-2011 01:21 AM



    很容易的啦,過你幾招:先認識Register:AX,BX,CX....

AX=16bit,EAX=32bit(?) AX=AH(8bit)+AL(8bit)

SET A=1                   MOV AX,1
A+=1                       ADD AX,1  或 INC AX
SET A=0                   XOR AX,AX 或 SUB AX,AX (MOV AX,0好像比較慢,沒有人這樣用)

IF A=3 THEN GOTO R:       CMP AX,3
                                      JNZ/JZ R: (我忘了AX=3是ZERO FLAG還是AX=3等於NOT ZERO FLAG)

大概這樣,接下來就是MEMORY部分,有CS,DS。。。都是一pair一pair的,也就是segment, offset。
比如CS是Code Segment,IP(Offset)是Intruction Pointer等等,然後就可以refer去其他Memory區域

記得。COM程序大小不能大過64KB,(現在還是這樣嗎?)  還有可以cast, 比如 MOVE AX,WORD PTR[BL](不太肯定,大概這樣)

還是你看那個筆者的帖比較好。

Microsoft好像用C++開發Windows的哦!C++程序員都很吃香。
回复

使用道具 举报

发表于 8-3-2011 02:50 AM | 显示全部楼层
很容易的啦,過你幾招:先認識Register:AX,BX,CX....

AX=16bit,EAX=32bit(?) AX=AH(8bit)+AL( ...
FlierMate.. 发表于 8-3-2011 02:02 AM



    你这样写比较容易明白了 , 因为有时候我理解了add , mov 但是实际运用的时候我就忘了应该用什么命令了
回复

使用道具 举报


ADVERTISEMENT

发表于 8-3-2011 02:47 PM | 显示全部楼层
你这样写比较容易明白了 , 因为有时候我理解了add , mov 但是实际运用的时候我就忘了应该用什么命 ...
A-B-C. 发表于 8-3-2011 02:50 AM



    誇獎了,我比你差,ASM只是咖喱菲,我真正的職業(前幾年)是Visual Basic 6,離C++的地位有天淵之別。

還有那些SHR, SHL, AND, OR, XOR都是High-Level Language都有用到的,所以別怕ASM!
回复

使用道具 举报

发表于 8-3-2011 07:29 PM | 显示全部楼层
我只会.....AX,3 INT 10H....
回复

使用道具 举报

发表于 8-3-2011 09:00 PM | 显示全部楼层
我只会.....AX,3 INT 10H....
aquamax 发表于 8-3-2011 07:29 PM



    哦,查了查,是80x25 Text Mode吧,我很多程序都在Vista跑不到,因爲這個需要Full Screen!!!而被Vista中止了。
回复

使用道具 举报

发表于 11-3-2011 03:11 PM | 显示全部楼层
是的....我还跑XP呢....
回复

使用道具 举报

发表于 11-3-2011 03:56 PM | 显示全部楼层
我只会.....AX,3 INT 10H....
aquamax 发表于 8-3-2011 07:29 PM


弱弱的说.你忘记 MOV AX ,3
INT 10H.


回复

使用道具 举报

发表于 18-3-2011 07:00 AM | 显示全部楼层
基本上这里头包含了最基本的读key,在垂直扫描返回周期更新画面的部分。。。
是个不错的查考样本,感谢分享。。。
回复

使用道具 举报

发表于 19-3-2011 11:02 AM | 显示全部楼层
忽然想起大学时的assembly programming lab test,
几乎每个星期班上的学生都全军覆没...
回复

使用道具 举报

发表于 19-3-2011 10:11 PM | 显示全部楼层
基本上这里头包含了最基本的读key,在垂直扫描返回周期更新画面的部分。。。
是个不错的查考样本,感谢分享 ...
astral 发表于 18-3-2011 07:00 AM



    谢谢你。说不定你比我更懂汇编/组合语言呢!
回复

使用道具 举报


ADVERTISEMENT

发表于 19-3-2011 10:12 PM | 显示全部楼层
忽然想起大学时的assembly programming lab test,
几乎每个星期班上的学生都全军覆没...
AdventChildren 发表于 19-3-2011 11:02 AM



    是电脑工程科系吗?我记得Assembly Language是唯一教到的‘软件’部分。
回复

使用道具 举报

发表于 21-3-2011 12:17 AM | 显示全部楼层
是电脑工程科系吗?我记得Assembly Language是唯一教到的‘软件’部分。
FlierMate__ 发表于 19-3-2011 10:12 PM

电脑科学的其中一个硬件课,lecturer也是借机会让大家见识见识,没有很认真教...
回复

使用道具 举报

发表于 27-3-2011 12:14 PM | 显示全部楼层
忽然想起大学时的assembly programming lab test,
几乎每个星期班上的学生都全军覆没...
AdventChildren 发表于 2011-3-19 11:02

我是覆没到最后的,幸好retake过关。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 17-11-2025 12:44 AM , Processed in 0.131795 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表