Implementing MicroThread / Coroutine via esp-‐swap for Danmaku Shooting Games
Yoh Okuno / @nokuno
#x86opti
About Presenter • Name: Yoh Okuno / @nokuno
• R&D Engineer at Yahoo! Japan
• Interest: NLP (Natural Language Processing),
Machine Learning, Data Mining and TOUHOU
• Skills: C/C++, Java, Python, and Hadoop.
• Website: http://yoh.okuno.name/
Overview 1. Introduction to Danmaku Shooting Games
– Let’s play!
2. Using MicroThread / Coroutine
– Why it is useful for Danmaku Shooting Games
3. Implementation of MicroThread / Coroutine
– How to implement it with x86 inline assembly
1. Introduction to Danmaku Shooting Games
TOBIMARISA: Dojin Danmaku Shooting • Full-‐scratch C++ program with 20,000 lines
DEMO Video: http://www.youtube.com/watch?v=UBCnQJbrZxU
Source: https://github.com/nokuno/tbm
Structure of Danmaku Shooting Games • Nested sequences of actions
Game Start
Game End
Retry
Title
Stage 1
Stage 2
Stage 3
Ending
Enemies
Sub Boss
Enemies
Main Boss
Normal
Spell 1
Normal
Spell 2
Last Spell
Wait
Circle Shot
Move
Wait
Special Shot
Loop Until Beat or Tim
e up
2. Using MicroThread / Coroutine for Danmaku Shooting Games
How to Implement Danmaku Shooting Games? • Toy example: Enemy class and main loop
Main Loop (
!"FPS)
How to Implement Danmaku Shooting Games?
• How about nesting?
• Manually code?
• No!
– Too many states
– Too many counts
– Undebuggable
Handle nested sequences via MicroThread
Natural D
escription of Sequence
3. Implementation of MicroThread / Coroutine
Review of Memory Space in Program
• There are 3 types of fields in memory space
– Program: read-‐only field, store instructions
– Heap: dynamic field used by malloc/new
– Stack: read/write field to store local variables
• In x86, esp register stores the stack pointer
Stack
esp eip
0x100000c63 0x100100080 0x7fff5lfe808
Program Heap
MicroThread with x86 inline assembly
• Trick: allocate own space and swap esp with it
• The idea:
– Allocate own memory for alternative stack
– Swap esp (stack pointer) with it (m_esp)
– Call functional pointer when it starts
Program Stack
m_esp
0x100000c63 0x100100080 0x7fff5lfe808
MyStack Heap
esp eip
Components of MicroThread
• There are 2 files:
– microthread.h
– microthread.cc
• There are 2 main functions:
– MicroThread::SwitchThread()
– MicroThread::Start()
Main Variables
Main Functions
Header: microthread.h
MicroThread::SwitchThread() Source: microthread.cc
Reserve registers
Restore registers
Swap esp (stack pointer)
MicroThread::Start()
• Initialize
– Allocate a buffer for our own stack (default: 32KB)
– Set stack pointer to the end of buffer
Source: microthread.cc
Reserve registers
Restore registers
Swap esp and call CallBack
Restore esp and set flag
Source: microthread.cc
Other Functions
// use in enemies
// use in main loop
Conclusion • Developed Danmaku Shooting Game
– Structure: Nested Sequences of Actions
• Why MicroThread / Coroutine can contribute
– Make it simple, natural way
• How to Implement MicroThread / Coroutine
– Swap esp (stack pointer) with own space
Any Questions? https://github.com/nokuno/nokuno/tree/master/cc/x86