├── .gitignore ├── src ├── utils │ ├── #my_strlen.c# │ ├── convert_endian.asm │ ├── usage.asm │ ├── init_arena.asm │ ├── my_putstr.asm │ ├── my_strlen.asm │ ├── swap_int.asm │ ├── is_already_define.asm │ ├── my_strcmp.asm │ ├── check_place_arena.asm │ ├── int_to_str.asm │ ├── check_big_prog_number.asm │ ├── my_strcat.asm │ ├── my_putnbr.asm │ ├── attribute_address.asm │ ├── attribute_i_to_someone.asm │ ├── get_magic.asm │ ├── to_negativ.asm │ ├── check_same_prog_number.asm │ ├── attribute_one_def.asm │ ├── read_arena.asm │ ├── attribute_address_defined.asm │ ├── get_name.asm │ ├── write_arena_two.asm │ ├── get_comment.asm │ ├── get_size.asm │ ├── my_showmem.asm │ ├── attribute_two_def.asm │ ├── get_name_comment_champions.asm │ ├── attribute_prog_number.asm │ ├── check_args.asm │ └── write_arena_four.asm ├── functions │ ├── is_good_register.asm │ ├── is_register.asm │ ├── is_direct.asm │ ├── is_indirect.asm │ ├── get_size_param.asm │ ├── copy_reg.asm │ ├── my_zjmp.asm │ ├── get_second_value.asm │ ├── my_xor.asm │ ├── my_live.asm │ ├── my_and.asm │ ├── my_lld.asm │ ├── my_lldi.asm │ ├── my_or.asm │ ├── my_st.asm │ ├── my_ld.asm │ ├── my_sub.asm │ ├── my_add.asm │ ├── my_lfork.asm │ ├── my_ldi.asm │ ├── get_value.asm │ ├── my_fork.asm │ ├── get_first_value.asm │ └── my_sti.asm ├── init_reg.asm ├── gui │ ├── put_background.asm │ ├── get_list_pc.asm │ ├── hex_to_str.asm │ ├── get_color.asm │ ├── is_pc.asm │ ├── get_color_champions.asm │ ├── disp_players.asm │ ├── get_image_path.asm │ ├── events.asm │ ├── get_arena.asm │ ├── print_bytes.asm │ ├── load_players_name.asm │ ├── set_color_with_pc.asm │ ├── my_gui.asm │ ├── disp_arena.asm │ └── disp_info_players.asm ├── find_max_prog_number.asm ├── init_values_champions.asm ├── exec_instructions │ ├── search_who_still_alive.asm │ ├── is_special.asm │ ├── check_first_in_list.asm │ ├── winner.asm │ ├── get_cycle_to_wait.asm │ ├── exec_instructions.asm │ ├── exec_function.asm │ ├── check_live_process.asm │ ├── manage_instructions.asm │ ├── fulfil_dir.asm │ ├── fulfil_params.asm │ ├── get_type_and_param.asm │ └── get_instruction.asm ├── save_args.asm ├── desassembleur │ ├── desassemble_it.asm │ ├── print_args.asm │ ├── my_desassembler.asm │ └── print_instruction.asm ├── get_dump.asm ├── load_arena.asm ├── load_champions_in_arena.asm ├── main.asm ├── save_champion.asm ├── load_file_in_arena.asm ├── list_champions.asm └── arguments.asm ├── arial.ttf ├── Corewar.bmp ├── champions ├── 42.cor ├── roller.cor ├── spartiates.cor └── try_again.cor ├── img └── corewar.png ├── README.md ├── includes ├── SDL_definitions.inc ├── macro.inc └── corewar.inc ├── corewar.man └── Makefile /.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | corewar 3 | -------------------------------------------------------------------------------- /src/utils/#my_strlen.c#: -------------------------------------------------------------------------------- 1 | 2 | section .text -------------------------------------------------------------------------------- /arial.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sebastiencs/Corewar_x86/HEAD/arial.ttf -------------------------------------------------------------------------------- /Corewar.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sebastiencs/Corewar_x86/HEAD/Corewar.bmp -------------------------------------------------------------------------------- /champions/42.cor: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sebastiencs/Corewar_x86/HEAD/champions/42.cor -------------------------------------------------------------------------------- /img/corewar.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sebastiencs/Corewar_x86/HEAD/img/corewar.png -------------------------------------------------------------------------------- /champions/roller.cor: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sebastiencs/Corewar_x86/HEAD/champions/roller.cor -------------------------------------------------------------------------------- /champions/spartiates.cor: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sebastiencs/Corewar_x86/HEAD/champions/spartiates.cor -------------------------------------------------------------------------------- /champions/try_again.cor: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sebastiencs/Corewar_x86/HEAD/champions/try_again.cor -------------------------------------------------------------------------------- /src/utils/convert_endian.asm: -------------------------------------------------------------------------------- 1 | 2 | section .data 3 | 4 | global little_to_big_endian 5 | 6 | little_to_big_endian: 7 | 8 | push ebp 9 | mov ebp, esp 10 | 11 | mov eax, [ebp + 8] 12 | bswap eax 13 | 14 | mov esp, ebp 15 | pop ebp 16 | ret 17 | -------------------------------------------------------------------------------- /src/functions/is_good_register.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: is_good_register.asm 4 | ;; Author: chapui_s 5 | ;; Created: 20/07/2014 23:12:16 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | 12 | %include "corewar.inc" 13 | 14 | section .text 15 | 16 | proc is_good_register, nb 17 | 18 | xor eax, eax 19 | 20 | cmp dword [nb], REG_NUMBER 21 | jg .endproc 22 | 23 | cmp dword [nb], 0 24 | jle .endproc 25 | 26 | mov eax, 1 27 | 28 | endproc -------------------------------------------------------------------------------- /src/utils/usage.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: usage.asm 4 | ;; Author: chapui_s 5 | ;; Created: 5/08/2014 02:14:40 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "macro.inc" 12 | 13 | section .data 14 | 15 | msg: db 'usage: ./corewar [-dump nbr_cycle] [[-n prog_number] [-a load_address] prog_name] ...', 10, 0 16 | 17 | section .text 18 | 19 | extern my_putstr 20 | 21 | proc usage 22 | 23 | invoke my_putstr, msg 24 | 25 | mov eax, -1 26 | 27 | endproc -------------------------------------------------------------------------------- /src/utils/init_arena.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: init_arena.asm 4 | ;; Author: chapui_s 5 | ;; Created: 5/08/2014 01:33:48 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "macro.inc" 12 | 13 | section .text 14 | 15 | proc init_arena, ptr, size, value 16 | 17 | pushx eax, edi, ecx 18 | pushfd 19 | 20 | mov eax, [value] 21 | mov edi, [ptr] 22 | mov ecx, [size] 23 | cld 24 | 25 | repne stosb 26 | 27 | popfd 28 | popx eax, edi, ecx 29 | 30 | endproc 31 | -------------------------------------------------------------------------------- /src/utils/my_putstr.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: my_putstr.asm 4 | ;; Author: chapui_s 5 | ;; Created: 26/07/2014 15:45:25 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .text 14 | 15 | extern my_strlen 16 | 17 | proc my_putstr, string 18 | 19 | pushx eax, ebx, ecx, edx 20 | 21 | mov edx, 4 22 | mov ebx, 1 23 | mov ecx, [string] 24 | invoke my_strlen, [string] 25 | xchg edx, eax 26 | int 0x80 27 | 28 | popx eax, ebx, ecx, edx 29 | 30 | endproc -------------------------------------------------------------------------------- /src/utils/my_strlen.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: my_strlen.asm 4 | ;; Author: chapui_s 5 | ;; Created: 26/07/2014 15:41:47 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "macro.inc" 12 | 13 | section .text 14 | 15 | proc my_strlen, string 16 | 17 | pushx ecx, edi 18 | 19 | xor eax, eax 20 | mov ecx, 0xffffffff 21 | mov edi, [string] 22 | test edi, edi 23 | jz end 24 | cld 25 | 26 | repne scasb 27 | 28 | not ecx 29 | dec ecx 30 | 31 | mov eax, ecx 32 | end: popx ecx, edi 33 | 34 | endproc 35 | -------------------------------------------------------------------------------- /src/init_reg.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: init_reg.asm 4 | ;; Author: chapui_s 5 | ;; Created: 6/08/2014 00:09:28 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .text 14 | 15 | proc init_reg, reg, prog_number 16 | 17 | pushx eax, ecx, edx 18 | 19 | mov ecx, 2 20 | mov eax, [reg] 21 | mov edx, [prog_number] 22 | mov [eax + 4], edx 23 | 24 | WHILE ecx, le, REG_NUMBER 25 | 26 | mov dword [eax + (ecx * 4)], 0 27 | inc ecx 28 | 29 | END_WHILE 30 | 31 | popx eax, ecx, edx 32 | 33 | endproc 34 | -------------------------------------------------------------------------------- /src/utils/swap_int.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: swap_int.asm 4 | ;; Author: chapui_s 5 | ;; Created: 5/08/2014 02:05:25 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "macro.inc" 12 | 13 | section .text 14 | 15 | proc swap_int, a, b 16 | 17 | pushx edx 18 | 19 | _var_ tmp 20 | 21 | mov eax, [a] 22 | mov eax, [eax] 23 | mov [tmp], eax 24 | 25 | mov eax, [b] 26 | mov eax, [eax] 27 | mov edx, [a] 28 | mov [edx], eax 29 | 30 | mov eax, [b] 31 | mov edx, [tmp] 32 | mov [eax], edx 33 | 34 | popx edx 35 | 36 | endproc 37 | -------------------------------------------------------------------------------- /src/gui/put_background.asm: -------------------------------------------------------------------------------- 1 | 2 | %include "corewar.inc" 3 | 4 | section .text 5 | 6 | extern get_image_path 7 | extern load_players_name 8 | 9 | global put_background 10 | 11 | put_background: 12 | 13 | push ebp 14 | mov ebp, esp 15 | 16 | mov eax, [ebp + 12] 17 | add eax, s_gui.pos_background 18 | mov word [eax + SDL_Rect.x], 0 19 | mov word [eax + SDL_Rect.y], 705 20 | 21 | push dword [ebp + 16] 22 | push dword [ebp + 12] 23 | call get_image_path 24 | add esp, 8 25 | cmp eax, 0 26 | jne .FAIL 27 | 28 | push dword [ebp + 12] 29 | push dword [ebp + 8] 30 | call load_players_name 31 | add esp, 8 32 | cmp eax, -1 33 | je .FAIL 34 | 35 | xor eax, eax 36 | jmp .END 37 | 38 | .FAIL mov eax, -1 39 | 40 | .END mov esp, ebp 41 | pop ebp 42 | ret 43 | -------------------------------------------------------------------------------- /src/functions/is_register.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: is_register.asm 4 | ;; Author: chapui_s 5 | ;; Created: 20/07/2014 23:27:20 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | proc is_register, octet_type, num_param 14 | 15 | mov eax, 1 16 | 17 | .LOOP cmp eax, [num_param] 18 | jge .ENDL 19 | 20 | shl dword [octet_type], 2 21 | inc eax 22 | jmp .LOOP 23 | 24 | .ENDL and dword [octet_type], 0b11000000 25 | cmp dword [octet_type], 0b01000000 26 | je .ONE 27 | 28 | xor eax, eax 29 | jmp .endproc 30 | 31 | .ONE mov eax, 1 32 | 33 | endproc -------------------------------------------------------------------------------- /src/functions/is_direct.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: is_direct.asm 4 | ;; Author: chapui_s 5 | ;; Created: 20/07/2014 23:16:10 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | 12 | %include "corewar.inc" 13 | 14 | section .text 15 | 16 | proc is_direct, octet_type, num_param 17 | 18 | mov eax, 1 19 | 20 | .LOOP cmp eax, [num_param] 21 | jge .ENDL 22 | 23 | shl dword [octet_type], 2 24 | inc eax 25 | jmp .LOOP 26 | 27 | .ENDL and dword [octet_type], 0b11000000 28 | cmp dword [octet_type], 0b10000000 29 | je .ONE 30 | 31 | xor eax, eax 32 | jmp .endproc 33 | 34 | .ONE mov eax, 1 35 | 36 | endproc -------------------------------------------------------------------------------- /src/functions/is_indirect.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: is_indirect.asm 4 | ;; Author: chapui_s 5 | ;; Created: 20/07/2014 23:21:16 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .text 14 | 15 | proc is_indirect, octet_type, num_param 16 | 17 | mov eax, 1 18 | 19 | .LOOP cmp eax, [num_param] 20 | jge .ENDL 21 | 22 | shl dword [octet_type], 2 23 | inc eax 24 | jmp .LOOP 25 | 26 | .ENDL and dword [octet_type], 0b11000000 27 | cmp dword [octet_type], 0b11000000 28 | je .ONE 29 | 30 | xor eax, eax 31 | jmp .endproc 32 | 33 | .ONE mov eax, 1 34 | 35 | endproc -------------------------------------------------------------------------------- /src/utils/is_already_define.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: is_already_define.asm 4 | ;; Author: chapui_s 5 | ;; Created: 5/08/2014 01:41:36 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .text 14 | 15 | proc is_addr_already_define, champions 16 | 17 | pushx edx 18 | 19 | xor edx, edx 20 | mov eax, [champions] 21 | 22 | WHILE eax, ne, 0 23 | 24 | IF dword [eax + s_champions.load_address], ne, 0 25 | 26 | inc edx 27 | 28 | ENDIF 29 | 30 | mov eax, [eax + s_champions.next] 31 | 32 | END_WHILE 33 | 34 | mov eax, edx 35 | popx edx 36 | 37 | endproc 38 | -------------------------------------------------------------------------------- /src/find_max_prog_number.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: find_max_prog_number.asm 4 | ;; Author: chapui_s 5 | ;; Created: 5/08/2014 23:51:59 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .text 14 | 15 | proc find_max_prog_number, champions 16 | 17 | pushx edx 18 | 19 | mov eax, [champions] 20 | xor edx, edx 21 | 22 | WHILE eax, ne, 0 23 | 24 | cmp [eax + s_champions.prog_number], edx 25 | cmovg edx, [eax + s_champions.prog_number] ; bien cette instruction 26 | 27 | mov eax, [eax + s_champions.next] 28 | 29 | END_WHILE 30 | 31 | mov eax, edx 32 | popx edx 33 | 34 | endproc 35 | -------------------------------------------------------------------------------- /src/gui/get_list_pc.asm: -------------------------------------------------------------------------------- 1 | 2 | %include "corewar.inc" 3 | 4 | section .text 5 | 6 | extern disp_gui 7 | extern disp_core 8 | extern disp_list 9 | 10 | global get_list_pc 11 | 12 | get_list_pc: 13 | 14 | push ebp 15 | mov ebp, esp 16 | 17 | push ecx 18 | push ebx 19 | push edx 20 | 21 | mov eax, [ebp + 12] 22 | mov eax, [eax + s_gui.list_pc] 23 | mov ebx, [ebp + 8] 24 | mov ebx, [ebx + s_corewar.champions] 25 | 26 | mov ecx, 0 27 | 28 | .LOOP cmp ebx, 0 29 | je .END 30 | 31 | cmp ecx, ((MAX_PC * 2) - 1) 32 | jge .END 33 | 34 | mov edx, [ebx + s_champions.pc] 35 | mov [eax], edx 36 | mov edx, [ebx + s_champions.color_gui] 37 | mov [eax + 4], edx 38 | 39 | add eax, 8 40 | add ecx, 2 41 | mov ebx, [ebx + s_champions.next] 42 | jmp .LOOP 43 | 44 | .END xor eax, eax 45 | pop edx 46 | pop ebx 47 | pop ecx 48 | mov esp, ebp 49 | pop ebp 50 | ret 51 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Corewar ASM x86 2 | ===== 3 | 4 | ![Alt text](img/corewar.png?raw=true "Corewar ASM") 5 | 6 | Corewar_x86 is a Virtual Machine fully written in Assembly language using Intel syntax. 7 | It's fully functional, if you found bugs please report them. 8 | This project derived from the 'Corewar' project [https://github.com/sebastiencs/corewar] written in C. 9 | It doesn't include the Assembler, only the VM. 10 | 11 | Tested with Fedora 20 x86_64. 12 | I needed to install some librairies: 13 | 14 | `# yum install glibc.i686 ncurses-libs.i686 libzip.i686 libX11.i686 libXrandr.i686 SDL.i686 SDL_ttf.i686 SDL-devel.i686 SDL_ttf-devel.i686 glibc-devel.i686 ` 15 | 16 | Command line: 17 | `$ ./corewar champions/42.cor champions.try_again.cor` 18 | 19 | To dissasemble champions: 20 | `$ ./corewar -d champions/42.cor` 21 | 22 | A man page is also available: 23 | `$ man ./corewar.man ` 24 | -------------------------------------------------------------------------------- /src/gui/hex_to_str.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: hex_to_str.asm 4 | ;; Author: chapui_s 5 | ;; Created: 26/07/2014 17:59:45 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "macro.inc" 12 | 13 | section .text 14 | 15 | proc hex_to_str, nb, string 16 | 17 | pushx ecx, edx, edi 18 | 19 | mov edx, [nb] 20 | mov edi, [string] 21 | mov ecx, 3 22 | 23 | .LOOP rol dl, 4 24 | movzx eax, dl 25 | and eax, 0xF 26 | 27 | IF eax, l, 0xA 28 | 29 | add eax, '0' 30 | 31 | ELSE 32 | 33 | add eax, 'A' - 0x0A 34 | 35 | ENDIF 36 | 37 | .NOT stosb 38 | loop .LOOP 39 | 40 | xor eax, eax 41 | dec edi 42 | stosb 43 | mov eax, [string] 44 | 45 | popx ecx, edx, edi 46 | 47 | endproc -------------------------------------------------------------------------------- /src/functions/get_size_param.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: get_size_param.asm 4 | ;; Author: chapui_s 5 | ;; Created: 21/07/2014 19:33:26 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .text 14 | 15 | proc get_size_param, type 16 | 17 | _var_ size 18 | 19 | mov dword [size], 0 20 | 21 | .LOOP movzx eax, byte [type] 22 | and eax, 0b11000000 23 | cmp eax, 0 24 | je .ENDL 25 | 26 | .1 cmp eax, 1 27 | jne .2 28 | 29 | inc dword [size] 30 | 31 | .2 cmp eax, 0b10 32 | jne .3 33 | 34 | add dword [size], 4 35 | jmp .NEXT 36 | 37 | .3 add dword [size], 2 38 | 39 | .NEXT shl byte [type], 2 40 | jmp .LOOP 41 | 42 | .ENDL mov eax, [size] 43 | inc eax 44 | 45 | endproc -------------------------------------------------------------------------------- /src/utils/my_strcmp.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: my_strcmp.asm 4 | ;; Author: chapui_s 5 | ;; Created: 5/08/2014 02:01:42 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "macro.inc" 12 | 13 | section .text 14 | 15 | proc my_strcmp, s1, s2 16 | 17 | pushx esi, edi 18 | pushfd 19 | 20 | xor eax, eax 21 | mov esi, [s1] 22 | mov edi, [s2] 23 | cld 24 | 25 | .LOOP cmpsb 26 | jnz .DIFF 27 | 28 | cmp byte [esi - 1], 0 29 | jne .LOOP 30 | jmp .END 31 | 32 | .DIFF movzx eax, byte [esi - 1] 33 | sub al, byte [edi - 1] 34 | cbw 35 | cwde 36 | 37 | .END popfd 38 | popx esi, edi 39 | 40 | endproc 41 | -------------------------------------------------------------------------------- /src/utils/check_place_arena.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: check_place_arena.asm 4 | ;; Author: chapui_s 5 | ;; Created: 5/08/2014 00:24:09 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "macro.inc" 12 | 13 | section .data 14 | 15 | str1: db 'error: two champions are in the same address', 10, 0 16 | 17 | section .text 18 | 19 | extern my_putstr 20 | 21 | proc check_place_arena, info_arena, prog_number, i 22 | 23 | pushx edx 24 | 25 | mov eax, [info_arena] 26 | mov edx, [i] 27 | add eax, [edx] 28 | inc dword [edx] 29 | 30 | IF byte [eax], e, 0 31 | 32 | mov edx, [prog_number] 33 | mov byte [eax], dl 34 | xor eax, eax 35 | 36 | ELSE 37 | 38 | invoke my_putstr, str1 39 | mov eax, -1 40 | 41 | ENDIF 42 | 43 | popx edx 44 | 45 | endproc 46 | -------------------------------------------------------------------------------- /src/utils/int_to_str.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: int_to_str.asm 4 | ;; Author: chapui_s 5 | ;; Created: 18/07/2014 19:51:04 (+08:00 UTC) 6 | ;; Updated: 5/08/2014 01:39:37 (+08:00 UTC) 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "macro.inc" 12 | 13 | section .text 14 | 15 | proc int_to_str, nb, string 16 | 17 | pushx ebx, ecx, edx, edi 18 | 19 | mov eax, [nb] 20 | mov edi, [string] 21 | mov ebx, 10 22 | xor ecx, ecx 23 | 24 | IF eax, l, 0 25 | 26 | mov [edi], byte '-' 27 | inc edi 28 | neg eax 29 | 30 | ENDIF 31 | 32 | .L1 cdq 33 | div ebx 34 | push edx 35 | inc ecx 36 | cmp eax, 0 37 | jne .L1 38 | 39 | .L2 pop eax 40 | add eax, '0' 41 | stosb 42 | loop .L2 43 | 44 | xor eax, eax 45 | stosb 46 | 47 | mov eax, [string] 48 | 49 | popx ebx, ecx, edx, edi 50 | 51 | endproc 52 | -------------------------------------------------------------------------------- /src/gui/get_color.asm: -------------------------------------------------------------------------------- 1 | 2 | %include "corewar.inc" 3 | 4 | section .data 5 | 6 | str1: db 'edx = %d', 10, 0 7 | 8 | section .text 9 | 10 | extern get_color_champions 11 | extern set_color_with_pc 12 | 13 | extern TTF_RenderText_Solid 14 | extern disp_gui 15 | 16 | global get_color 17 | 18 | get_color: 19 | 20 | push ebp 21 | mov ebp, esp 22 | 23 | push ecx 24 | push edx 25 | push ebx 26 | 27 | mov eax, [ebp + 12] 28 | mov eax, [eax + s_corewar.info_arena] 29 | ;add eax, s_corewar.info_arena 30 | add eax, dword [ebp + 16] 31 | 32 | movzx edx, byte [eax] 33 | 34 | push dword edx 35 | push dword [ebp + 8] 36 | call get_color_champions 37 | add esp, 8 38 | 39 | push dword [ebp + 16] 40 | push dword [ebp + 12] 41 | push dword [ebp + 8] 42 | call set_color_with_pc 43 | add esp, 12 44 | cmp eax, -1 45 | je .FAIL 46 | 47 | xor eax, eax 48 | jmp .END 49 | 50 | .FAIL mov eax, -1 51 | 52 | .END pop ebx 53 | pop edx 54 | pop ecx 55 | mov esp, ebp 56 | pop ebp 57 | ret 58 | -------------------------------------------------------------------------------- /src/init_values_champions.asm: -------------------------------------------------------------------------------- 1 | 2 | %include "corewar.inc" 3 | 4 | section .data 5 | 6 | str1: db MALLOC_FAILED 7 | 8 | section .text 9 | 10 | extern init_reg 11 | extern malloc 12 | extern my_putstr 13 | 14 | proc init_values_champions, champions 15 | 16 | mov edx, [champions] 17 | 18 | WHILE edx, ne, 0 19 | 20 | push edx 21 | 22 | invoke malloc, (4 * (REG_NUMBER + 1)) ; sizeof(int) * ... 23 | _check_ .FAIL, 0 24 | 25 | pop edx 26 | mov [edx + s_champions.reg], eax 27 | 28 | invoke init_reg, [edx + s_champions.reg], [edx + s_champions.prog_number] 29 | 30 | mov eax, [edx + s_champions.load_address] 31 | mov [edx + s_champions.pc], eax 32 | 33 | mov dword [edx + s_champions.carry], 0 34 | mov dword [edx + s_champions.last_live], 0 35 | mov dword [edx + s_champions.cycle_to_wait], 0 36 | 37 | mov edx, [edx + s_champions.next] 38 | 39 | END_WHILE 40 | 41 | xor eax, eax 42 | RET 43 | 44 | .FAIL invoke my_putstr, str1 45 | mov eax, -1 46 | 47 | endproc 48 | -------------------------------------------------------------------------------- /src/utils/check_big_prog_number.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: check_big_prog_number.asm 4 | ;; Author: chapui_s 5 | ;; Created: 5/08/2014 00:22:33 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .data 14 | 15 | str1: db 'error: prog_number too big !', 10, 0 16 | 17 | section .text 18 | 19 | extern my_putstr 20 | 21 | proc check_big_prog_number, list_champion, nb_champions 22 | 23 | pushx edx 24 | 25 | mov eax, [list_champion] 26 | mov edx, [nb_champions] 27 | 28 | WHILE eax, ne, 0 29 | 30 | IF [eax + s_champions.prog_number], g, edx 31 | 32 | jmp .FAIL 33 | 34 | ENDIF 35 | 36 | mov eax, [eax + s_champions.next] 37 | 38 | END_WHILE 39 | 40 | xor eax, eax 41 | .END popx edx 42 | RET 43 | 44 | .FAIL invoke my_putstr, str1 45 | mov eax, -1 46 | jmp .END 47 | 48 | endproc 49 | -------------------------------------------------------------------------------- /src/utils/my_strcat.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: my_strcat.asm 4 | ;; Author: chapui_s 5 | ;; Created: 5/08/2014 01:51:43 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "macro.inc" 12 | 13 | section .text 14 | 15 | extern malloc 16 | extern my_strlen 17 | 18 | proc my_strcat, s1, s2 19 | 20 | _var_ len1, len2, ptr 21 | 22 | pushx ecx, edi, esi 23 | pushfd 24 | 25 | invoke my_strlen, [s1] 26 | mov [len1], eax 27 | 28 | invoke my_strlen, [s2] 29 | mov [len2], eax 30 | 31 | add eax, [len1] 32 | 33 | invoke malloc, eax 34 | _check_ .END, 0 35 | 36 | mov [ptr], eax 37 | cld 38 | 39 | mov esi, [s1] 40 | mov edi, eax 41 | mov ecx, [len1] 42 | rep movsb 43 | 44 | mov esi, [s2] 45 | mov ecx, [len2] 46 | rep movsb 47 | 48 | mov byte [edi], 0 49 | 50 | mov eax, [ptr] 51 | 52 | .END popfd 53 | popx ecx, edi, esi 54 | RET 55 | 56 | endproc 57 | -------------------------------------------------------------------------------- /includes/SDL_definitions.inc: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: SDL_definitions.inc 4 | ;; Author: chapui_s 5 | ;; Created: 25/07/2014 21:40:23 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %ifndef SDL_DEFINITIONS_INC_ 12 | %define SDL_DEFINITIONS_INC_ 13 | 14 | struc SDL_keysym 15 | .scancode: resb 1 16 | .unused: resb 3 ; alignb 4 ; Uint8 17 | .sym: resd 1 ; SDLKey 18 | .mod: resd 1 ; SDLMod 19 | .unicode: resw 1 20 | alignb 2 ; Uint16 21 | endstruc 22 | 23 | struc SDL_KeyboardEvent 24 | .type: resb 1 ; Uint8 25 | .which: resb 1 ; Uint8 26 | .state: resb 1 ; Uint8 27 | .unused: resb 1 28 | .keysim: resb 16 ; SDL_Keysim 29 | endstruc 30 | 31 | struc SDL_Event 32 | resb 20 ; Union de tout plein de trucs (SDL_Event.h) 33 | endstruc 34 | 35 | %define SDL_KEYDOWN (2) 36 | %define SDL_QUIT (12) 37 | %define SDLK_ESCAPE (27) 38 | %define SDLK_SPACE (32) 39 | 40 | %endif -------------------------------------------------------------------------------- /src/exec_instructions/search_who_still_alive.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: search_who_still_alive.asm 4 | ;; Author: chapui_s 5 | ;; Created: 19/07/2014 18:37:52 (+08:00 UTC) 6 | ;; Updated: 19/07/2014 19:11:08 (+08:00 UTC) 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .text 14 | 15 | extern winner 16 | 17 | proc search_who_still_alive, core 18 | 19 | _var_ id_champion 20 | 21 | pushx edx 22 | 23 | mov eax, [core] 24 | mov eax, [eax + s_corewar.champions] 25 | mov dword [id_champion], 0 26 | 27 | .LOOP cmp eax, 0 28 | je .ENDL 29 | 30 | mov edx, [eax + s_champions.color_gui] 31 | 32 | cmp dword [id_champion], 0 33 | je .ELSE 34 | 35 | cmp dword [id_champion], edx 36 | je .ELSE 37 | 38 | mov eax, 1 39 | jmp .ENDPROC 40 | 41 | .ELSE mov [id_champion], edx 42 | 43 | mov eax, [eax + s_champions.next] 44 | jmp .LOOP 45 | 46 | .ENDL invoke winner, [core] 47 | popx edx 48 | xor eax, eax 49 | 50 | endproc -------------------------------------------------------------------------------- /src/save_args.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: save_args.asm 4 | ;; Author: chapui_s 5 | ;; Created: 6/08/2014 01:49:11 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .text 14 | 15 | extern save_champion 16 | extern is_file_dot_cor 17 | 18 | proc save_args, argc, argv, core 19 | 20 | _var_ nb_cor 21 | 22 | pushx ecx, edx 23 | 24 | mov ecx, 1 25 | mov dword [nb_cor], 0 26 | 27 | WHILE ecx, l, [argc] 28 | 29 | mov eax, [argv] 30 | invoke is_file_dot_cor, [eax + (ecx * 4)] 31 | 32 | IF eax, e, 1 33 | 34 | inc dword [nb_cor] 35 | 36 | invoke save_champion, ecx, [argv], [core] 37 | _check_ .FAIL, -1 38 | 39 | ENDIF 40 | 41 | inc ecx 42 | 43 | END_WHILE 44 | 45 | mov eax, [core] 46 | mov edx, [nb_cor] 47 | mov [eax + s_corewar.nb_champions], edx 48 | 49 | xor eax, eax 50 | 51 | .END popx ecx, edx 52 | RET 53 | 54 | .FAIL mov eax, -1 55 | jmp .END 56 | 57 | endproc 58 | -------------------------------------------------------------------------------- /src/functions/copy_reg.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: copy_reg.asm 4 | ;; Author: chapui_s 5 | ;; Created: 21/07/2014 20:17:12 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .data 14 | 15 | str1: db 'error: malloc', 10, 0 16 | 17 | section .text 18 | 19 | extern malloc 20 | extern my_putstr 21 | 22 | proc copy_reg, father 23 | 24 | _var_ new 25 | 26 | pushx ebx, ecx, edx 27 | 28 | mov eax, REG_NUMBER 29 | inc eax 30 | shl eax, 2 31 | invoke malloc, eax 32 | _check_ .FAIL, 0 33 | mov [new], eax 34 | 35 | mov ecx, 1 36 | 37 | mov eax, [new] 38 | mov edx, [father] 39 | 40 | .LOOP cmp ecx, REG_NUMBER 41 | jg .ENDL 42 | 43 | mov ebx, [edx] 44 | mov [eax], ebx 45 | 46 | add eax, 4 47 | add edx, 4 48 | inc ecx 49 | jmp .LOOP 50 | 51 | .ENDL mov eax, [new] 52 | popx ebx, ecx, edx 53 | jmp .endproc 54 | 55 | .FAIL invoke my_putstr, str1 56 | popx ebx, ecx, edx 57 | xor eax, eax 58 | 59 | endproc -------------------------------------------------------------------------------- /src/gui/is_pc.asm: -------------------------------------------------------------------------------- 1 | 2 | %include "corewar.inc" 3 | 4 | section .data 5 | 6 | str1: db 'list[cur] = %d\n', 10, 0 7 | 8 | section .text 9 | 10 | extern get_color_champions 11 | 12 | extern printf 13 | extern disp_list 14 | 15 | global is_pc 16 | 17 | is_pc: 18 | 19 | push ebp 20 | mov ebp, esp 21 | 22 | sub esp, 8 23 | ; [ebp - 4] cur 24 | ; [ebp - 8] j 25 | 26 | ; invoke disp_list, [ebp + 8], [ebp + 12] 27 | 28 | mov eax, [ebp + 12] 29 | mov eax, [eax + s_gui.list_pc] ; eax -> list_pc 30 | mov ecx, [ebp + 16] ; ecx -> i 31 | mov dword [ebp - 8], 0 32 | mov edx, [ebp + 8] 33 | mov edx, [edx + s_corewar.nb_champions] ; edx -> nb_champions 34 | mov dword [ebp - 4], 0 35 | 36 | .LOOP cmp dword [ebp - 8], edx 37 | jge .END 38 | 39 | cmp dword [ebp - 4], ((MAX_PC * 2) - 1) 40 | jge .END 41 | 42 | cmp [eax], ecx 43 | jne .NEXT 44 | 45 | ; invoke printf, str1, [eax] 46 | 47 | 48 | push dword [eax + 4] 49 | push dword [ebp + 12] 50 | call get_color_champions 51 | add esp, 8 52 | mov eax, 1 53 | jmp .END1 54 | 55 | .NEXT inc dword [ebp - 8] 56 | add eax, 8 57 | jmp .LOOP 58 | 59 | .END xor eax, eax 60 | .END1 mov esp, ebp 61 | pop ebp 62 | ret 63 | -------------------------------------------------------------------------------- /src/utils/my_putnbr.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: my_putnbr.asm 4 | ;; Author: chapui_s 5 | ;; Created: 19/07/2014 18:19:44 (+08:00 UTC) 6 | ;; Updated: 6/08/2014 19:56:58 (+08:00 UTC) 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .text 14 | 15 | proc my_putchar, caracter 16 | 17 | pushx eax, ebx, ecx, edx 18 | 19 | mov eax, 4 20 | mov ebx, 1 21 | lea ecx, [caracter] 22 | mov edx, 1 23 | int 80h 24 | 25 | popx eax, ebx, ecx, edx 26 | 27 | endproc 28 | 29 | proc my_putnbr, nb 30 | 31 | pushx eax, ebx, edx 32 | 33 | IF dword [nb], l, 0 34 | 35 | invoke my_putchar, '-' 36 | mov eax, [nb] 37 | mov edx, -1 38 | imul edx 39 | mov [nb], eax 40 | 41 | ENDIF 42 | 43 | mov ebx, 10 44 | 45 | IF dword [nb], ge, 10 46 | 47 | mov eax, [nb] 48 | cdq 49 | idiv ebx 50 | invoke my_putnbr, eax 51 | 52 | ENDIF 53 | 54 | mov eax, [nb] 55 | cdq 56 | idiv ebx 57 | add edx, '0' 58 | invoke my_putchar, edx 59 | 60 | popx eax, ebx, edx 61 | 62 | endproc -------------------------------------------------------------------------------- /src/desassembleur/desassemble_it.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: desassemble_it.asm 4 | ;; Author: chapui_s 5 | ;; Created: 6/08/2014 18:52:49 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .data 14 | 15 | str1: db 9, '[', 0 16 | str2: db ']', 10, 0 17 | 18 | section .text 19 | 20 | extern my_putstr 21 | extern my_desassembler 22 | 23 | proc desassemble_it, core 24 | 25 | _var_ is_filename 26 | 27 | mov eax, [core] 28 | mov eax, [eax + s_corewar.champions] 29 | 30 | mov dword [is_filename], 0 31 | 32 | IF dword [eax + s_champions.next], ne, 0 33 | 34 | mov dword [is_filename], 1 35 | 36 | ENDIF 37 | 38 | WHILE eax, ne, 0 39 | 40 | IF dword [is_filename], e, 1 41 | 42 | invoke my_putstr, str1 43 | invoke my_putstr, [eax + s_champions.filename] 44 | invoke my_putstr, str2 45 | 46 | ENDIF 47 | 48 | invoke my_desassembler, [core], eax 49 | 50 | mov eax, [eax + s_champions.next] 51 | 52 | END_WHILE 53 | 54 | xor eax, eax 55 | 56 | endproc -------------------------------------------------------------------------------- /src/functions/my_zjmp.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: my_zjmp.asm 4 | ;; Author: chapui_s 5 | ;; Created: 23/07/2014 22:53:54 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .text 14 | 15 | proc my_zjmp, core, champions, instruction 16 | 17 | _var_ param0, pc, mem_size, idx_mod 18 | 19 | pushx ebx, edx 20 | 21 | mov eax, [champions] 22 | cmp dword [eax + s_champions.carry], 1 23 | jne .END 24 | 25 | mov edx, [eax + s_champions.pc] 26 | mov dword [pc], edx 27 | 28 | mov dword [idx_mod], IDX_MOD 29 | mov dword [mem_size], MEM_SIZE 30 | 31 | mov eax, [instruction] 32 | mov eax, [eax + s_instruction.params] 33 | cdq 34 | idiv dword [idx_mod] 35 | 36 | mov eax, [pc] 37 | sub eax, 3 38 | add eax, edx 39 | 40 | cdq 41 | idiv dword [mem_size] 42 | 43 | .LOOP cmp edx, 0 44 | jge .ENDL 45 | 46 | add edx, MEM_SIZE 47 | jmp .LOOP 48 | 49 | .ENDL mov eax, [champions] 50 | mov [eax + s_champions.pc], edx 51 | 52 | .END xor eax, eax 53 | popx ebx, edx 54 | 55 | endproc -------------------------------------------------------------------------------- /src/exec_instructions/is_special.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: is_special.asm 4 | ;; Author: chapui_s 5 | ;; Created: 19/07/2014 19:40:27 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | 12 | %include "corewar.inc" 13 | 14 | section .text 15 | 16 | proc is_special, champions, instruction, decal 17 | 18 | pushx edx 19 | 20 | mov edx, [champions] 21 | mov edx, [edx + s_champions.pc] 22 | 23 | mov eax, [decal] 24 | sub eax, edx 25 | mov edx, eax 26 | 27 | mov eax, [instruction] 28 | 29 | .FIRST cmp byte [eax + s_instruction.code], STI 30 | jne .SECOND 31 | 32 | cmp edx, 3 33 | je .SPEC 34 | 35 | cmp edx, 5 36 | je .SPEC 37 | 38 | .SECOND cmp byte [eax + s_instruction.code], LDI 39 | jne .THIRD 40 | 41 | cmp edx, 2 42 | je .SPEC 43 | 44 | cmp edx, 4 45 | je .SPEC 46 | 47 | .THIRD cmp byte [eax + s_instruction.code], LLDI 48 | jne .END 49 | 50 | cmp edx, 2 51 | je .SPEC 52 | 53 | cmp edx, 4 54 | je .SPEC 55 | 56 | .END xor eax, eax 57 | popx edx 58 | jmp .ENDPROC 59 | 60 | .SPEC mov eax, 1 61 | popx edx 62 | 63 | endproc -------------------------------------------------------------------------------- /src/exec_instructions/check_first_in_list.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: check_first_in_list.asm 4 | ;; Author: chapui_s 5 | ;; Created: 19/07/2014 19:11:22 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .text 14 | 15 | extern free 16 | 17 | proc check_first_in_list, core, cycle_to_die_cur 18 | 19 | _var_ tmp 20 | 21 | pushx edx 22 | 23 | mov eax, [core] 24 | mov eax, [eax + s_corewar.champions] 25 | mov [tmp], eax 26 | 27 | .LOOP cmp dword [tmp], 0 28 | je .ENDL 29 | 30 | mov eax, [tmp] 31 | mov edx, [cycle_to_die_cur] 32 | cmp dword [eax + s_champions.last_live], edx 33 | jl .ENDL 34 | 35 | mov eax, [eax + s_champions.next] 36 | mov edx, [core] 37 | mov dword [edx + s_corewar.champions], eax 38 | 39 | mov eax, [tmp] 40 | invoke free, [eax + s_champions.reg] 41 | invoke free, eax 42 | 43 | mov eax, [core] 44 | dec dword [eax + s_corewar.nb_champions] 45 | 46 | mov eax, [core] 47 | mov eax, [eax + s_corewar.champions] 48 | mov [tmp], eax 49 | jmp .LOOP 50 | 51 | .ENDL popx edx 52 | 53 | endproc -------------------------------------------------------------------------------- /src/utils/attribute_address.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: attribute_address.asm 4 | ;; Author: chapui_s 5 | ;; Created: 4/08/2014 17:10:02 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .text 14 | 15 | extern is_addr_already_define 16 | extern attribute_address_defined 17 | 18 | proc attribute_address, champions, nb_champions 19 | 20 | _var_ place_after 21 | 22 | pushx edx 23 | 24 | mov eax, MEM_SIZE 25 | cdq 26 | idiv dword [nb_champions] 27 | mov [place_after], eax 28 | 29 | invoke is_addr_already_define, [champions] 30 | 31 | IF eax, e, 0x0 32 | 33 | mov eax, [champions] 34 | xor edx, edx 35 | 36 | WHILE eax, ne, 0x0 37 | 38 | mov [eax + s_champions.load_address], edx 39 | mov [eax + s_champions.pc], edx 40 | 41 | add edx, dword [place_after] 42 | mov eax, [eax + s_champions.next] 43 | 44 | END_WHILE 45 | 46 | ELSE 47 | 48 | invoke attribute_address_defined, [champions], [nb_champions], [place_after] 49 | 50 | ENDIF 51 | 52 | popx edx 53 | xor eax, eax 54 | 55 | endproc 56 | -------------------------------------------------------------------------------- /src/exec_instructions/winner.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: winner.asm 4 | ;; Author: chapui_s 5 | ;; Created: 19/07/2014 18:18:08 (+08:00 UTC) 6 | ;; Updated: 25/07/2014 21:19:24 (+08:00 UTC) 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .data 14 | 15 | str1: db 'le joueur ', 0 16 | str2: db '(', 0 17 | str3: db ') a gagne', 10, 0 18 | 19 | section .text 20 | 21 | extern my_putstr 22 | extern my_putnbr 23 | 24 | proc winner, core 25 | 26 | invoke my_putstr, str1 27 | 28 | mov eax, [core] 29 | IF dword [eax + s_corewar.champions], ne, 0 30 | 31 | mov eax, [eax + s_corewar.champions] 32 | invoke my_putnbr, [eax + s_champions.color_gui] 33 | 34 | ELSE 35 | 36 | invoke my_putnbr, [eax + s_corewar.last_live_nb] 37 | 38 | ENDIF 39 | 40 | invoke my_putstr, str2 41 | 42 | mov eax, [core] 43 | IF dword [eax + s_corewar.champions], ne, 0 44 | 45 | mov eax, [eax + s_corewar.champions] 46 | invoke my_putstr, [eax + s_champions.name] 47 | 48 | ELSE 49 | 50 | invoke my_putstr, [eax + s_corewar.last_live_name] 51 | 52 | ENDIF 53 | 54 | invoke my_putstr, str3 55 | 56 | endproc -------------------------------------------------------------------------------- /src/gui/get_color_champions.asm: -------------------------------------------------------------------------------- 1 | 2 | %include "corewar.inc" 3 | 4 | section .text 5 | 6 | global get_color_champions 7 | 8 | get_color_champions: 9 | 10 | push ebp 11 | mov ebp, esp 12 | 13 | push eax 14 | push edx 15 | 16 | movzx edx, byte [ebp + 12] 17 | 18 | mov eax, [ebp + 8] 19 | add eax, s_gui.my_color 20 | 21 | mov byte [eax + SDL_Color.r], 30 22 | mov byte [eax + SDL_Color.g], 30 23 | mov byte [eax + SDL_Color.b], 30 24 | mov byte [eax + SDL_Color.unused], 0 25 | 26 | cmp edx, 0 27 | je .END 28 | 29 | cmp edx, 4 30 | jg .END 31 | 32 | cmp edx, 1 33 | je .ONE 34 | cmp edx, 2 35 | je .TWO 36 | cmp edx, 3 37 | je .THREE 38 | cmp edx, 4 39 | je .FOUR 40 | 41 | jmp .END 42 | 43 | .ONE mov byte [eax + SDL_Color.r], 0 44 | mov byte [eax + SDL_Color.g], 255 45 | mov byte [eax + SDL_Color.b], 0 46 | jmp .END 47 | 48 | .TWO mov byte [eax + SDL_Color.r], 0 49 | mov byte [eax + SDL_Color.g], 0 50 | mov byte [eax + SDL_Color.b], 255 51 | jmp .END 52 | 53 | .THREE mov byte [eax + SDL_Color.r], 255 54 | mov byte [eax + SDL_Color.g], 255 55 | mov byte [eax + SDL_Color.b], 0 56 | jmp .END 57 | 58 | .FOUR mov byte [eax + SDL_Color.r], 66 59 | mov byte [eax + SDL_Color.g], 0 60 | mov byte [eax + SDL_Color.b], 66 61 | 62 | .END pop edx 63 | pop eax 64 | mov esp, ebp 65 | pop ebp 66 | ret 67 | -------------------------------------------------------------------------------- /src/utils/attribute_i_to_someone.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: attribute_i_to_someone.asm 4 | ;; Author: chapui_s 5 | ;; Created: 4/08/2014 17:36:40 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .text 14 | 15 | proc attribute_i_to_someone, list_champions, i 16 | 17 | _var_ is_already_attr 18 | 19 | pushx edx 20 | 21 | mov dword [is_already_attr], 0x0 22 | mov eax, [list_champions] 23 | 24 | WHILE eax, ne, 0x0 25 | 26 | mov edx, [i] 27 | 28 | IF dword [eax + s_champions.prog_number], e, edx 29 | 30 | mov dword [is_already_attr], 0x1 31 | 32 | ENDIF 33 | 34 | mov eax, [eax + s_champions.next] 35 | 36 | END_WHILE 37 | 38 | IF dword [is_already_attr], e, 0x1 39 | 40 | jmp .END 41 | 42 | ENDIF 43 | 44 | mov eax, [list_champions] 45 | 46 | WHILE eax, ne, 0x0 47 | 48 | IF dword [eax + s_champions.prog_number], e, 0x0 49 | 50 | mov edx, [i] 51 | mov dword [eax + s_champions.prog_number], edx 52 | jmp .END 53 | 54 | ENDIF 55 | 56 | mov eax, [eax + s_champions.next] 57 | 58 | END_WHILE 59 | 60 | .END popx edx 61 | xor eax, eax 62 | 63 | endproc 64 | -------------------------------------------------------------------------------- /src/desassembleur/print_args.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: print_args.asm 4 | ;; Author: chapui_s 5 | ;; Created: 6/08/2014 17:54:56 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .data 14 | 15 | str1: db 'r', 0 16 | 17 | section .text 18 | 19 | extern my_putstr 20 | extern my_putnbr 21 | extern my_putchar 22 | 23 | proc print_args, tmp_type, instruction, i 24 | 25 | _var_ tmp_int 26 | 27 | pushx eax, ecx, edx 28 | 29 | mov eax, [tmp_type] 30 | mov eax, [eax] 31 | mov [tmp_int], eax 32 | mov ecx, [i] 33 | 34 | and eax, 0b11000000 35 | shr eax, 6 36 | 37 | IF eax, e, 1 38 | 39 | invoke my_putstr, str1 40 | 41 | ELSEIF eax, e, 10b 42 | 43 | invoke my_putchar, DIRECT_CHAR 44 | 45 | ENDIF 46 | 47 | mov eax, [instruction] 48 | lea eax, [eax + s_instruction.params] 49 | mov eax, [eax + (ecx * 4)] 50 | invoke my_putnbr, eax 51 | 52 | mov eax, [tmp_int] 53 | shl al, 2 54 | mov edx, [tmp_type] 55 | mov [edx], eax 56 | 57 | and eax, 0b11000000 58 | IF eax, ne, 0 59 | 60 | invoke my_putchar, SEPARATOR_CHAR 61 | 62 | ENDIF 63 | 64 | popx eax, ecx, edx 65 | 66 | endproc -------------------------------------------------------------------------------- /src/utils/get_magic.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: get_magic.asm 4 | ;; Author: chapui_s 5 | ;; Created: 5/08/2014 00:57:05 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .data 14 | 15 | str1: db 'error: bad magic number with ', 0 16 | str2: db 10, 0 17 | str3: db 'error: read', 10, 0 18 | 19 | section .text 20 | 21 | extern little_to_big_endian 22 | extern my_putstr 23 | extern read 24 | 25 | proc get_magic, champions, fd 26 | 27 | _var_ buf 28 | 29 | pushx ebx, ecx, edx 30 | 31 | mov dword [buf], 0 32 | 33 | mov eax, 3 ; read 34 | mov ebx, [fd] 35 | lea ecx, [buf] 36 | mov edx, 4 ; size int 37 | int 80h 38 | 39 | _check_ .FAILR, -1 40 | 41 | mov eax, [buf] 42 | bswap eax 43 | 44 | IF eax, ne, COREWAR_EXEC_MAGIC 45 | 46 | invoke my_putstr, str1 47 | mov eax, [champions] 48 | invoke my_putstr, [eax + s_champions.filename] 49 | invoke my_putstr, str2 50 | mov eax, -1 51 | 52 | ELSE 53 | 54 | xor eax, eax 55 | 56 | ENDIF 57 | 58 | .END popx ebx, ecx, edx 59 | RET 60 | 61 | .FAILR invoke my_putstr, str3 62 | mov eax, -1 63 | jmp .END 64 | 65 | endproc 66 | -------------------------------------------------------------------------------- /src/utils/to_negativ.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: to_negativ.asm 4 | ;; Author: chapui_s 5 | ;; Created: 19/07/2014 20:37:22 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .text 14 | 15 | extern my_putnbr 16 | 17 | proc to_negativ, nb, nb_octet 18 | 19 | _var_ result, tmp, decal 20 | 21 | mov dword [result], 0 22 | mov dword [decal], 0 23 | 24 | cmp dword [nb_octet], 4 25 | jne .OR 26 | 27 | mov eax, [nb] 28 | and eax, 0b1000000000000000000000000000000 29 | cmp eax, 0 30 | je .OR 31 | 32 | jmp .DO 33 | 34 | .OR cmp dword [nb_octet], 2 35 | jne .END 36 | 37 | and eax, 0b100000000000000 38 | cmp eax, 0 39 | je .END 40 | 41 | .DO IF dword [nb_octet], e, 4 42 | 43 | .TEST xor eax, eax 44 | mov eax, [nb] 45 | ; xor eax, 0b11111111111111111111111111111111 46 | or eax, 0b10000000000000000000000000000000 47 | ; inc eax 48 | ; neg eax 49 | 50 | ELSEIF dword [nb_octet], e, 2 51 | 52 | xor eax, eax 53 | mov eax, [nb] 54 | xor ax, 0b1111111111111111 55 | inc ax 56 | neg eax 57 | 58 | ENDIF 59 | mov dword [nb], eax 60 | RET 61 | 62 | .END mov eax, [nb] 63 | 64 | endproc 65 | -------------------------------------------------------------------------------- /src/gui/disp_players.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: disp_players.asm 4 | ;; Author: chapui_s 5 | ;; Created: 18/07/2014 18:51:56 (+08:00 UTC) 6 | ;; Updated: 19/07/2014 17:52:51 (+08:00 UTC) 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .data 14 | 15 | str1: db 'error: SDL_BlitSurface', 10, 0 16 | 17 | section .text 18 | 19 | extern SDL_BlitSurface 20 | extern my_putstr 21 | 22 | %define position ebp - 8 23 | 24 | proc disp_players, gui 25 | 26 | sub esp, 8 27 | 28 | pushx edx, ebx, ecx 29 | 30 | lea eax, [position] 31 | mov word [eax + SDL_Rect.x], 100 32 | mov word [eax + SDL_Rect.y], 730 33 | 34 | mov ecx, 0 35 | 36 | .LOOP mov edx, [gui] 37 | mov eax, [gui] 38 | add edx, s_gui.players 39 | add edx, ecx 40 | cmp dword [edx], 0 41 | je .ENDL 42 | 43 | pushx ecx 44 | lea ebx, [position] 45 | invoke SDL_BlitSurface, [edx], 0, [eax + s_gui.screen], ebx 46 | _check_ .FAIL, -1 47 | popx ecx 48 | 49 | lea eax, [position] 50 | add word [eax + SDL_Rect.y], 15 51 | 52 | add ecx, 4 53 | jmp .LOOP 54 | 55 | .FAIL invoke my_putstr, str1 56 | mov eax, -1 57 | jmp .ENDPROC 58 | 59 | .ENDL xor eax, eax 60 | popx edx, ebx, ecx 61 | 62 | endproc -------------------------------------------------------------------------------- /src/utils/check_same_prog_number.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: check_same_prog_number.asm 4 | ;; Author: chapui_s 5 | ;; Created: 5/08/2014 00:37:39 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .data 14 | 15 | str1: db 'error: two programs have the same number', 10, 0 16 | 17 | section .text 18 | 19 | extern my_putstr 20 | 21 | proc check_same_prog_number, list_champion 22 | 23 | _var_ prog_number 24 | 25 | pushx ebx, edx 26 | 27 | mov eax, [list_champion] 28 | 29 | WHILE eax, ne, 0 30 | 31 | mov edx, [eax + s_champions.prog_number] 32 | mov [prog_number], edx 33 | 34 | mov ebx, [eax + s_champions.next] 35 | 36 | WHILE ebx, ne, 0 37 | 38 | mov edx, [ebx + s_champions.prog_number] 39 | 40 | IF [prog_number], e, edx 41 | 42 | IF dword [prog_number], ne, 0 43 | 44 | jmp .FAIL 45 | 46 | ENDIF 47 | 48 | ENDIF 49 | 50 | mov ebx, [ebx + s_champions.next] 51 | 52 | END_WHILE 53 | 54 | mov eax, [eax + s_champions.next] 55 | 56 | END_WHILE 57 | 58 | xor eax, eax 59 | .END popx ebx, edx 60 | RET 61 | 62 | .FAIL invoke my_putstr, str1 63 | mov eax, -1 64 | jmp .END 65 | 66 | endproc 67 | -------------------------------------------------------------------------------- /src/get_dump.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: get_dump.asm 4 | ;; Author: chapui_s 5 | ;; Created: 6/08/2014 00:05:09 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .data 14 | 15 | str1: db '-dump', 0 16 | str2: db '-d', 0 17 | 18 | section .text 19 | 20 | extern my_strcmp 21 | extern my_putstr 22 | 23 | proc get_dump, argc, argv, core 24 | 25 | pushx eax, ecx, edx 26 | 27 | mov ecx, 1 28 | mov eax, [core] 29 | mov dword [eax + s_corewar.nbr_cycle_dump], 0 30 | mov dword [eax + s_corewar.is_desassembler], 0 31 | 32 | WHILE ecx, l, [argc] 33 | 34 | mov eax, [argv] 35 | invoke my_strcmp, [eax + (ecx * 4)], str1 36 | 37 | IF eax, e, 0 38 | 39 | push ecx 40 | 41 | mov eax, [argv] 42 | invoke atoi, [eax + (ecx * 4) + 4] 43 | mov edx, [core] 44 | mov [edx + s_corewar.nbr_cycle_dump], eax 45 | 46 | pop ecx 47 | 48 | ENDIF 49 | 50 | mov eax, [argv] 51 | invoke my_strcmp, [eax + (ecx * 4)], str2 52 | 53 | IF eax, e, 0 54 | 55 | mov eax, [core] 56 | mov dword [eax + s_corewar.is_desassembler], 1 57 | 58 | ENDIF 59 | 60 | inc ecx 61 | 62 | END_WHILE 63 | 64 | popx eax, ecx, edx 65 | 66 | endproc 67 | -------------------------------------------------------------------------------- /src/gui/get_image_path.asm: -------------------------------------------------------------------------------- 1 | 2 | %include "corewar.inc" 3 | 4 | section .data 5 | 6 | str1: db 'Corewar.bmp', 0 7 | str2: db 'arial.ttf', 0 8 | str3: db 'error: SDL_LoadBMP', 10, 0 9 | str4: db 'error: TTF_OpenFont', 10, 0 10 | str5: db 'rb', 0 11 | 12 | section .text 13 | 14 | extern my_putstr 15 | extern SDL_RWFromFile 16 | extern TTF_OpenFont 17 | extern SDL_LoadBMP_RW 18 | 19 | global get_image_path 20 | 21 | get_image_path: 22 | 23 | push ebp 24 | mov ebp, esp 25 | 26 | push edx 27 | 28 | push str5 29 | push str1 30 | call SDL_RWFromFile 31 | add esp, 8 32 | 33 | push dword 1 34 | push eax 35 | call SDL_LoadBMP_RW 36 | add esp, 8 37 | cmp eax, 0 38 | je .FAILL 39 | 40 | mov edx, [ebp + 8] 41 | mov [edx + s_gui.background], eax 42 | 43 | push dword 11 44 | push str2 45 | call TTF_OpenFont 46 | add esp, 8 47 | cmp eax, 0 48 | je .FAILO 49 | 50 | mov edx, [ebp + 8] 51 | mov [edx + s_gui.font], eax 52 | 53 | push dword 13 54 | push str2 55 | call TTF_OpenFont 56 | add esp, 8 57 | cmp eax, 0 58 | je .FAILO 59 | 60 | mov edx, [ebp + 8] 61 | mov [edx + s_gui.font_info], eax 62 | 63 | xor eax, eax 64 | jmp .END 65 | 66 | .FAILL push str3 67 | call my_putstr 68 | add esp, 4 69 | mov eax, -1 70 | jmp .END 71 | 72 | .FAILO push str4 73 | call my_putstr 74 | add esp, 4 75 | mov eax, -1 76 | 77 | .END pop edx 78 | mov esp, ebp 79 | pop ebp 80 | ret 81 | -------------------------------------------------------------------------------- /src/functions/get_second_value.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: get_second_value.asm 4 | ;; Author: chapui_s 5 | ;; Created: 21/07/2014 21:37:07 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .text 14 | 15 | extern is_register 16 | extern is_good_register 17 | extern is_direct 18 | 19 | proc get_second_value, core, champions, instruction 20 | 21 | _var_ second_value, param1 22 | 23 | pushx edx 24 | 25 | mov dword [second_value], 0 26 | mov eax, [instruction] 27 | mov eax, [eax + s_instruction.params + 4] 28 | mov dword [param1], eax 29 | 30 | mov eax, [instruction] 31 | movzx edx, byte [eax + s_instruction.type] 32 | 33 | .IF invoke is_register, edx, 2 34 | cmp eax, 1 35 | jne .ELSEIF 36 | 37 | invoke is_good_register, [param1] 38 | cmp eax, 1 39 | jne .END 40 | 41 | mov eax, [param1] 42 | shl eax, 2 43 | mov edx, [champions] 44 | mov edx, [edx + s_champions.reg] 45 | add edx, eax 46 | mov eax, [edx] 47 | mov dword [second_value], eax 48 | jmp .END 49 | 50 | .ELSEIF invoke is_direct, edx, 2 51 | cmp eax, 1 52 | jne .END 53 | 54 | mov eax, [param1] 55 | mov dword [second_value], eax 56 | 57 | .END mov eax, [second_value] 58 | 59 | popx edx 60 | 61 | endproc -------------------------------------------------------------------------------- /src/utils/attribute_one_def.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: attribute_one_def.asm 4 | ;; Author: chapui_s 5 | ;; Created: 4/08/2014 18:16:42 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .data 14 | 15 | extern mem_size 16 | 17 | section .text 18 | 19 | proc attribute_one_def, champions, place_after 20 | 21 | _var_ place_save 22 | 23 | pushx eax, edx 24 | 25 | mov dword [place_save], 0x0 26 | mov eax, [champions] 27 | 28 | WHILE eax, ne, 0x0 29 | 30 | mov edx, [eax + s_champions.load_address] 31 | 32 | IF edx, ne, 0x0 33 | 34 | mov dword [place_save], edx 35 | 36 | ENDIF 37 | 38 | mov eax, [eax + s_champions.next] 39 | 40 | END_WHILE 41 | 42 | mov eax, [champions] 43 | 44 | WHILE eax, ne, 0x0 45 | 46 | IF dword [eax + s_champions.load_address], e, 0x0 47 | 48 | push eax ; save pour division 49 | 50 | mov eax, [place_save] 51 | add eax, [place_after] 52 | cdq 53 | idiv dword [mem_size] 54 | 55 | pop eax ; restore 56 | 57 | mov [eax + s_champions.load_address], edx 58 | mov [place_save], edx 59 | 60 | ENDIF 61 | 62 | mov eax, [eax + s_champions.next] 63 | 64 | END_WHILE 65 | 66 | popx eax, edx 67 | 68 | endproc 69 | -------------------------------------------------------------------------------- /src/gui/events.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: events.asm 4 | ;; Author: chapui_s 5 | ;; Created: 26/07/2014 13:40:29 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .data 14 | 15 | t_events: 16 | istruc SDL_Event 17 | times 20 db 0 18 | iend 19 | 20 | section .text 21 | 22 | extern SDL_PollEvent 23 | extern TTF_CloseFont 24 | extern SDL_Quit 25 | extern TTF_Quit 26 | 27 | proc manage_event, core, gui, is_pause 28 | 29 | .LOOP invoke SDL_PollEvent, t_events 30 | cmp eax, 0 31 | je .ENDL 32 | 33 | cmp byte [t_events], SDL_QUIT 34 | je .EXIT 35 | 36 | cmp byte [t_events + SDL_KeyboardEvent.keysim + SDL_keysym.sym], SDLK_ESCAPE 37 | je .EXIT 38 | 39 | jmp .PAUSE 40 | 41 | .EXIT mov eax, [gui] 42 | invoke TTF_CloseFont, [eax + s_gui.font] 43 | call SDL_Quit 44 | call TTF_Quit 45 | mov eax, -1 46 | RET 47 | 48 | .PAUSE cmp byte [t_events], SDL_KEYDOWN 49 | jne .NOT 50 | 51 | cmp byte [t_events + SDL_KeyboardEvent.keysim + SDL_keysym.sym], SDLK_SPACE 52 | jne .NOT 53 | 54 | mov eax, [is_pause] 55 | 56 | .TEST cmp dword [eax], 0 57 | je .ONE 58 | 59 | mov dword [eax], 0 60 | jmp .NOT 61 | 62 | .ONE mov dword [eax], 1 63 | 64 | .NOT jmp .LOOP 65 | 66 | .ENDL xor eax, eax 67 | 68 | endproc -------------------------------------------------------------------------------- /src/load_arena.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: load_arena.asm 4 | ;; Author: chapui_s 5 | ;; Created: 6/08/2014 00:59:27 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .data 14 | 15 | str1: db 'error: malloc', 10, 0 16 | 17 | section .text 18 | 19 | extern malloc 20 | extern my_putstr 21 | extern init_arena 22 | extern load_champions_in_arena 23 | 24 | proc load_arena, core 25 | 26 | pushx edx 27 | 28 | mov eax, [core] 29 | 30 | mov dword [eax + s_corewar.nbr_live_cur], 0 31 | mov dword [eax + s_corewar.cycle_to_die_cur], CYCLE_TO_DIE 32 | 33 | invoke malloc, MEM_SIZE 34 | _check_ .FAILM, 0 35 | 36 | mov edx, [core] 37 | mov [edx + s_corewar.arena], eax 38 | 39 | invoke malloc, MEM_SIZE 40 | _check_ .FAILM, 0 41 | 42 | mov edx, [core] 43 | mov [edx + s_corewar.info_arena], eax 44 | 45 | invoke init_arena, [edx + s_corewar.arena], MEM_SIZE, 0 46 | invoke init_arena, [edx + s_corewar.info_arena], MEM_SIZE, 0 47 | 48 | invoke load_champions_in_arena, [edx + s_corewar.arena], [edx + s_corewar.info_arena], edx 49 | _check_ .FAIL, -1 50 | 51 | xor eax, eax 52 | 53 | .END popx edx 54 | RET 55 | 56 | .FAILM invoke my_putstr, str1 57 | 58 | .FAIL mov eax, -1 59 | jmp .END 60 | 61 | endproc 62 | -------------------------------------------------------------------------------- /src/utils/read_arena.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: read_arena.asm 4 | ;; Author: chapui_s 5 | ;; Created: 19/07/2014 22:37:23 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .text 14 | 15 | extern to_negativ 16 | 17 | proc read_arena, core, index, n_to_read 18 | 19 | _var_ value, mem_size 20 | 21 | pushx ecx, edx 22 | 23 | mov dword [mem_size], MEM_SIZE 24 | mov ecx, 0 25 | mov dword [value], 0 26 | 27 | .L1 cmp dword [index], 0 28 | jge .L2 29 | 30 | add dword [index], MEM_SIZE 31 | jmp .L1 32 | 33 | .L2 cmp ecx, [n_to_read] 34 | jge .ENDL2 35 | 36 | shl dword [value], 8 37 | 38 | mov eax, [index] 39 | add eax, ecx 40 | cdq 41 | idiv dword [mem_size] 42 | 43 | mov eax, [core] 44 | mov eax, [eax + s_corewar.arena] 45 | add eax, edx 46 | movzx edx, byte [eax] 47 | 48 | mov eax, [value] 49 | add eax, edx 50 | mov [value], eax 51 | 52 | inc ecx 53 | jmp .L2 54 | 55 | .ENDL2 invoke to_negativ, [value], [n_to_read] 56 | mov [value], eax 57 | 58 | cmp dword [n_to_read], 1 59 | jne .ELSE 60 | 61 | cmp dword [value], 16 62 | jg .DO 63 | 64 | cmp dword [value], 1 65 | jl .DO 66 | 67 | jmp .ELSE 68 | 69 | .DO mov eax, 16 70 | jmp .END 71 | 72 | .ELSE mov eax, [value] 73 | 74 | .END popx ecx, edx 75 | 76 | endproc -------------------------------------------------------------------------------- /src/exec_instructions/get_cycle_to_wait.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: get_cycle_to_wait.asm 4 | ;; Author: chapui_s 5 | ;; Created: 20/07/2014 12:11:09 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .data 14 | 15 | fucking_cycles: 16 | dd 10, 5, 5, 10, 10, 6, 6, 6, 20, 25, 25, 800, 10, 50, 1000, 2 17 | 18 | section .text 19 | 20 | proc get_cycle_to_wait, core, champions 21 | 22 | _var_ index, mem_size 23 | 24 | pushx eax, edx 25 | 26 | mov dword [mem_size], MEM_SIZE 27 | 28 | mov eax, [champions] 29 | mov eax, [eax + s_champions.pc] 30 | xor edx, edx 31 | cmp eax, 0 32 | jge .NO 33 | mov edx, -1 34 | .NO idiv dword [mem_size] 35 | mov [index], edx 36 | 37 | .LOOP cmp dword [index], 0 38 | jge .ENDL 39 | 40 | add dword [index], MEM_SIZE 41 | jmp .LOOP 42 | 43 | .ENDL mov eax, [core] 44 | mov eax, [eax + s_corewar.arena] 45 | add eax, [index] 46 | movzx edx, byte [eax] 47 | 48 | cmp edx, 1 49 | jl .ELSE 50 | 51 | cmp edx, 16 52 | jg .ELSE 53 | 54 | dec edx 55 | shl edx, 2 56 | mov edx, [fucking_cycles + edx] 57 | mov eax, [champions] 58 | mov [eax + s_champions.cycle_to_wait], edx 59 | popx eax, edx 60 | jmp .endproc 61 | 62 | .ELSE mov eax, [champions] 63 | mov dword [eax + s_champions.cycle_to_wait], 0 64 | popx eax, edx 65 | 66 | endproc -------------------------------------------------------------------------------- /src/utils/attribute_address_defined.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: attribute_address_defined.asm 4 | ;; Author: chapui_s 5 | ;; Created: 4/08/2014 17:24:27 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .text 14 | 15 | extern is_addr_already_define 16 | extern attribute_one_def 17 | extern attribute_two_def 18 | 19 | proc attribute_address_defined, champions, nb_champions, place_after 20 | 21 | _var_ nb_already_defined 22 | 23 | pushx edx 24 | 25 | invoke is_addr_already_define, [champions] 26 | mov [nb_already_defined], eax 27 | 28 | IF dword [nb_already_defined], e, 0x1 29 | 30 | invoke attribute_one_def, [champions], [place_after] 31 | 32 | ENDIF 33 | 34 | IF dword [nb_champions], ne, 0x2 35 | 36 | IF dword [nb_already_defined], e, 0x2 37 | 38 | lea eax, [nb_champions - 0x1] 39 | invoke attribute_two_def, [champions], [place_after], eax 40 | 41 | ENDIF 42 | 43 | ENDIF 44 | 45 | IF dword [nb_champions], ne, 0x3 46 | 47 | IF dword [nb_already_defined], e, 0x3 48 | 49 | ; TODO 50 | 51 | ENDIF 52 | 53 | ENDIF 54 | 55 | mov eax, [champions] 56 | 57 | WHILE eax, ne, 0x0 58 | 59 | mov edx, [eax + s_champions.load_address] 60 | mov [eax + s_champions.pc], edx 61 | mov eax, [eax + s_champions.next] 62 | 63 | END_WHILE 64 | 65 | popx edx 66 | xor eax, eax 67 | 68 | endproc 69 | -------------------------------------------------------------------------------- /src/functions/my_xor.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: my_xor.asm 4 | ;; Author: chapui_s 5 | ;; Created: 23/07/2014 22:45:06 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | 12 | %include "corewar.inc" 13 | 14 | section .text 15 | 16 | extern get_value 17 | extern is_good_register 18 | 19 | proc my_xor, core, champions, instruction 20 | 21 | _var_ value1, value2, xor_value, param2 22 | 23 | pushx edx 24 | 25 | mov eax, [instruction] 26 | mov eax, [eax + s_instruction.params + 8] 27 | mov dword [param2], eax 28 | 29 | invoke get_value, [core], [champions], [instruction], 1 30 | mov dword [value1], eax 31 | .TEST nop 32 | invoke get_value, [core], [champions], [instruction], 2 33 | mov dword [value2], eax 34 | nop 35 | 36 | mov eax, [value1] 37 | xor eax, dword [value2] 38 | mov dword [xor_value], eax 39 | 40 | invoke is_good_register, [param2] 41 | cmp eax, 1 42 | jne .CARRY 43 | 44 | mov eax, [param2] 45 | shl eax, 2 46 | ; mov edx, 4 47 | ; mul edx 48 | 49 | mov edx, [champions] 50 | mov edx, [edx + s_champions.reg] 51 | add edx, eax 52 | 53 | mov eax, dword [xor_value] 54 | mov [edx], eax 55 | 56 | .CARRY mov eax, [champions] 57 | cmp dword [xor_value], 0 58 | je .ONE 59 | 60 | mov dword [eax + s_champions.carry], 0 61 | jmp .END 62 | 63 | .ONE mov dword [eax + s_champions.carry], 1 64 | 65 | .END xor eax, eax 66 | popx edx 67 | 68 | endproc -------------------------------------------------------------------------------- /src/gui/get_arena.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: get_arena.asm 4 | ;; Author: chapui_s 5 | ;; Created: 16/07/2014 22:09:30 (+08:00 UTC) 6 | ;; Updated: 26/07/2014 15:02:11 (+08:00 UTC) 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .data 14 | 15 | str1: db 'j = %d\n', 10, 0 16 | 17 | section .text 18 | 19 | extern disp_arena 20 | extern disp_gui 21 | extern manage_instructions 22 | extern manage_event 23 | extern my_showmem 24 | 25 | extern printf 26 | 27 | proc get_arena, core, gui 28 | 29 | _var_ _pause, j 30 | 31 | mov dword [j], 0 32 | mov dword [_pause], 0 33 | 34 | .LOOP mov eax, [j] 35 | cmp eax, 100000 36 | jge .ENDPROC 37 | 38 | lea eax, [_pause] 39 | invoke manage_event, [core], [gui], eax 40 | _check_ .END, -1 41 | 42 | IF dword [_pause], e, 0 43 | 44 | invoke manage_instructions, [core] 45 | _check_ .END, 1 46 | 47 | invoke disp_arena, [core], [gui], [j], 0 48 | _check_ .FAIL, -1 49 | 50 | inc dword [j] 51 | 52 | ELSE 53 | 54 | invoke disp_arena, [core], [gui], [j], 1 55 | 56 | ENDIF 57 | 58 | mov eax, [core] 59 | mov eax, [eax + s_corewar.nbr_cycle_dump] 60 | IF [j], e, eax 61 | 62 | mov eax, [core] 63 | invoke my_showmem, [eax + s_corewar.arena], MEM_SIZE 64 | jmp .endproc 65 | 66 | ENDIF 67 | 68 | jmp .LOOP 69 | 70 | .END xor eax, eax 71 | jmp .endproc 72 | 73 | .FAIL mov eax, -1 74 | 75 | endproc 76 | -------------------------------------------------------------------------------- /src/load_champions_in_arena.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: load_champions_in_arena.asm 4 | ;; Author: chapui_s 5 | ;; Created: 6/08/2014 01:00:28 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .data 14 | 15 | str1: db 'error: too many programmes (max = 4)', 10, 0 16 | 17 | section .text 18 | 19 | extern load_file_in_arena 20 | extern init_values_champions 21 | extern find_max_prog_number 22 | extern my_putstr 23 | extern get_cycle_to_wait 24 | 25 | proc load_champions_in_arena, arena, info_arena, core 26 | 27 | pushx edx 28 | 29 | mov eax, [core] 30 | invoke load_file_in_arena, [arena], [info_arena], [eax + s_corewar.champions] 31 | _check_ .FAIL, -1 32 | 33 | mov eax, [core] 34 | invoke init_values_champions, [eax + s_corewar.champions] 35 | 36 | mov eax, [core] 37 | invoke find_max_prog_number, [eax + s_corewar.champions] 38 | mov edx, [core] 39 | mov [edx + s_corewar.prog_number_max], eax 40 | 41 | mov eax, [edx + s_corewar.champions] 42 | 43 | WHILE eax, ne, 0 44 | 45 | invoke get_cycle_to_wait, [core], eax 46 | mov eax, [eax + s_champions.next] 47 | 48 | END_WHILE 49 | 50 | mov eax, [core] 51 | 52 | cmp dword [eax + s_corewar.nb_champions], 4 53 | jg .FAILN 54 | 55 | xor eax, eax 56 | 57 | .END popx edx 58 | RET 59 | 60 | .FAILN invoke my_putstr, str1 61 | 62 | .FAIL mov eax, -1 63 | jmp .END 64 | 65 | endproc 66 | -------------------------------------------------------------------------------- /src/utils/get_name.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: get_name.asm 4 | ;; Author: chapui_s 5 | ;; Created: 5/08/2014 00:54:28 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .data 14 | 15 | str1: db 'error: malloc', 10, 0 16 | str2: db 'error: read', 10, 0 17 | 18 | section .text 19 | 20 | extern my_putstr 21 | extern malloc 22 | 23 | proc get_name, champions, fd 24 | 25 | _var_ name, i, buf 26 | 27 | pushx ebx, ecx, edx 28 | 29 | invoke malloc, (PROG_NAME_LENGTH + 1) 30 | _check_ .FAILM, 0 31 | 32 | mov [name], eax 33 | mov edx, eax 34 | mov dword [i], 0 35 | xor ecx, ecx 36 | 37 | WHILE dword [i], l, PROG_NAME_LENGTH 38 | 39 | push edx 40 | 41 | mov eax, 3 ; read 42 | mov ebx, [fd] 43 | lea ecx, [buf] 44 | mov edx, 1 45 | int 0x80 46 | 47 | pop edx 48 | 49 | cmp eax, 0 50 | jl .FAILR 51 | jz .AFTER 52 | 53 | IF byte [buf], ne, 0 54 | 55 | mov al, byte [buf] 56 | mov [edx], al 57 | inc edx 58 | 59 | ENDIF 60 | 61 | inc dword [i] 62 | 63 | END_WHILE 64 | 65 | .AFTER mov byte [edx], 0 66 | 67 | mov eax, [champions] 68 | mov edx, [name] 69 | mov [eax + s_champions.name], edx 70 | xor eax, eax 71 | 72 | .END popx ebx, ecx, edx 73 | RET 74 | 75 | .FAILM invoke my_putstr, str1 76 | mov eax, -1 77 | jmp .END 78 | 79 | .FAILR invoke my_putstr, str2 80 | mov eax, -1 81 | jmp .END 82 | 83 | endproc 84 | -------------------------------------------------------------------------------- /src/functions/my_live.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: my_live.asm 4 | ;; Author: chapui_s 5 | ;; Created: 21/07/2014 21:57:17 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .data 14 | 15 | str1: db 'le joueur ', 0 16 | str2: db '(', 0 17 | str3: db ') est en vie', 10, 0 18 | 19 | section .text 20 | 21 | extern my_putstr 22 | extern my_putnbr 23 | extern puts 24 | 25 | proc im_alive, name , nb 26 | 27 | invoke my_putstr, str1 28 | invoke my_putnbr, [nb] 29 | invoke my_putstr, str2 30 | invoke my_putstr, [name] 31 | invoke my_putstr, str3 32 | 33 | endproc 34 | 35 | proc my_live, core, champions, instruction 36 | 37 | _var_ color 38 | 39 | pushx edx 40 | 41 | mov eax, [champions] 42 | mov eax, [eax + s_champions.color_gui] 43 | mov dword [color], eax 44 | 45 | mov eax, [instruction] 46 | mov eax, [eax + s_instruction.params] 47 | 48 | cmp dword [color], eax 49 | jne .END 50 | 51 | mov eax, [champions] 52 | invoke im_alive, [eax + s_champions.name], [color] 53 | 54 | mov dword [eax + s_champions.last_live], 0 55 | mov eax, [core] 56 | mov edx, [color] 57 | mov dword [eax + s_corewar.last_live_nb], edx 58 | mov edx, [champions] 59 | mov edx, [edx + s_champions.name] 60 | mov dword [eax + s_corewar.last_live_name], edx 61 | inc dword [eax + s_corewar.nbr_live_cur] 62 | 63 | .END xor eax, eax 64 | 65 | popx edx 66 | 67 | endproc -------------------------------------------------------------------------------- /src/utils/write_arena_two.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: write_arena_two.asm 4 | ;; Author: chapui_s 5 | ;; Created: 19/07/2014 23:03:41 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .text 14 | 15 | proc write_arena_two, core, champions, to_write, index 16 | 17 | _var_ mem_size, index_mod, index_one_mod 18 | 19 | pushx eax, edx 20 | 21 | mov dword [mem_size], MEM_SIZE 22 | 23 | mov eax, [index] 24 | mov edx, 0 25 | div dword [mem_size] 26 | mov [index_mod], edx 27 | 28 | mov eax, [index] 29 | inc eax 30 | mov edx, 0 31 | div dword [mem_size] 32 | mov [index_one_mod], edx 33 | 34 | mov eax, [core] 35 | mov eax, [eax + s_corewar.arena] 36 | add eax, dword [index_mod] 37 | mov edx, [to_write] 38 | shr edx, 8 39 | mov byte [eax], dl 40 | 41 | mov eax, [core] 42 | mov eax, [eax + s_corewar.info_arena] 43 | add eax, dword [index_mod] 44 | mov edx, [champions] 45 | mov edx, [edx + s_champions.prog_number] 46 | mov byte [eax], dl 47 | 48 | mov eax, [core] 49 | mov eax, [eax + s_corewar.arena] 50 | add eax, dword [index_one_mod] 51 | mov edx, [to_write] 52 | and edx, 0b11111111 53 | mov byte [eax], dl 54 | 55 | mov eax, [core] 56 | mov eax, [eax + s_corewar.info_arena] 57 | add eax, dword [index_one_mod] 58 | mov edx, [champions] 59 | mov edx, [edx + s_champions.prog_number] 60 | mov byte [eax], dl 61 | 62 | popx eax, edx 63 | 64 | endproc -------------------------------------------------------------------------------- /src/utils/get_comment.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: get_comment.asm 4 | ;; Author: chapui_s 5 | ;; Created: 5/08/2014 00:54:28 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .data 14 | 15 | str1: db 'error: malloc', 10, 0 16 | str2: db 'error: read', 10, 0 17 | 18 | section .text 19 | 20 | extern my_putstr 21 | extern malloc 22 | 23 | proc get_comment, champions, fd 24 | 25 | _var_ comment, i, buf 26 | 27 | pushx ebx, ecx, edx 28 | 29 | invoke malloc, (COMMENT_LENGTH + 1) 30 | _check_ .FAILM, 0 31 | 32 | mov [comment], eax 33 | mov edx, eax 34 | mov dword [i], 0 35 | xor ecx, ecx 36 | 37 | WHILE dword [i], l, COMMENT_LENGTH 38 | 39 | push edx 40 | 41 | mov eax, 3 ; read 42 | mov ebx, [fd] 43 | lea ecx, [buf] 44 | mov edx, 1 45 | int 0x80 46 | 47 | pop edx 48 | 49 | cmp eax, 0 50 | jl .FAILR 51 | jz .AFTER 52 | 53 | IF byte [buf], ne, 0 54 | 55 | mov al, byte [buf] 56 | mov [edx], al 57 | inc edx 58 | 59 | ENDIF 60 | 61 | inc dword [i] 62 | 63 | END_WHILE 64 | 65 | .AFTER mov byte [edx], 0 66 | 67 | mov eax, [champions] 68 | mov edx, [comment] 69 | mov [eax + s_champions.comment], edx 70 | xor eax, eax 71 | 72 | .END popx ebx, ecx, edx 73 | RET 74 | 75 | .FAILM invoke my_putstr, str1 76 | mov eax, -1 77 | jmp .END 78 | 79 | .FAILR invoke my_putstr, str2 80 | mov eax, -1 81 | jmp .END 82 | 83 | endproc 84 | -------------------------------------------------------------------------------- /src/main.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: main.asm 4 | ;; Author: chapui_s 5 | ;; Created: 6/08/2014 01:48:15 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .data 14 | 15 | t_corewar: 16 | istruc s_corewar 17 | at s_corewar.arena, dd 0 18 | at s_corewar.info_arena, dd 0 19 | at s_corewar.nb_champions, dd 0 20 | at s_corewar.champions, dd 0 21 | at s_corewar.last_champions, dd 0 22 | at s_corewar.last_live_nb, dd 0 23 | at s_corewar.last_live_name, dd 0 24 | at s_corewar.prog_number_max, dd 0 25 | at s_corewar.nbr_cycle_dump, dd 0 26 | at s_corewar.nbr_live_cur, dd 0 27 | at s_corewar.is_desassembler, dd 0 28 | at s_corewar.cycle_to_die_cur, dd 0 29 | iend 30 | 31 | global mem_size 32 | global idx_mod 33 | 34 | mem_size: dd MEM_SIZE 35 | idx_mod: dd IDX_MOD 36 | 37 | section .text 38 | 39 | extern get_args 40 | extern load_arena 41 | extern my_gui 42 | extern desassemble_it 43 | 44 | proc main, argc, argv 45 | 46 | invoke get_args, [argc], [argv], t_corewar 47 | _check_ .FAIL, -1 48 | 49 | invoke load_arena, t_corewar 50 | _check_ .FAIL, -1 51 | 52 | IF dword [t_corewar + s_corewar.is_desassembler], e, 1 53 | 54 | invoke desassemble_it, t_corewar 55 | 56 | ELSE 57 | 58 | mov eax, [argv] 59 | invoke my_gui, t_corewar, [eax] 60 | _check_ .FAIL, -1 61 | 62 | ENDIF 63 | 64 | xor eax, eax 65 | RET 66 | 67 | .FAIL mov eax, -1 68 | 69 | endproc 70 | -------------------------------------------------------------------------------- /src/exec_instructions/exec_instructions.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: exec_instructions.asm 4 | ;; Author: chapui_s 5 | ;; Created: 20/07/2014 12:36:59 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .data 14 | 15 | instruction: 16 | istruc s_instruction 17 | times 10 db 10 18 | iend 19 | 20 | str1: db '&instruction >> %x <<', 10, 0 21 | 22 | section .text 23 | 24 | extern get_instruction 25 | extern exec_function 26 | extern get_cycle_to_wait 27 | 28 | proc exec_instructions, core, champions 29 | 30 | _var_ tmp 31 | 32 | pushx edx 33 | 34 | mov eax, [champions] 35 | mov [tmp], eax 36 | 37 | .LOOP cmp dword [tmp], 0 38 | je .ENDL 39 | 40 | mov eax, [tmp] 41 | mov eax, [eax + s_champions.cycle_to_wait] 42 | 43 | .TEST IF eax, le, 1 44 | 45 | lea eax, [instruction] 46 | invoke get_instruction, [core], [tmp], eax 47 | 48 | lea eax, [instruction] 49 | invoke exec_function, [core], [tmp], eax 50 | _check_ .FAIL, -1 51 | 52 | invoke get_cycle_to_wait, [core], [tmp] 53 | 54 | ELSE 55 | 56 | mov eax, [tmp] 57 | dec dword [eax + s_champions.cycle_to_wait] 58 | 59 | ENDIF 60 | 61 | mov eax, [tmp] 62 | inc dword [eax + s_champions.last_live] 63 | 64 | mov edx, [eax + s_champions.next] 65 | cmp eax, edx 66 | jz .ENDL 67 | 68 | mov eax, [tmp] 69 | mov eax, [eax + s_champions.next] 70 | mov dword [tmp], eax 71 | jmp .LOOP 72 | 73 | 74 | .ENDL xor eax, eax 75 | 76 | .END popx edx 77 | RET 78 | 79 | .FAIL mov eax, -1 80 | jmp .END 81 | 82 | endproc -------------------------------------------------------------------------------- /src/exec_instructions/exec_function.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: exec_function.asm 4 | ;; Author: chapui_s 5 | ;; Created: 20/07/2014 11:15:20 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | extern my_live 14 | extern my_ld 15 | extern my_st 16 | extern my_add 17 | extern my_sub 18 | extern my_and 19 | extern my_or 20 | extern my_xor 21 | extern my_zjmp 22 | extern my_ldi 23 | extern my_sti 24 | extern my_fork 25 | extern my_lld 26 | extern my_lldi 27 | extern my_lfork 28 | 29 | extern disp_carry 30 | 31 | section .data 32 | 33 | t_functions: 34 | dd 0x01, my_live, \ 35 | 0x02, my_ld, \ 36 | 0x03, my_st, \ 37 | 0x04, my_add, \ 38 | 0x05, my_sub, \ 39 | 0x06, my_and, \ 40 | 0x07, my_or, \ 41 | 0x08, my_xor, \ 42 | 0x09, my_zjmp, \ 43 | 0x0A, my_ldi, \ 44 | 0x0B, my_sti, \ 45 | 0x0C, my_fork, \ 46 | 0x0D, my_lld, \ 47 | 0x0E, my_lldi, \ 48 | 0x0F, my_lfork, \ 49 | 0x10, 0x0 50 | 51 | section .text 52 | 53 | proc exec_function, core, champions, instruction 54 | 55 | ; pushx eax, ebx, ecx, edx 56 | ; invoke disp_carry, [champions] 57 | ; popx eax, ebx, ecx, edx 58 | 59 | mov eax, [instruction] 60 | movzx eax, byte [eax + s_instruction.code] 61 | 62 | cmp eax, REG_NUMBER 63 | jg .END 64 | 65 | cmp eax, 0 66 | jle .END 67 | 68 | dec eax 69 | shl eax, 3 70 | add eax, t_functions 71 | add eax, 4 72 | 73 | invoke [eax], [core], [champions], [instruction] 74 | _check_ .FAIL, -1 75 | 76 | .END xor eax, eax 77 | jmp .endproc 78 | 79 | .FAIL mov eax, -1 80 | 81 | endproc 82 | -------------------------------------------------------------------------------- /src/exec_instructions/check_live_process.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: check_live_process.asm 4 | ;; Author: chapui_s 5 | ;; Created: 19/07/2014 19:26:44 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .text 14 | 15 | extern check_first_in_list 16 | extern free 17 | extern search_who_still_alive 18 | 19 | proc check_live_process, core, cycle_to_die_cur 20 | 21 | _var_ tmp, tmp_to_rm 22 | 23 | pushx edx 24 | 25 | invoke check_first_in_list, [core], [cycle_to_die_cur] 26 | 27 | mov eax, [core] 28 | mov eax, [eax + s_corewar.champions] 29 | mov [tmp], eax 30 | 31 | .LOOP cmp dword [tmp], 0 32 | je .ENDL 33 | 34 | mov edx, [cycle_to_die_cur] 35 | 36 | mov eax, [tmp] 37 | mov eax, [eax + s_champions.next] 38 | cmp eax, 0 39 | je .ELSE 40 | 41 | cmp dword [eax + s_champions.last_live], edx 42 | jl .ELSE 43 | 44 | mov [tmp_to_rm], eax 45 | mov eax, [eax + s_champions.next] 46 | mov edx, [tmp] 47 | mov [edx + s_champions.next], eax 48 | 49 | cmp dword [edx + s_champions.next], 0 50 | jne .NOT 51 | 52 | mov eax, [core] 53 | mov dword [eax + s_corewar.last_champions], edx 54 | 55 | .NOT mov eax, [tmp_to_rm] 56 | 57 | mov eax, [core] 58 | dec dword [eax + s_corewar.nb_champions] 59 | jmp .LOOP 60 | 61 | .ELSE mov eax, [tmp] 62 | mov eax, [eax + s_champions.next] 63 | mov [tmp], eax 64 | jmp .LOOP 65 | 66 | .ENDL invoke search_who_still_alive, [core] 67 | cmp eax, 0 68 | je .ONE 69 | 70 | xor eax, eax 71 | jmp .END 72 | 73 | .ONE mov eax, 1 74 | 75 | .END popx edx 76 | 77 | endproc -------------------------------------------------------------------------------- /src/save_champion.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: save_champion.asm 4 | ;; Author: chapui_s 5 | ;; Created: 6/08/2014 02:01:34 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .data 14 | 15 | str1: db '-n', 0 16 | str2: db '-a', 0 17 | mem_size: dd MEM_SIZE 18 | 19 | section .text 20 | 21 | extern push_champion 22 | extern is_file_dot_cor 23 | extern my_strcmp 24 | extern atoi 25 | 26 | proc save_champion, i, argv, core 27 | 28 | _var_ cur, prog_number, load_address 29 | 30 | pushx ecx, edx 31 | 32 | mov ecx, [i] 33 | mov [cur], ecx 34 | dec ecx 35 | mov dword [prog_number], 0 36 | mov dword [load_address], 0 37 | 38 | WHILE ecx, g, 0 39 | 40 | mov eax, [argv] 41 | invoke is_file_dot_cor, [eax + (ecx * 4)] 42 | _check_ .ENDL, -1 43 | 44 | mov eax, [argv] 45 | invoke my_strcmp, [eax + (ecx * 4)], str1 46 | 47 | IF eax, e, 0 48 | 49 | invoke atoi, [eax + (ecx * 4) + 4] 50 | mov [prog_number], eax 51 | 52 | ENDIF 53 | 54 | mov eax, [argv] 55 | invoke my_strcmp, [eax + (ecx * 4)], str2 56 | 57 | IF eax, e, 0 58 | 59 | invoke atoi, [eax + (ecx * 4) + 4] 60 | cdq 61 | idiv dword [mem_size] 62 | mov [load_address], edx 63 | 64 | ENDIF 65 | 66 | dec ecx 67 | 68 | END_WHILE 69 | 70 | .ENDL mov eax, [argv] 71 | mov edx, [cur] 72 | invoke push_champion, [core], [eax + (edx * 4)], [prog_number], [load_address] 73 | _check_ .FAIL, -1 74 | 75 | xor eax, eax 76 | 77 | .END popx ecx, edx 78 | RET 79 | 80 | .FAIL mov eax, -1 81 | jmp .END 82 | 83 | endproc 84 | -------------------------------------------------------------------------------- /src/desassembleur/my_desassembler.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: my_desassembler.asm 4 | ;; Author: chapui_s 5 | ;; Created: 6/08/2014 18:44:49 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .data 14 | 15 | str1: db '.name', 9, 9, '"', 0 16 | str2: db '.comment', 9, '"', 0 17 | str3: db '"', 10, 0 18 | str4: db 10, 0 19 | 20 | inst: 21 | istruc s_instruction 22 | times 18 db 0 23 | iend 24 | 25 | section .text 26 | 27 | extern my_putstr 28 | extern get_instruction 29 | extern my_putchar 30 | extern print_instruction 31 | 32 | proc my_desassembler, core, champions 33 | 34 | _var_ pc_base, instruction 35 | 36 | pushx eax, ebx, edx 37 | 38 | mov eax, [champions] 39 | mov eax, [eax + s_champions.pc] 40 | mov [pc_base], eax 41 | 42 | mov eax, [champions] 43 | 44 | IF dword [eax + s_champions.name], ne, 0 45 | 46 | invoke my_putstr, str1 47 | invoke my_putstr, [eax + s_champions.name] 48 | invoke my_putstr, str3 49 | 50 | ENDIF 51 | 52 | IF dword [eax + s_champions.comment], ne, 0 53 | 54 | invoke my_putstr, str2 55 | invoke my_putstr, [eax + s_champions.comment] 56 | invoke my_putstr, str3 57 | 58 | ENDIF 59 | 60 | mov edx, [pc_base] 61 | add edx, [eax + s_champions.size] 62 | 63 | WHILE dword [eax + s_champions.pc], l, edx 64 | 65 | lea ebx, [inst] 66 | invoke get_instruction, [core], [champions], ebx 67 | lea ebx, [inst] 68 | invoke print_instruction, ebx 69 | invoke my_putchar, 10 70 | 71 | END_WHILE 72 | 73 | xor eax, eax 74 | 75 | popx eax, ebx, edx 76 | 77 | endproc 78 | -------------------------------------------------------------------------------- /src/exec_instructions/manage_instructions.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: manage_instructions.asm 4 | ;; Author: chapui_s 5 | ;; Created: 20/07/2014 12:46:57 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .data 14 | 15 | i: dd 0 16 | cycle_to_die_cur: dd 0 17 | str1: db 'i = %d', 10, 0 18 | 19 | section .text 20 | 21 | extern check_live_process 22 | extern exec_instructions 23 | 24 | proc manage_instructions, core 25 | 26 | pushx edx 27 | 28 | 29 | ; pushx eax, ebx, ecx, edx 30 | ; invoke printf, str1, [i] 31 | ; popx eax, ebx, ecx, edx 32 | 33 | 34 | IF dword [i], e, 0 35 | 36 | mov dword [i], 1 37 | 38 | ENDIF 39 | 40 | IF dword [cycle_to_die_cur], e, 0 41 | 42 | mov dword [cycle_to_die_cur], CYCLE_TO_DIE 43 | 44 | ENDIF 45 | 46 | mov eax, [i] 47 | IF eax, ge, [cycle_to_die_cur] 48 | 49 | invoke check_live_process, [core], [cycle_to_die_cur] 50 | _check_ .ONE, 1 51 | mov dword [i], 1 52 | 53 | ENDIF 54 | 55 | mov eax, [core] 56 | IF dword [eax + s_corewar.nbr_live_cur] , ge, NBR_LIVE 57 | 58 | sub dword [cycle_to_die_cur], CYCLE_DELTA 59 | mov edx, [cycle_to_die_cur] 60 | mov dword [eax + s_corewar.cycle_to_die_cur], edx 61 | mov dword [eax + s_corewar.nbr_live_cur], 0 62 | 63 | ENDIF 64 | 65 | inc dword [i] 66 | 67 | mov eax, [core] 68 | mov eax, [eax + s_corewar.champions] 69 | invoke exec_instructions, [core], eax 70 | _check_ .FAIL, -1 71 | 72 | xor eax, eax 73 | jmp .END 74 | 75 | .ONE mov eax, 1 76 | jmp .END 77 | 78 | .FAIL mov eax, -1 79 | 80 | .END popx edx 81 | 82 | endproc -------------------------------------------------------------------------------- /src/functions/my_and.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: my_and.asm 4 | ;; Author: chapui_s 5 | ;; Created: 21/07/2014 20:06:39 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .data 14 | 15 | str1: db 'AND v1 = %d v2 = %d reg = %d', 10, 0 16 | 17 | section .text 18 | 19 | extern get_value 20 | extern is_good_register 21 | 22 | proc my_and, core, champions, instruction 23 | 24 | _var_ value1, value2 25 | 26 | pushx ecx, edx 27 | 28 | invoke get_value, [core], [champions], [instruction], 1 29 | mov [value1], eax 30 | 31 | invoke get_value, [core], [champions], [instruction], 2 32 | mov [value2], eax 33 | 34 | mov eax, [value1] 35 | and eax, dword [value2] 36 | mov ecx, eax 37 | 38 | ; pushx eax, ebx, ecx, edx 39 | ; invoke printf, str1, [value1], [value2], 111 40 | ; popx eax, ebx, ecx, edx 41 | 42 | 43 | mov eax, [instruction] 44 | mov eax, [eax + s_instruction.params + 8] 45 | mov edx, eax 46 | invoke is_good_register, eax 47 | cmp eax, 1 48 | jne .NO 49 | 50 | mov eax, [champions] 51 | .TEST mov eax, [eax + s_champions.reg] 52 | 53 | mov edx, [instruction] 54 | mov edx, [edx + s_instruction.params + 8] 55 | shl edx, 2 56 | mov [eax + edx], ecx 57 | 58 | ; mov [eax + (edx * 4)], ecx 59 | 60 | ; pushx eax, ebx, ecx, edx 61 | ; invoke printf, str1, [value1], [value2], [eax + edx] 62 | ; popx eax, ebx, ecx, edx 63 | 64 | .NO cmp ecx, 0 65 | je .ONE 66 | 67 | mov edx, 0 68 | jmp .END 69 | 70 | .ONE mov edx, 1 71 | 72 | .END mov eax, [champions] 73 | mov [eax + s_champions.carry], edx 74 | 75 | xor eax, eax 76 | 77 | popx ecx, edx 78 | 79 | endproc -------------------------------------------------------------------------------- /src/load_file_in_arena.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: load_file_in_arena.asm 4 | ;; Author: chapui_s 5 | ;; Created: 6/08/2014 01:10:30 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .text 14 | 15 | extern get_name_comment_champions 16 | extern read 17 | extern check_place_arena 18 | extern check_size_read 19 | extern my_putstr 20 | 21 | proc load_file_in_arena, arena, info_arena, champions 22 | 23 | _var_ fd, buf, size, i, s_read 24 | 25 | pushx ebx, ecx, edx 26 | 27 | mov ebx, [champions] 28 | 29 | WHILE ebx, ne, 0 30 | 31 | mov dword [size], 0 32 | mov eax, [ebx + s_champions.load_address] 33 | mov [i], eax 34 | 35 | lea eax, [fd] 36 | invoke get_name_comment_champions, ebx, eax 37 | _check_ .FAIL, -1 38 | 39 | .LOOP push ebx 40 | 41 | mov eax, 3 42 | mov ebx, [fd] 43 | lea ecx, [buf] 44 | mov edx, 1 45 | int 0x80 46 | 47 | pop ebx 48 | 49 | mov [s_read], eax 50 | cmp eax, 0 51 | jle .ENDL 52 | 53 | inc dword [size] 54 | 55 | mov eax, [arena] 56 | mov ecx, [i] 57 | mov edx, [buf] 58 | mov byte [eax + ecx], dl 59 | 60 | lea eax, [i] 61 | invoke check_place_arena, [info_arena], [ebx + s_champions.prog_number], eax 62 | _check_ .FAIL, -1 63 | 64 | IF dword [i], e, (MEM_SIZE - 1) 65 | 66 | mov dword [i], 0 67 | 68 | ENDIF 69 | 70 | jmp .LOOP 71 | 72 | .ENDL invoke check_size_read, [size], ebx, [fd], [s_read] 73 | _check_ .FAIL, -1 74 | 75 | mov ebx, [ebx + s_champions.next] 76 | 77 | END_WHILE 78 | 79 | xor eax, eax 80 | 81 | .END popx ebx, ecx, edx 82 | RET 83 | 84 | .FAIL mov eax, -1 85 | jmp .END 86 | 87 | endproc 88 | -------------------------------------------------------------------------------- /src/exec_instructions/fulfil_dir.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: fulfil_dir.asm 4 | ;; Author: chapui_s 5 | ;; Created: 19/07/2014 23:40:11 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .text 14 | 15 | extern read_arena 16 | extern is_special 17 | 18 | proc fulfil_dir, core, champion, instruction, decal 19 | 20 | _var_ to_return, code, decal_int 21 | 22 | pushx edx 23 | 24 | mov dword [to_return], 0 25 | mov eax, [instruction] 26 | movzx edx, byte [eax + s_instruction.code] 27 | mov [code], edx 28 | 29 | mov eax, [decal] 30 | mov eax, [eax] 31 | mov [decal_int], eax 32 | 33 | .IF cmp edx, LDI 34 | je .ELSE 35 | 36 | cmp edx, STI 37 | je .ELSE 38 | 39 | cmp edx, LLDI 40 | je .ELSE 41 | 42 | invoke read_arena, [core], [decal_int], 4 43 | mov [to_return], eax 44 | 45 | mov eax, [decal_int] 46 | add eax, 4 47 | mov edx, [decal] 48 | mov [edx], eax 49 | jmp .END 50 | 51 | .ELSE invoke is_special, [champion], [instruction], [decal_int] 52 | .IF2 cmp eax, 1 53 | jne .ELSE2 54 | 55 | invoke read_arena, [core], [decal_int], 2 56 | mov [to_return], eax 57 | 58 | mov eax, [decal_int] 59 | add eax, 2 60 | mov edx, [decal] 61 | mov [edx], eax 62 | jmp .END 63 | 64 | .ELSE2 invoke read_arena, [core], [decal_int], 4 65 | mov [to_return], eax 66 | 67 | mov eax, [decal_int] 68 | add eax, 4 69 | mov edx, [decal] 70 | mov [edx], eax 71 | 72 | .END mov eax, [to_return] 73 | 74 | popx edx 75 | 76 | endproc -------------------------------------------------------------------------------- /src/utils/get_size.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: get_size.asm 4 | ;; Author: chapui_s 5 | ;; Created: 5/08/2014 01:33:09 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .data 14 | 15 | str1: db 'error: read', 10, 0 16 | str2: db 'error while loading file', 10, 0 17 | str3: db 'error: size indicate in file different from true size, maybe the file is corrupt: ', 0 18 | str4: db 10, 0 19 | 20 | section .text 21 | 22 | extern close 23 | extern my_putstr 24 | extern read 25 | 26 | proc check_size_read, size, champions1, fd1, s_read 27 | 28 | invoke close, [fd1] 29 | 30 | IF dword [s_read], e, -1 31 | 32 | invoke my_putstr, str1 33 | mov eax, -1 34 | jmp .FAIL 35 | 36 | ENDIF 37 | 38 | IF dword [champions1], e, 0 39 | 40 | invoke my_putstr, str2 41 | jmp .FAIL 42 | 43 | ENDIF 44 | 45 | mov eax, [champions1] 46 | mov eax, [eax + s_champions.size] 47 | 48 | IF [size], ne, eax 49 | 50 | invoke my_putstr, str3 51 | invoke my_putstr, [eax + s_champions.filename] 52 | invoke my_putstr, str4 53 | jmp .FAIL 54 | 55 | ENDIF 56 | 57 | .END xor eax, eax 58 | RET 59 | 60 | .FAIL mov eax, -1 61 | 62 | endproc 63 | 64 | proc get_size, champions, fd 65 | 66 | _var_ buf 67 | 68 | pushx ebx, ecx, edx 69 | 70 | mov dword [buf], 0 71 | 72 | mov eax, 3 73 | mov ebx, [fd] 74 | lea ecx, [buf] 75 | mov edx, 4 76 | int 0x80 77 | _check_ .FAIL, -1 78 | 79 | mov eax, [buf] 80 | 81 | bswap eax ; putin d'instruction 82 | 83 | mov edx, [champions] 84 | mov [edx + s_champions.size], eax 85 | 86 | xor eax, eax 87 | .END popx ebx, ecx, edx 88 | RET 89 | 90 | .FAIL invoke my_putstr, str1 91 | mov eax, -1 92 | jmp .END 93 | 94 | endproc 95 | -------------------------------------------------------------------------------- /src/list_champions.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: list_champions.asm 4 | ;; Author: chapui_s 5 | ;; Created: 6/08/2014 00:52:34 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .data 14 | 15 | str_malloc_error: db MALLOC_FAILED 16 | 17 | section .text 18 | 19 | extern my_putstr 20 | extern malloc 21 | 22 | proc create_champion, file, prog_n, load_a 23 | 24 | pushx edx 25 | 26 | invoke malloc, SIZE_S_CHAMPIONS 27 | _check_ .FAIL, 0 28 | 29 | mov edx, [file] 30 | mov [eax + s_champions.filename], edx 31 | 32 | mov edx, [prog_n] 33 | mov [eax + s_champions.prog_number], edx 34 | mov [eax + s_champions.color_gui], edx 35 | 36 | mov edx, [load_a] 37 | mov [eax + s_champions.load_address], edx 38 | mov dword [eax + s_champions.next], 0 39 | 40 | .END popx edx 41 | RET 42 | 43 | .FAIL invoke my_putstr, str_malloc_error 44 | xor eax, eax 45 | jmp .END 46 | 47 | endproc 48 | 49 | proc push_champion, core, filename, prog_number, load_address 50 | 51 | pushx edx 52 | 53 | invoke create_champion, [filename], [prog_number], [load_address] 54 | _check_ .FAIL, 0 55 | 56 | mov edx, eax 57 | 58 | mov eax, [core] 59 | mov [eax + s_corewar.last_champions], edx 60 | 61 | IF dword [eax + s_corewar.champions], ne, 0 62 | 63 | mov eax, [eax + s_corewar.champions] 64 | 65 | WHILE dword [eax + s_champions.next], ne, 0 66 | 67 | mov eax, [eax + s_champions.next] 68 | 69 | END_WHILE 70 | 71 | lea eax, [eax + s_champions.next] 72 | 73 | ELSE 74 | 75 | lea eax, [eax + s_corewar.champions] 76 | 77 | ENDIF 78 | 79 | mov [eax], edx 80 | 81 | xor eax, eax 82 | 83 | .END popx edx 84 | RET 85 | 86 | .FAIL mov eax, -1 87 | jmp .END 88 | 89 | endproc 90 | -------------------------------------------------------------------------------- /src/functions/my_lld.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: my_lld.asm 4 | ;; Author: chapui_s 5 | ;; Created: 21/07/2014 22:04:44 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .text 14 | 15 | extern read_arena 16 | extern is_indirect 17 | extern is_direct 18 | extern is_good_register 19 | 20 | proc my_lld, core, champions, instruction 21 | 22 | _var_ value_to_load, pc, param0 23 | 24 | pushx edx 25 | 26 | mov dword [value_to_load], 0 27 | 28 | mov eax, [champions] 29 | mov eax, [eax + s_champions.pc] 30 | mov [pc], eax 31 | 32 | mov eax, [instruction] 33 | mov eax, [eax + s_instruction.params] 34 | mov dword [param0], eax 35 | 36 | mov eax, [instruction] 37 | movzx edx, byte [eax + s_instruction.type] 38 | 39 | .IF invoke is_indirect, edx, 1 40 | cmp eax, 1 41 | jne .ELSEIF 42 | 43 | mov eax, [pc] 44 | add eax, dword [param0] 45 | invoke read_arena, [core], eax, 4 46 | mov dword [value_to_load], eax 47 | jmp .THEN 48 | 49 | .ELSEIF invoke is_direct, edx, 1 50 | cmp eax, 1 51 | jne .THEN 52 | 53 | mov eax, [param0] 54 | mov dword [value_to_load], eax 55 | 56 | .THEN mov eax, [instruction] 57 | mov eax, [eax + s_instruction.params + 4] 58 | mov edx, eax 59 | invoke is_good_register, eax 60 | cmp eax, 1 61 | jne .CARRY 62 | 63 | mov eax, edx 64 | shl eax, 2 65 | mov edx, [champions] 66 | mov edx, [edx + s_champions.reg] 67 | add edx, eax 68 | 69 | mov eax, [value_to_load] 70 | mov [edx], eax 71 | 72 | .CARRY cmp dword [value_to_load], 0 73 | je .ONE 74 | 75 | xor eax, eax 76 | jmp .END 77 | 78 | .ONE mov eax, 1 79 | 80 | .END mov edx, [champions] 81 | mov [edx + s_champions.carry], eax 82 | 83 | xor eax, eax 84 | popx edx 85 | 86 | endproc -------------------------------------------------------------------------------- /src/functions/my_lldi.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: my_lldi.asm 4 | ;; Author: chapui_s 5 | ;; Created: 21/07/2014 22:14:37 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .text 14 | 15 | extern get_first_value 16 | extern get_second_value 17 | extern read_arena 18 | extern is_register 19 | extern is_good_register 20 | 21 | proc my_lldi, core, champions, instruction 22 | 23 | _var_ first_value, read_value, pc, param2, reg_ptr 24 | 25 | pushx edx 26 | 27 | mov eax, [champions] 28 | mov eax, [eax + s_champions.reg] 29 | mov dword [reg_ptr], eax 30 | 31 | mov eax, [champions] 32 | mov eax, [eax + s_champions.pc] 33 | mov dword [pc], eax 34 | 35 | mov eax, [instruction] 36 | mov eax, [eax + s_instruction.params + 0x8] 37 | mov dword [param2], eax 38 | 39 | invoke get_first_value, [core], [champions], [instruction], 0x1 40 | mov dword [first_value], eax 41 | 42 | invoke get_second_value, [core], [champions], [instruction] 43 | 44 | add eax, [first_value] 45 | add eax, [pc] 46 | sub eax, 0x6 47 | 48 | invoke read_arena, [core], eax, 0x4 49 | mov dword [read_value], eax 50 | 51 | mov eax, [instruction] 52 | movzx eax, byte [eax + s_instruction.type] 53 | invoke is_register, eax, 0x3 54 | cmp eax, 0x1 55 | jne .END 56 | 57 | invoke is_good_register, [param2] 58 | cmp eax, 0x1 59 | jne .END 60 | 61 | mov eax, dword [param2] 62 | shl eax, 0x2 63 | add eax, dword [reg_ptr] 64 | 65 | mov edx, [read_value] 66 | mov [eax], edx 67 | 68 | cmp edx, 0 69 | je .ONE 70 | 71 | xor edx, edx 72 | jmp .CARRY 73 | 74 | .ONE mov edx, 1 75 | 76 | .CARRY mov eax, [champions] 77 | lea eax, [eax + s_champions.carry] 78 | mov [eax], edx 79 | 80 | .END xor eax, eax 81 | popx edx 82 | 83 | endproc -------------------------------------------------------------------------------- /src/gui/print_bytes.asm: -------------------------------------------------------------------------------- 1 | 2 | %include "corewar.inc" 3 | 4 | section .data 5 | 6 | str1: db 'error: SDL_BlitSurface', 10, 0 7 | 8 | str2: db 'cur = %d', 10, 0 9 | 10 | section .text 11 | 12 | extern get_color 13 | extern SDL_UpperBlit ;SDL_BlitSurface 14 | extern SDL_FreeSurface 15 | extern my_putstr 16 | 17 | extern printf 18 | 19 | global print_bytes 20 | 21 | print_bytes: 22 | 23 | push ebp 24 | .TE mov ebp, esp 25 | 26 | sub esp, 8 27 | ; [ebp - 8] position (2 * dword) 28 | 29 | mov dword [ebp - 4], 0 30 | mov dword [ebp - 8], 0 31 | mov ecx, 0 32 | 33 | .LOOP cmp ecx, MEM_SIZE 34 | jge .END 35 | 36 | push dword ecx 37 | push dword [ebp + 8] 38 | push dword [ebp + 12] 39 | call get_color 40 | add esp, 12 41 | cmp eax, -1 42 | je .FAIL 43 | 44 | push ecx 45 | lea eax, [ebp - 8] 46 | push dword eax 47 | mov eax, [ebp + 12] 48 | ; add eax, s_gui.screen 49 | mov eax, [eax + s_gui.screen] 50 | push eax 51 | push dword 0 52 | mov eax, [ebp + 12] 53 | mov eax, [eax + s_gui.byte_arena] 54 | push dword eax 55 | .OL call SDL_UpperBlit 56 | add esp, 16 57 | cmp eax, 0 58 | jl .FAILB 59 | 60 | pop ecx 61 | 62 | lea eax, [ebp - 8] 63 | add word [eax + SDL_Rect.x], 14 64 | 65 | .IF cmp ecx, 0 66 | jle .ENDIF 67 | 68 | mov edx, 0 69 | mov eax, ecx 70 | inc eax 71 | mov ebx, 96 72 | div ebx 73 | cmp edx, 0 74 | jne .ENDIF 75 | 76 | lea eax, [ebp - 8] 77 | mov word [eax + SDL_Rect.x], 0 78 | add word [eax + SDL_Rect.y], 11 79 | 80 | .ENDIF push ecx 81 | mov eax, [ebp + 12] 82 | mov eax, [eax + s_gui.byte_arena] 83 | push dword eax 84 | call SDL_FreeSurface 85 | add esp, 4 86 | pop ecx 87 | inc ecx 88 | jmp .LOOP 89 | 90 | .FAILB push str1 91 | call my_putstr 92 | add esp, 4 93 | mov eax, -1 94 | mov esp, ebp 95 | pop ebp 96 | ret 97 | 98 | .FAIL mov eax, -1 99 | mov esp, ebp 100 | pop ebp 101 | ret 102 | 103 | .END xor eax, eax 104 | mov esp, ebp 105 | pop ebp 106 | ret 107 | -------------------------------------------------------------------------------- /src/functions/my_or.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: my_or.asm 4 | ;; Author: chapui_s 5 | ;; Created: 23/07/2014 22:10:18 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .text 14 | 15 | extern get_value 16 | extern is_good_register 17 | 18 | extern my_putnbr 19 | 20 | proc my_or, core, champions, instruction 21 | 22 | _var_ value1, value2, or_value 23 | 24 | pushx edx 25 | 26 | invoke get_value, [core], [champions], [instruction], 1 27 | mov dword [value1], eax 28 | 29 | invoke get_value, [core], [champions], [instruction], 2 30 | mov dword [value2], eax 31 | 32 | mov eax, dword [value1] 33 | or eax, dword [value2] 34 | mov dword [or_value], eax 35 | 36 | mov eax, [instruction] 37 | mov eax, [eax + s_instruction.params + 8] 38 | mov edx, eax 39 | invoke is_good_register, eax 40 | cmp eax, 1 41 | jne .CARRY 42 | 43 | mov eax, edx 44 | shl eax, 2 45 | ; mov edx, 4 46 | ; mul edx 47 | 48 | mov edx, [champions] 49 | mov edx, [edx + s_champions.reg] 50 | 51 | ; mov eax, [instruction] 52 | ; mov eax, [eax + s_instruction.params + 8] 53 | ; shl eax, 2 54 | 55 | add edx, eax 56 | 57 | mov eax, dword [or_value] 58 | 59 | mov dword [edx], eax 60 | 61 | 62 | ; invoke my_putnbr, eax 63 | 64 | ; mov eax, [instruction] 65 | ; mov eax, [eax + s_instruction.params + 8] 66 | ; shl eax, 2 67 | ; mov edx, [champions] 68 | ; mov edx, [edx + s_champions.reg] 69 | ; add edx, eax 70 | ; pushx eax, ebx, ecx, edx 71 | ; invoke printf, str1, [value1], [value2], [edx] 72 | ; popx eax, ebx, ecx, edx 73 | 74 | 75 | .CARRY mov eax, [champions] 76 | 77 | cmp dword [or_value], 0 78 | je .ONE 79 | 80 | mov dword [eax + s_champions.carry], 0 81 | jmp .END 82 | 83 | .ONE mov dword [eax + s_champions.carry], 1 84 | 85 | .END xor eax, eax 86 | popx edx 87 | 88 | endproc -------------------------------------------------------------------------------- /src/functions/my_st.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: my_st.asm 4 | ;; Author: chapui_s 5 | ;; Created: 23/07/2014 22:20:35 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | extern is_good_register 14 | extern is_register 15 | extern is_indirect 16 | extern write_arena_four 17 | 18 | section .text 19 | 20 | proc my_st, core, champions, instruction 21 | 22 | _var_ value_to_store, index, param0, param1, idx_mod 23 | 24 | pushx edx 25 | 26 | mov dword [value_to_store], 0 27 | mov eax, [instruction] 28 | mov edx, [eax + s_instruction.params] 29 | mov dword [param0], edx 30 | mov edx, [eax + s_instruction.params + 4] 31 | mov dword [param1], edx 32 | mov dword [idx_mod], IDX_MOD 33 | 34 | invoke is_good_register, [param0] 35 | cmp eax, 1 36 | jne .THEN 37 | 38 | mov eax, [param0] 39 | shl eax, 2 40 | 41 | mov edx, [champions] 42 | mov edx, [edx + s_champions.reg] 43 | add edx, eax 44 | 45 | mov eax, dword [edx] 46 | mov dword [value_to_store], eax 47 | 48 | .THEN mov eax, [instruction] 49 | movzx edx, byte [eax + s_instruction.type] 50 | 51 | invoke is_register, edx, 2 52 | cmp eax, 1 53 | jne .ELSEIF 54 | 55 | invoke is_good_register, [param1] 56 | cmp eax, 1 57 | jne .END 58 | 59 | mov eax, [param1] 60 | shl eax, 2 61 | 62 | mov edx, [champions] 63 | mov edx, [edx + s_champions.reg] 64 | add edx, eax 65 | 66 | mov eax, [value_to_store] 67 | mov dword [edx], eax 68 | jmp .END 69 | 70 | .ELSEIF invoke is_indirect, edx, 2 71 | cmp eax, 1 72 | jne .END 73 | 74 | xor edx, edx 75 | mov eax, [param1] 76 | cmp eax, 0 77 | jge .NO 78 | mov edx, -1 79 | .NO idiv dword [idx_mod] 80 | 81 | .TEST mov eax, [champions] 82 | mov eax, [eax + s_champions.pc] 83 | sub eax, 5 84 | add eax, edx 85 | 86 | invoke write_arena_four, [core], [champions], [value_to_store], eax 87 | 88 | .END xor eax, eax 89 | popx edx 90 | 91 | endproc -------------------------------------------------------------------------------- /src/gui/load_players_name.asm: -------------------------------------------------------------------------------- 1 | 2 | %include "corewar.inc" 3 | 4 | section .data 5 | 6 | str1: db 'error: TTF_RenderText', 10, 0 7 | 8 | section .text 9 | 10 | extern get_color_champions 11 | extern TTF_RenderText_Solid 12 | extern my_putstr 13 | extern my_strlen 14 | 15 | global load_players_name 16 | 17 | load_players_name: 18 | 19 | push ebp 20 | mov ebp, esp 21 | 22 | sub esp, 12 23 | ; [ebp - 4] cur 24 | ; [ebp - 8] i 25 | ; [ebp - 12] *tmp 26 | push ebx 27 | push edx 28 | 29 | mov dword [ebp - 8], 0 30 | mov dword [ebp - 4], 1 31 | mov ebx, [ebp + 8] 32 | mov ebx, [ebx + s_corewar.champions] 33 | 34 | .LOOP cmp ebx, 0 35 | je .ENDL 36 | 37 | .IF1 mov edx, [ebx + s_champions.prog_number] 38 | cmp edx, [ebp - 4] 39 | jne .ELSE1 40 | 41 | mov edx, [ebx + s_champions.color_gui] 42 | push edx 43 | push dword [ebp + 12] 44 | call get_color_champions 45 | add esp, 8 46 | 47 | .IF2 mov edx, [ebx + s_champions.name] 48 | push dword edx 49 | call my_strlen 50 | add esp, 4 51 | cmp eax, 0 52 | jle .ELSE2 53 | 54 | push ebx 55 | mov edx, [ebp + 12] 56 | mov edx, [edx + s_gui.my_color] 57 | push dword edx 58 | mov edx, [ebx + s_champions.name] 59 | push dword edx 60 | mov edx, [ebp + 12] 61 | mov edx, [edx + s_gui.font_info] 62 | push dword edx 63 | call TTF_RenderText_Solid 64 | add esp, 12 65 | cmp eax, 0 66 | je .FAIL 67 | pop ebx 68 | 69 | mov edx, [ebp + 12] 70 | add edx, s_gui.players 71 | ; mov edx, [edx + s_gui.players] 72 | add edx, dword [ebp - 8] 73 | mov [edx], eax 74 | add dword [ebp - 8], 4 75 | 76 | .ELSE2 inc dword [ebp - 4] 77 | mov ebx, [ebp + 8] 78 | mov ebx, [ebx + s_corewar.champions] 79 | jmp .LOOP 80 | 81 | .ELSE1 mov ebx, [ebx + s_champions.next] 82 | jmp .LOOP 83 | 84 | .FAIL push str1 85 | call my_putstr 86 | add esp, 4 87 | mov eax, -1 88 | jmp .END 89 | 90 | .ENDL mov edx, [ebp + 12] 91 | add edx, s_gui.players 92 | add edx, [ebp - 8] 93 | mov dword [edx], 0 94 | xor eax, eax 95 | 96 | .END pop edx 97 | pop ebx 98 | mov esp, ebp 99 | pop ebp 100 | ret 101 | -------------------------------------------------------------------------------- /src/functions/my_ld.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: my_ld.asm 4 | ;; Author: chapui_s 5 | ;; Created: 21/07/2014 20:47:47 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .text 14 | 15 | extern is_indirect 16 | extern is_direct 17 | extern read_arena 18 | extern is_good_register 19 | 20 | proc my_ld, core, champions, instruction 21 | 22 | _var_ value_to_load, pc, param0_mod, idx_mod 23 | 24 | pushx edx 25 | 26 | mov dword [value_to_load], 0 27 | mov dword [idx_mod], IDX_MOD 28 | 29 | mov eax, [instruction] 30 | movzx edx, byte [eax + s_instruction.type] 31 | 32 | .IF invoke is_indirect, edx, 1 33 | cmp eax, 1 34 | jne .ELSEIF 35 | 36 | mov eax, [champions] 37 | mov eax, [eax + s_champions.pc] 38 | mov dword [pc], eax 39 | 40 | mov eax, [instruction] 41 | mov eax, [eax + s_instruction.params] 42 | cdq 43 | idiv dword [idx_mod] 44 | mov dword [param0_mod], edx 45 | 46 | add edx, dword [pc] 47 | 48 | invoke read_arena, [core], edx, 4 49 | mov [value_to_load], eax 50 | jmp .NEXT 51 | 52 | .ELSEIF invoke is_direct, edx, 1 53 | cmp eax, 1 54 | jne .NEXT 55 | 56 | mov eax, [instruction] 57 | mov eax, [eax + s_instruction.params] 58 | 59 | mov [value_to_load], eax 60 | 61 | .NEXT mov eax, [instruction] 62 | mov eax, [eax + s_instruction.params + 4] 63 | 64 | invoke is_good_register, eax 65 | cmp eax, 1 66 | jne .NOT 67 | 68 | mov eax, [instruction] 69 | mov eax, [eax + s_instruction.params + 4] 70 | shl eax, 2 71 | 72 | mov edx, [champions] 73 | mov edx, [edx + s_champions.reg] 74 | add edx, eax 75 | 76 | mov eax, [value_to_load] 77 | mov [edx], eax 78 | 79 | .NOT cmp dword [value_to_load], 0 80 | je .ONE 81 | 82 | mov eax, 0 83 | jmp .END 84 | 85 | .ONE mov eax, 1 86 | 87 | .END mov edx, [champions] 88 | mov dword [edx + s_champions.carry], eax 89 | 90 | xor eax, eax 91 | 92 | popx edx 93 | 94 | endproc -------------------------------------------------------------------------------- /src/gui/set_color_with_pc.asm: -------------------------------------------------------------------------------- 1 | 2 | %include "corewar.inc" 3 | 4 | section .data 5 | 6 | str1: db 'error: TTF_RenderText', 10, 0 7 | 8 | section .text 9 | 10 | extern TTF_RenderText_Shaded 11 | extern TTF_RenderText_Solid 12 | extern my_putstr 13 | extern is_pc 14 | extern hex_to_str 15 | 16 | extern printf 17 | extern disp_core 18 | 19 | global set_color_with_pc 20 | 21 | set_color_with_pc: 22 | 23 | push ebp 24 | mov ebp, esp 25 | 26 | sub esp, 8 27 | ; [ebp - 4] fg_color 28 | ; [ebp - 8] str[0] 29 | ; [ebp - 9] str[1] 30 | ; [ebp - 10] str[2] 31 | push edx 32 | push ebx 33 | 34 | mov dword [ebp - 4], 0 35 | 36 | mov edx, [ebp + 16] 37 | 38 | ; invoke disp_core, [ebp + 12] 39 | 40 | ; problem de ptr ici ! 41 | 42 | lea eax, [ebp - 8] 43 | push eax 44 | mov eax, [ebp + 12] 45 | mov eax, [eax + s_corewar.arena] 46 | add eax, edx 47 | mov ebx, eax 48 | mov eax, 0 49 | mov al, byte [ebx] 50 | push eax 51 | call hex_to_str 52 | add esp, 8 53 | 54 | .IF push dword [ebp + 16] 55 | push dword [ebp + 8] 56 | push dword [ebp + 12] 57 | call is_pc 58 | add esp, 12 59 | cmp eax, 0 60 | je .ELSE 61 | 62 | mov eax, [ebp + 8] 63 | mov eax, [eax + s_gui.my_color] 64 | push eax 65 | push dword [ebp - 4] 66 | lea eax, [ebp - 8] 67 | push eax 68 | mov eax, [ebp + 8] 69 | mov eax, [eax + s_gui.font] 70 | push eax 71 | call TTF_RenderText_Shaded 72 | add esp, 16 73 | 74 | ; invoke disp_core, [ebp + 12] 75 | 76 | mov edx, [ebp + 8] 77 | mov [edx + s_gui.byte_arena], eax 78 | jmp .ENDIF 79 | 80 | .ELSE mov eax, [ebp + 8] 81 | mov eax, [eax + s_gui.my_color] 82 | push eax 83 | lea eax, [ebp - 8] 84 | push eax 85 | mov eax, [ebp + 8] 86 | mov eax, [eax + s_gui.font] 87 | push eax 88 | call TTF_RenderText_Solid 89 | add esp, 12 90 | 91 | ; invoke printf, str1 92 | 93 | mov edx, [ebp + 8] 94 | mov [edx + s_gui.byte_arena], eax 95 | 96 | .ENDIF xor eax, eax 97 | cmp dword [edx + s_gui.byte_arena], 0 98 | jne .END 99 | 100 | .FAIL push str1 101 | call my_putstr 102 | add esp, 4 103 | mov eax, -1 104 | 105 | .END pop edx 106 | mov esp, ebp 107 | pop ebp 108 | ret 109 | -------------------------------------------------------------------------------- /src/functions/my_sub.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: my_sub.asm 4 | ;; Author: chapui_s 5 | ;; Created: 23/07/2014 22:35:30 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .data 14 | 15 | str1: db 'SUB %d - %d = %d', 10, 0 16 | 17 | section .text 18 | 19 | extern is_good_register 20 | 21 | proc my_sub, core, champions, instruction 22 | 23 | _var_ value1, value2, sub_value, param0, param1, param2, reg_ptr 24 | 25 | pushx edx 26 | 27 | mov dword [value1], 0 28 | mov dword [value2], 0 29 | mov dword [sub_value], 0 30 | 31 | mov eax, [instruction] 32 | mov edx, [eax + s_instruction.params] 33 | mov dword [param0], edx 34 | mov edx, [eax + s_instruction.params + 4] 35 | mov dword [param1], edx 36 | mov edx, [eax + s_instruction.params + 8] 37 | mov dword [param2], edx 38 | 39 | mov eax, [champions] 40 | mov eax, [eax + s_champions.reg] 41 | mov dword [reg_ptr], eax 42 | 43 | .1 invoke is_good_register, [param0] 44 | cmp eax, 1 45 | jne .2 46 | 47 | mov eax, [param0] 48 | shl eax, 2 49 | mov edx, [reg_ptr] 50 | add edx, eax 51 | 52 | mov eax, [edx] 53 | mov dword [value1], eax 54 | 55 | .2 invoke is_good_register, [param1] 56 | cmp eax, 1 57 | jne .3 58 | 59 | mov eax, [param1] 60 | shl eax, 2 61 | mov edx, [reg_ptr] 62 | add edx, eax 63 | 64 | mov eax, [edx] 65 | mov dword [value2], eax 66 | 67 | .3 mov eax, [value1] 68 | sub eax, dword [value2] 69 | mov dword [sub_value], eax 70 | 71 | ; pushx eax, ebx, ecx, edx 72 | ; invoke printf, str1, [value1], [value2], [sub_value] 73 | ; popx eax, ebx, ecx, edx 74 | 75 | invoke is_good_register, [param2] 76 | cmp eax, 1 77 | jne .CARRY 78 | 79 | mov eax, [param2] 80 | shl eax, 2 81 | mov edx, [reg_ptr] 82 | add edx, eax 83 | 84 | mov eax, [sub_value] 85 | mov [edx], eax 86 | 87 | .CARRY mov eax, [champions] 88 | cmp dword [sub_value], 0 89 | je .ONE 90 | 91 | mov dword [eax + s_champions.carry], 0 92 | jmp .END 93 | 94 | .ONE mov dword [eax + s_champions.carry], 1 95 | 96 | .END xor eax, eax 97 | popx edx 98 | 99 | endproc -------------------------------------------------------------------------------- /src/utils/my_showmem.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: my_showmem.asm 4 | ;; Author: chapui_s 5 | ;; Created: 26/07/2014 14:39:37 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "macro.inc" 12 | 13 | section .data 14 | 15 | _16: dd 16 16 | _32: dd 32 17 | base_str: db '0123456789ABCDEF', 0 18 | 19 | section .text 20 | 21 | extern my_putchar 22 | 23 | proc my_put_hexa, nb, base, is_first 24 | 25 | .1 cmp dword [nb], 16 26 | jge .2 27 | 28 | cmp dword [is_first], 1 29 | jne .2 30 | 31 | invoke my_putchar, '0' 32 | 33 | .2 cmp dword [nb], 16 34 | jl .3 35 | 36 | mov eax, [nb] 37 | xor edx, edx 38 | div dword [_16] 39 | invoke my_put_hexa, eax, [base], 0 40 | 41 | .3 mov eax, [nb] 42 | xor edx, edx 43 | cmp eax, 0 44 | jge .NO 45 | mov edx, -1 46 | .NO idiv dword [_16] 47 | mov eax, [base] 48 | add eax, edx 49 | invoke my_putchar, [eax] 50 | 51 | endproc 52 | 53 | proc write_hex, i_cur, line_cur1, size1, string1 54 | 55 | _var_ tmp 56 | 57 | mov eax, [line_cur1] 58 | mul dword [_32] 59 | add eax, 32 60 | mov [tmp], eax 61 | 62 | .LOOP mov eax, [i_cur] 63 | cmp eax, [tmp] 64 | jge .endproc 65 | 66 | cmp eax, [size1] 67 | jge .endproc 68 | 69 | mov edx, [string1] 70 | add edx, eax 71 | movzx edx, byte [edx] 72 | invoke my_put_hexa, edx, base_str, 1 73 | inc dword [i_cur] 74 | 75 | jmp .LOOP 76 | 77 | endproc 78 | 79 | proc my_showmem, string, size 80 | 81 | _var_ nb_lines, line_cur, i 82 | 83 | pushx edx 84 | 85 | mov dword [line_cur], 0 86 | mov dword [i], 0 87 | mov eax, [size] 88 | xor edx, edx 89 | div dword [_32] 90 | inc eax 91 | mov [nb_lines], eax 92 | 93 | .LOOP mov eax, [line_cur] 94 | cmp eax, [nb_lines] 95 | jge .END 96 | 97 | mov eax, [i] 98 | cmp eax, [size] 99 | jge .END 100 | 101 | invoke write_hex, [i], [line_cur], [size], [string] 102 | invoke my_putchar, 10 103 | inc dword [line_cur] 104 | mov eax, [line_cur] 105 | mul dword [_32] 106 | mov [i], eax 107 | 108 | jmp .LOOP 109 | 110 | .END xor eax, eax 111 | popx edx 112 | 113 | endproc -------------------------------------------------------------------------------- /corewar.man: -------------------------------------------------------------------------------- 1 | .\" Manpage for corewar_x86. 2 | .\" Contact sebastien@chapuis.co to correct errors or typos. 3 | .TH COREWAR_x86 "1" "August 2014" "1.0" "Corewar_x86 man page" 4 | .SH NAME 5 | .PP 6 | corewar \- a game where little programs fight in a virtual machine 7 | .SH SYNOPSIS 8 | .PP 9 | \fB./corewar\fR [\fB-dump\fR \fI nbr_cycle\fR] [[\fB-n\fR \fIprog_number\fR] [\fB-a\fR \fIload_address\fR] \fIprog_name\fR]... [\fB-d\fR] 10 | .SH DESCRIPTION 11 | .PP 12 | The corewar is a very particular game. The objective is to stop others programs by all means. The winner is the last who execute a "live" instruction. 13 | .PP 14 | The game is divided in 3 parts : 15 | .TP 16 | \fBThe assembler [See https://github.com/sebastiencs/corewar for this part]\fR 17 | It writes the programs who fights. It understands the assembler language and generate binary files understandable by the virtual machine. 18 | .TP 19 | \fBThe virtual machine\fR 20 | It loads the binaries generated by the assembler in an arena, which offer a lot of possibilities for the champions. It can execute several programs at the same time. 21 | .br 22 | You can leave the virtual machine by hitting the ESC key. You can pause the execution of the virtual machin by hitting SPACE key. While in pause, if you hit three times the D key, the memory will be dumped. 23 | .br 24 | The virtual machine can also desassemble a program when used with \fI-d\fR parameter. 25 | .TP 26 | \fBThe champion\fR 27 | It is the program who fights in the virtual machine. It is written in the assembler language. It will be compilated by the assembler 28 | .SH OPTIONS 29 | .TP 30 | \fB-a\fR \fBload_adress\fR 31 | Modify the adress of the next program. 32 | .TP 33 | \fB-d\fR 34 | Desassemble the champions given in parameters and exit. 35 | .TP 36 | \fB-dump\fR \fInbr_cycle\fR 37 | Dump the memory after \fInbr_cycle\fR cycles. After the memory being dumping, we exit the game. 38 | .TP 39 | \fB-n\fR \fIprog_number\fR 40 | Modify the number of the next program 41 | .SH EXAMPLES 42 | .TP 43 | \fB./corewar roller.cor try_again.cor 44 | It will launch the virtual machine with the champion roller.cor fighting the champion try_again.cor 45 | .SH SEE ALSO 46 | No related manpage. 47 | .SH REPORTING BUGS 48 | No known bugs. 49 | .br 50 | Report corewar_x86 bugs to sebastien@chapuis.co 51 | .SH AUTHOR 52 | Sebastien Chapuis 53 | -------------------------------------------------------------------------------- /src/functions/my_add.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: my_add.asm 4 | ;; Author: chapui_s 5 | ;; Created: 20/07/2014 23:30:24 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .data 14 | 15 | str1: db 'ADD %d + %d = %d', 10, 0 16 | 17 | section .text 18 | 19 | extern is_good_register 20 | 21 | extern printf 22 | 23 | proc my_add, core, champions, instruction 24 | 25 | _var_ value1, value2, param0, param1, param2 26 | 27 | pushx edx 28 | 29 | mov dword [value1], 0 30 | mov dword [value2], 0 31 | 32 | mov eax, [instruction] 33 | add eax, s_instruction.params 34 | 35 | mov edx, [eax] 36 | mov [param0], edx 37 | mov edx, [eax + 4] 38 | mov [param1], edx 39 | mov edx, [eax + 8] 40 | mov [param2], edx 41 | 42 | .1 invoke is_good_register, [param0] 43 | cmp eax, 1 44 | jne .2 45 | 46 | mov eax, [param0] 47 | shl eax, 2 48 | mov edx, [champions] 49 | mov edx, [edx + s_champions.reg] 50 | add edx, eax 51 | mov edx, [edx] 52 | mov [value1], edx 53 | 54 | .2 invoke is_good_register, [param1] 55 | cmp eax, 1 56 | jne .3 57 | 58 | mov eax, [param1] 59 | shl eax, 2 60 | mov edx, [champions] 61 | mov edx, [edx + s_champions.reg] 62 | add edx, eax 63 | mov edx, [edx] 64 | mov [value2], edx 65 | 66 | .3 invoke is_good_register, [param2] 67 | cmp eax, 1 68 | jne .END 69 | 70 | mov eax, [value1] 71 | add eax, [value2] 72 | 73 | ; pushx eax, ebx, ecx, edx 74 | ; invoke printf, str1, [value1], [value2], eax 75 | ; popx eax, ebx, ecx, edx 76 | 77 | 78 | mov [value1], eax 79 | 80 | mov eax, [param2] 81 | shl eax, 2 82 | mov edx, [champions] 83 | mov edx, [edx + s_champions.reg] 84 | add edx, eax 85 | mov eax, [value1] 86 | mov [edx], eax 87 | 88 | .END mov eax, [champions] 89 | add eax, s_champions.carry 90 | 91 | cmp dword [value1], 0 92 | je .ONE 93 | 94 | mov dword [eax], 0 95 | xor eax, eax 96 | popx edx 97 | jmp .endproc 98 | 99 | .ONE mov dword [eax], 1 100 | xor eax, eax 101 | popx edx 102 | 103 | endproc -------------------------------------------------------------------------------- /src/exec_instructions/fulfil_params.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: fulfil_params.asm 4 | ;; Author: chapui_s 5 | ;; Created: 19/07/2014 23:54:56 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .data 14 | 15 | str1: db 'tmp_type = %d', 10, 0 16 | 17 | section .text 18 | 19 | extern fulfil_dir 20 | extern read_arena 21 | 22 | proc fulfil_param, core, champion, instruction 23 | 24 | _var_ decal, tmp_type, other, param_ptr, mem_size 25 | 26 | pushx eax, ecx, edx 27 | 28 | mov dword [mem_size], MEM_SIZE 29 | mov dword [other], 0 30 | mov dword [tmp_type], 0 31 | 32 | mov eax, [instruction] 33 | movzx edx, byte [eax + s_instruction.type] 34 | shr dl, 2 35 | shl dl, 2 36 | mov byte [tmp_type], dl 37 | 38 | mov eax, [champion] 39 | mov eax, [eax + s_champions.pc] 40 | add eax, 2 41 | mov [decal], eax 42 | 43 | mov eax, [instruction] 44 | lea eax, [eax + s_instruction.params] 45 | mov [param_ptr], eax 46 | 47 | xor ecx, ecx 48 | 49 | .LOOP movzx eax, byte [tmp_type] 50 | and al, 0b11000000 51 | test al, al 52 | jz .ENDL 53 | 54 | movzx eax, byte [tmp_type] 55 | shr al, 6 56 | mov byte [other], al 57 | 58 | IF byte [other], e, 1 59 | 60 | invoke read_arena, [core], [decal], 1 61 | mov edx, [param_ptr] 62 | mov [edx + (ecx * 4)], eax 63 | inc dword [decal] 64 | 65 | ELSEIF byte [other], e, 0b10 66 | 67 | lea eax, [decal] 68 | invoke fulfil_dir, [core], [champion], [instruction], eax 69 | mov edx, [param_ptr] 70 | mov [edx + (ecx * 4)], eax 71 | 72 | ELSE 73 | 74 | invoke read_arena, [core], [decal], 2 75 | mov edx, [param_ptr] 76 | mov [edx + (ecx * 4)], eax 77 | add dword [decal], 2 78 | 79 | ENDIF 80 | 81 | shl byte [tmp_type], 2 82 | inc ecx 83 | jmp .LOOP 84 | 85 | .ENDL mov eax, [decal] 86 | cdq 87 | idiv dword [mem_size] 88 | mov eax, [champion] 89 | mov [eax + s_champions.pc], edx 90 | 91 | popx eax, ecx, edx 92 | 93 | endproc -------------------------------------------------------------------------------- /src/gui/my_gui.asm: -------------------------------------------------------------------------------- 1 | 2 | %include "corewar.inc" 3 | 4 | section .data 5 | 6 | str1: db 'error: SDL_Init: ', 10, 0 7 | str2: db 'error: SDL_SetVideoMode', 10, 0 8 | str3: db 'Corewar ASM', 0 9 | str4: db 'error: malloc', 10, 0 10 | 11 | str5: db 'OK', 10, 0 12 | 13 | t_gui: 14 | istruc s_gui 15 | times 14 dd 0 16 | iend 17 | 18 | section .text 19 | 20 | extern SDL_Init 21 | extern SDL_SetVideoMode 22 | extern SDL_WM_SetCaption 23 | extern TTF_Init 24 | extern my_putstr 25 | extern malloc 26 | extern get_image_path 27 | extern get_color_champions 28 | extern put_background 29 | extern get_arena 30 | extern disp_gui 31 | 32 | global my_gui 33 | 34 | my_gui: 35 | 36 | push ebp 37 | mov ebp, esp 38 | 39 | push edx 40 | 41 | push dword SDL_INIT_VIDEO 42 | call SDL_Init 43 | add esp, 4 44 | cmp eax, -1 45 | je .FAILI 46 | 47 | push dword SDL_HWSURFACE 48 | push dword 32 49 | push dword WIN_Y 50 | push dword WIN_X 51 | call SDL_SetVideoMode 52 | add esp, 16 53 | cmp eax, 0 54 | je .FAILS 55 | 56 | mov edx, t_gui 57 | mov [edx + s_gui.screen], eax 58 | 59 | push dword 0 60 | push str3 61 | call SDL_WM_SetCaption 62 | add esp, 8 63 | 64 | call TTF_Init 65 | cmp eax, -1 66 | je .FAILT 67 | 68 | push dword [ebp + 12] 69 | push t_gui 70 | push dword [ebp + 8] 71 | call put_background 72 | add esp, 12 73 | cmp eax, -1 74 | je .FAIL 75 | 76 | push (4 * (MAX_PC * 2)) 77 | call malloc 78 | add esp, 4 79 | cmp eax, 0 80 | je .FAILM 81 | 82 | mov edx, t_gui 83 | mov [edx + s_gui.list_pc], eax 84 | 85 | ; TO RM 86 | ;.TEST invoke disp_gui, t_gui 87 | 88 | 89 | 90 | push dword t_gui 91 | push dword [ebp + 8] 92 | call get_arena 93 | add esp, 8 94 | cmp eax, -1 95 | je .FAIL 96 | 97 | xor eax, eax 98 | jmp .END 99 | 100 | .FAILI push str1 101 | call my_putstr 102 | add esp, 4 103 | mov eax, -1 104 | jmp .END 105 | 106 | .FAILS push str2 107 | call my_putstr 108 | add esp, 4 109 | mov eax, -1 110 | jmp .END 111 | 112 | .FAILT push str3 113 | call my_putstr 114 | add esp, 4 115 | mov eax, -1 116 | jmp .END 117 | 118 | .FAILM push str4 119 | call my_putstr 120 | add esp, 4 121 | mov eax, -1 122 | jmp .END 123 | 124 | .FAIL mov eax, -1 125 | 126 | .END pop edx 127 | mov esp, ebp 128 | pop ebp 129 | ret 130 | -------------------------------------------------------------------------------- /src/utils/attribute_two_def.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: attribute_two_def.asm 4 | ;; Author: chapui_s 5 | ;; Created: 4/08/2014 23:44:45 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .data 14 | 15 | extern mem_size 16 | 17 | section .text 18 | 19 | extern swap_int 20 | 21 | proc attribute_two_def, champions, pa, nb 22 | 23 | _var_ p0, p1 24 | 25 | pushx eax, ebx, ecx, edx 26 | 27 | mov eax, [champions] 28 | mov ecx, 0x1 29 | 30 | WHILE eax, ne, 0x0 31 | 32 | IF dword [eax + s_champions.load_address], ne, 0x0 33 | 34 | mov edx, [eax + s_champions.load_address] 35 | mov [p1 + (ecx * 0x4)], edx ; bascule entre p0 et p1 36 | dec ecx 37 | 38 | ENDIF 39 | 40 | mov eax, [eax + s_champions.next] 41 | 42 | END_WHILE 43 | 44 | mov eax, [p1] 45 | 46 | IF eax, l, [p0] 47 | 48 | lea eax, [p0] 49 | lea edx, [p1] 50 | invoke swap_int, eax, edx 51 | 52 | ENDIF 53 | 54 | mov eax, [p1] 55 | sub eax, [p0] 56 | IF eax, g, MEM_SIZE / 0x2 57 | 58 | cdq 59 | idiv dword [nb] 60 | add eax, [p0] 61 | 62 | ELSE 63 | 64 | mov edx, MEM_SIZE 65 | sub edx, eax 66 | mov eax, edx 67 | cdq 68 | idiv dword [nb] 69 | add eax, [p1] 70 | 71 | ENDIF 72 | 73 | mov [pa], eax 74 | 75 | mov eax, [champions] 76 | 77 | WHILE eax, ne, 0x0 78 | 79 | mov edx, [pa] 80 | 81 | IF edx, l, 0x0 82 | 83 | add dword [pa], MEM_SIZE 84 | 85 | ENDIF 86 | 87 | IF dword [eax + s_champions.load_address], e, 0x0 88 | 89 | push eax 90 | mov eax, [pa] 91 | cdq 92 | idiv dword [mem_size] 93 | pop eax 94 | mov [eax + s_champions.load_address], edx 95 | 96 | mov edx, [pa] 97 | 98 | mov ebx, [p1] 99 | sub ebx, [p0] 100 | 101 | IF ebx, l, (MEM_SIZE / 0x2) 102 | 103 | sub edx, [p1] 104 | 105 | ENDIF 106 | 107 | IF ebx, ge, (MEM_SIZE / 0x2) 108 | 109 | sub edx, [p0] 110 | 111 | ENDIF 112 | 113 | add [pa], edx 114 | 115 | ENDIF 116 | 117 | mov eax, [eax + s_champions.next] 118 | 119 | END_WHILE 120 | 121 | popx eax, ebx, ecx, edx 122 | 123 | endproc 124 | -------------------------------------------------------------------------------- /src/functions/my_lfork.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: my_lfork.asm 4 | ;; Author: chapui_s 5 | ;; Created: 21/07/2014 20:25:39 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .text 14 | 15 | extern push_champion 16 | extern copy_reg 17 | 18 | proc my_lfork, core, champions, instruction 19 | 20 | _var_ new_pc, tmp, param0, mem_size 21 | 22 | pushx ebx, edx 23 | 24 | mov dword [mem_size], MEM_SIZE 25 | 26 | mov eax, [instruction] 27 | add eax, s_instruction.params 28 | cmp dword [eax], 0 29 | jne .NEXT 30 | 31 | mov dword [eax], 3 32 | 33 | .NEXT mov eax, [eax] 34 | mov [param0], eax 35 | mov eax, [champions] 36 | mov eax, [eax + s_champions.pc] 37 | add eax, [param0] 38 | mov dword [new_pc], eax 39 | 40 | .LOOP cmp dword [new_pc], 0 41 | jge .ENDL 42 | 43 | add dword [new_pc], MEM_SIZE 44 | jmp .LOOP 45 | 46 | .ENDL mov edx, 0 47 | mov eax, [new_pc] 48 | div dword [mem_size] 49 | mov [new_pc], edx 50 | 51 | mov eax, [champions] 52 | invoke push_champion, [core], [eax + s_champions.filename], \ 53 | [eax + s_champions.prog_number], [new_pc] 54 | _check_ .FAIL, -1 55 | 56 | mov eax, [core] 57 | mov eax, [eax + s_corewar.last_champions] 58 | mov ebx, eax 59 | 60 | mov eax, [champions] 61 | mov edx, [eax + s_champions.name] 62 | mov [ebx + s_champions.name], edx 63 | mov edx, [eax + s_champions.comment] 64 | mov [ebx + s_champions.comment], edx 65 | mov edx, [core] 66 | inc dword [edx + s_corewar.prog_number_max] 67 | mov edx, [edx + s_corewar.prog_number_max] 68 | mov [ebx + s_champions.prog_number], edx 69 | mov edx, [eax + s_champions.carry] 70 | mov [ebx + s_champions.carry], edx 71 | mov dword [ebx + s_champions.last_live], 0 72 | mov dword [ebx + s_champions.cycle_to_wait], 2 73 | mov edx, [eax + s_champions.color_gui] 74 | mov [ebx + s_champions.color_gui], edx 75 | mov edx, [new_pc] 76 | mov [ebx + s_champions.pc], edx 77 | 78 | invoke copy_reg, [eax + s_champions.reg] 79 | _check_ .FAIL, 0 80 | 81 | mov [ebx + s_champions.reg], eax 82 | mov eax, [core] 83 | inc dword [eax + s_corewar.nb_champions] 84 | 85 | xor eax, eax 86 | jmp .END 87 | 88 | .FAIL mov eax, -1 89 | 90 | .END popx ebx, edx 91 | 92 | endproc -------------------------------------------------------------------------------- /src/arguments.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: arguments.asm 4 | ;; Author: chapui_s 5 | ;; Created: 5/08/2014 22:23:22 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .data 14 | 15 | str1: db '-d', 0 16 | 17 | section .text 18 | 19 | extern strcmp 20 | extern is_options 21 | extern is_numbers 22 | extern is_file_dot_cor 23 | extern is_one_file_cor 24 | extern usage 25 | extern get_dump 26 | extern printf 27 | extern my_strcmp 28 | extern save_args 29 | extern attribute_prog_number 30 | extern attribute_address 31 | 32 | proc is_error_in_args, argc1, argv1 33 | 34 | pushx ebx, ecx 35 | 36 | IF dword [argc1], l, 2 37 | 38 | jmp .FAIL 39 | 40 | ENDIF 41 | 42 | mov ecx, 1 43 | mov ebx, [argv1] 44 | 45 | WHILE ecx, l, [argc1] 46 | 47 | invoke is_options, [ebx + (ecx * 4)] 48 | 49 | IF eax, ne, 0 50 | 51 | invoke is_numbers, [ebx + ((ecx + 1)* 4)] 52 | 53 | IF eax, e, -1 54 | 55 | invoke my_strcmp, [ebx + (ecx * 4)], str1 56 | test eax, eax 57 | jnz .FAIL 58 | 59 | ENDIF 60 | 61 | add ecx, 2 62 | 63 | ELSE 64 | 65 | invoke is_file_dot_cor, [ebx + (ecx * 4)] 66 | cmp eax, -1 67 | je .FAIL 68 | 69 | inc ecx 70 | 71 | ENDIF 72 | 73 | END_WHILE 74 | 75 | invoke is_one_file_cor, [argc1], [argv1] 76 | cmp eax, -1 77 | je .FAIL 78 | 79 | xor eax, eax 80 | 81 | .END popx ebx, ecx 82 | RET 83 | 84 | .FAIL mov eax, -1 85 | jmp .END 86 | 87 | endproc 88 | 89 | proc get_args, argc, argv, core 90 | 91 | invoke is_error_in_args, [argc], [argv] 92 | 93 | IF eax, e, -1 94 | 95 | call usage 96 | jmp .FAIL 97 | 98 | ENDIF 99 | 100 | invoke get_dump, [argc], [argv], [core] 101 | 102 | invoke save_args, [argc], [argv], [core] 103 | _check_ .FAIL, -1 104 | 105 | mov eax, [core] 106 | invoke attribute_prog_number, [eax + s_corewar.champions], [eax + s_corewar.nb_champions] 107 | _check_ .FAIL, -1 108 | 109 | mov eax, [core] 110 | invoke attribute_address, [eax + s_corewar.champions], [eax + s_corewar.nb_champions] 111 | _check_ .FAIL, -1 112 | 113 | xor eax, eax 114 | RET 115 | 116 | .FAIL mov eax, -1 117 | 118 | endproc 119 | -------------------------------------------------------------------------------- /src/utils/get_name_comment_champions.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: get_name_comment_champions.asm 4 | ;; Author: chapui_s 5 | ;; Created: 5/08/2014 01:20:23 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .data 14 | 15 | str1: db 'error: lseek', 10, 0 16 | str2: db 'error: open', 10, 0 17 | 18 | section .text 19 | 20 | extern open 21 | extern get_magic 22 | extern lseek 23 | extern get_name 24 | extern get_size 25 | extern get_comment 26 | extern my_putstr 27 | 28 | proc get_name_comment_champions, champions, fd 29 | 30 | _var_ fd_int 31 | 32 | pushx ebx, ecx, edx 33 | 34 | mov eax, 5 ; open 35 | mov ebx, [champions] 36 | mov ebx, [ebx + s_champions.filename] 37 | mov ecx, O_RDONLY 38 | xor edx, edx 39 | int 0x80 40 | _check_ .FAILO, -1 41 | 42 | mov edx, [fd] 43 | mov [edx], eax 44 | mov [fd_int], eax 45 | 46 | invoke get_magic, [champions], [fd_int] 47 | _check_ .FAIL, -1 48 | 49 | mov eax, 19 ; lseek 50 | mov ebx, [fd_int] 51 | mov ecx, 4 ; size int 52 | mov edx, SEEK_SET 53 | int 0x80 54 | _check_ .FAILS, -1 55 | 56 | invoke get_name, [champions], [fd_int] 57 | _check_ .FAIL, -1 58 | 59 | mov eax, 19 ; lseek 60 | mov ebx, [fd_int] 61 | mov ecx, (4 + (PROG_NAME_LENGTH + 4)) ; sizeof(int) + (PROG_NAME_LENGTH + 4) 62 | mov edx, SEEK_SET 63 | int 0x80 64 | _check_ .FAILS, -1 65 | 66 | invoke get_size, [champions], [fd_int] 67 | _check_ .FAIL, -1 68 | 69 | mov eax, 19 ; lseek 70 | mov ebx, [fd_int] 71 | mov ecx, (8 + (PROG_NAME_LENGTH + 4)) ; (sizeof(int) * 2) + (PROG_NAME_LENGTH + 4) 72 | mov edx, SEEK_SET 73 | int 0x80 74 | _check_ .FAILS, -1 75 | 76 | invoke get_comment, [champions], [fd_int] 77 | _check_ .FAIL, -1 78 | 79 | mov eax, 19 ; lseek 80 | mov ebx, [fd_int] 81 | mov ecx, 2192 ; sizeof(struct header_s) 82 | mov edx, SEEK_SET 83 | int 0x80 84 | _check_ .FAIL, -1 85 | 86 | xor eax, eax 87 | 88 | .END popx ebx, ecx, edx 89 | RET 90 | 91 | .FAILO invoke my_putstr, str2 92 | mov eax, -1 93 | jmp .END 94 | 95 | .FAILS invoke my_putstr, str1 96 | mov eax, -1 97 | jmp .END 98 | 99 | .FAIL mov eax, -1 100 | jmp .END 101 | 102 | endproc 103 | -------------------------------------------------------------------------------- /src/utils/attribute_prog_number.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: attribute_prog_number.asm 4 | ;; Author: chapui_s 5 | ;; Created: 4/08/2014 21:50:58 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .text 14 | 15 | extern check_same_prog_number 16 | extern check_big_prog_number 17 | extern attribute_i_to_someone 18 | 19 | proc attribute_prog_number, list_champion, nb_champions 20 | 21 | pushx ecx, edx 22 | 23 | invoke check_same_prog_number, [list_champion] 24 | _check_ .END, 0xFFFFFFFF 25 | 26 | invoke check_big_prog_number, [list_champion], [nb_champions] 27 | _check_ .END, 0xFFFFFFFF 28 | 29 | mov ecx, 0x1 30 | 31 | WHILE ecx, le, [nb_champions] 32 | 33 | invoke attribute_i_to_someone, [list_champion], ecx 34 | inc ecx 35 | 36 | END_WHILE 37 | 38 | mov eax, [list_champion] 39 | 40 | WHILE eax, ne, 0x0 41 | 42 | mov edx, [eax + s_champions.prog_number] 43 | mov [eax + s_champions.color_gui], edx 44 | 45 | mov eax, [eax + s_champions.next] 46 | 47 | END_WHILE 48 | 49 | xor eax, eax 50 | 51 | .END popx ecx, edx 52 | 53 | endproc 54 | 55 | ; global attribute_prog_number 56 | 57 | ; attribute_prog_number: 58 | 59 | ; push ebp 60 | ; mov ebp, esp 61 | 62 | ; push ecx 63 | ; push edx 64 | 65 | ; mov ecx, 1 66 | 67 | ; push dword [ebp + 8] 68 | ; call check_same_prog_number 69 | ; add esp, 4 70 | ; cmp eax, -1 71 | ; je .FAIL 72 | 73 | ; push dword [ebp + 12] 74 | ; push dword [ebp + 8] 75 | ; call check_big_prog_number 76 | ; add esp, 8 77 | ; cmp eax, -1 78 | ; je .FAIL 79 | 80 | ; .L1 cmp dword ecx, [ebp + 12] 81 | ; jg .ENDL1 82 | 83 | ; push dword ecx 84 | ; push dword [ebp + 8] 85 | ; call attribute_i_to_someone 86 | ; add esp, 8 87 | 88 | ; inc ecx 89 | ; jmp .L1 90 | 91 | ; .ENDL1 mov eax, [ebp + 8] 92 | 93 | ; .L2 cmp eax, 0 94 | ; je .ENDL2 95 | 96 | ; mov edx, [eax + s_champions.prog_number] 97 | ; mov [eax + s_champions.color_gui], edx 98 | 99 | ; mov eax, [eax + s_champions.next] 100 | ; jmp .L2 101 | 102 | ; .ENDL2 xor eax, eax 103 | ; jmp .END 104 | 105 | ; .FAIL mov eax, -1 106 | 107 | ; .END pop edx 108 | ; pop ecx 109 | ; mov esp, ebp 110 | ; pop ebp 111 | ; ret 112 | -------------------------------------------------------------------------------- /src/exec_instructions/get_type_and_param.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: get_type_and_param.asm 4 | ;; Author: chapui_s 5 | ;; Created: 20/07/2014 00:21:56 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .text 14 | 15 | extern read_arena 16 | extern fulfil_param 17 | 18 | proc get_type_and_param, core, champions, instruction 19 | 20 | _var_ code, mem_size 21 | 22 | pushx eax, edx 23 | 24 | mov dword [mem_size], MEM_SIZE 25 | 26 | mov eax, [instruction] 27 | movzx edx, byte [eax + s_instruction.code] 28 | 29 | mov [code], edx 30 | 31 | cmp edx, LIVE 32 | je .ELSE 33 | 34 | cmp edx, ZJMP 35 | je .ELSE 36 | 37 | cmp edx, FORK 38 | je .ELSE 39 | 40 | cmp edx, LFORK 41 | je .ELSE 42 | 43 | mov eax, [champions] 44 | mov eax, [eax + s_champions.pc] 45 | inc eax 46 | cdq 47 | idiv dword [mem_size] 48 | 49 | mov eax, [core] 50 | mov eax, [eax + s_corewar.arena] 51 | add eax, edx 52 | mov al, byte [eax] 53 | 54 | mov edx, [instruction] 55 | add edx, s_instruction.type 56 | 57 | mov byte [edx], al 58 | 59 | invoke fulfil_param, [core], [champions], [instruction] 60 | jmp .END 61 | 62 | .ELSE nop 63 | 64 | mov eax, [instruction] 65 | add eax, s_instruction.type 66 | mov byte [eax], 0 67 | 68 | IF byte [code], e, LIVE 69 | 70 | mov eax, [champions] 71 | mov eax, [eax + s_champions.pc] 72 | inc eax 73 | invoke read_arena, [core], eax, 4 74 | mov edx, [instruction] 75 | add edx, s_instruction.params 76 | mov [edx], eax 77 | 78 | mov eax, [champions] 79 | add dword [eax + s_champions.pc], 5 80 | ; mov eax, [eax + s_champions.pc] 81 | ; add eax, 5 82 | ; mov edx, [champions] 83 | ; mov dword [edx + s_champions.pc], eax 84 | 85 | ELSE 86 | 87 | mov eax, [champions] 88 | mov eax, [eax + s_champions.pc] 89 | inc eax 90 | invoke read_arena, [core], eax, 2 91 | mov edx, [instruction] 92 | add edx, s_instruction.params 93 | mov [edx], eax 94 | 95 | mov eax, [champions] 96 | add dword [eax + s_champions.pc], 3 97 | ; mov eax, [eax + s_champions.pc] 98 | ; add eax, 3 99 | ; mov edx, [champions] 100 | ; mov dword [edx + s_champions.pc], eax 101 | 102 | ENDIF 103 | 104 | .END popx eax, edx 105 | 106 | endproc -------------------------------------------------------------------------------- /src/utils/check_args.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: check_args.asm 4 | ;; Author: chapui_s 5 | ;; Created: 4/08/2014 23:52:21 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .data 14 | 15 | str1: db '-dump', 0 16 | str2: db '-n', 0 17 | str3: db '-a', 0 18 | str4: db '-d', 0 19 | str5: db '.cor', 0 20 | 21 | section .text 22 | 23 | extern my_strcmp 24 | extern my_strlen 25 | 26 | proc is_numbers, string_numbers 27 | 28 | pushx edx 29 | 30 | cmp dword [string_numbers], 0 31 | je .FAIL 32 | 33 | mov edx, [string_numbers] 34 | 35 | WHILE byte [edx], ne, 0 36 | 37 | IF byte [edx], l, '0' 38 | 39 | jmp .FAIL 40 | 41 | ENDIF 42 | 43 | IF byte [edx], g, '9' 44 | 45 | jmp .FAIL 46 | 47 | ENDIF 48 | 49 | inc edx 50 | 51 | END_WHILE 52 | 53 | xor eax, eax 54 | 55 | .END popx edx 56 | RET 57 | 58 | .FAIL mov eax, -1 59 | jmp .END 60 | 61 | endproc 62 | 63 | proc is_options, string_options 64 | 65 | invoke my_strcmp, [string_options], str1 66 | test eax, eax 67 | jz .OK 68 | 69 | invoke my_strcmp, [string_options], str2 70 | test eax, eax 71 | jz .OK 72 | 73 | invoke my_strcmp, [string_options], str3 74 | test eax, eax 75 | jz .OK 76 | 77 | invoke my_strcmp, [string_options], str4 78 | test eax, eax 79 | jz .OK 80 | 81 | xor eax, eax 82 | RET 83 | 84 | .OK mov eax, 1 85 | 86 | endproc 87 | 88 | proc is_file_dot_cor, string_file 89 | 90 | invoke my_strlen, [string_file] 91 | _check_ .FAIL, 5, l 92 | 93 | sub eax, 4 94 | add eax, [string_file] 95 | 96 | invoke my_strcmp, eax, str5 97 | _check_ .FAIL, 0, ne 98 | 99 | mov eax, 1 100 | RET 101 | 102 | .FAIL mov eax, -1 103 | 104 | endproc 105 | 106 | proc is_one_file_cor, argc, argv 107 | 108 | _var_ nb_cor 109 | 110 | pushx ecx 111 | 112 | mov ecx, 1 113 | mov dword [nb_cor], 0 114 | 115 | WHILE ecx, l, [argc] 116 | 117 | mov eax, [argv] 118 | invoke is_file_dot_cor, [eax + (ecx * 4)] 119 | 120 | IF eax, e, 1 121 | 122 | inc dword [nb_cor] 123 | 124 | ENDIF 125 | 126 | inc ecx 127 | 128 | END_WHILE 129 | 130 | xor eax, eax 131 | 132 | IF dword [nb_cor], le, 0 133 | 134 | mov eax, -1 135 | 136 | ENDIF 137 | 138 | popx ecx 139 | 140 | endproc 141 | -------------------------------------------------------------------------------- /src/functions/my_ldi.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: my_ldi.asm 4 | ;; Author: chapui_s 5 | ;; Created: 21/07/2014 21:46:17 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .data 14 | 15 | str1: db 'LDI: first = %d second = %d read_value = %d', 10, 0 16 | str2: db 'PTR LDI = %d', 10, 0 17 | 18 | section .text 19 | 20 | extern get_first_value 21 | extern get_second_value 22 | extern read_arena 23 | extern is_register 24 | extern is_good_register 25 | 26 | extern printf 27 | 28 | proc my_ldi, core, champions, instruction 29 | 30 | _var_ first_value, second_value, read_value, idx_mod 31 | 32 | pushx edx 33 | 34 | mov dword [idx_mod], IDX_MOD 35 | 36 | invoke get_first_value, [core], [champions], [instruction], 0 37 | mov dword [first_value], eax 38 | 39 | invoke get_second_value, [core], [champions], [instruction] 40 | mov dword [second_value], eax 41 | 42 | .TEST nop 43 | 44 | mov eax, [first_value] 45 | add eax, dword [second_value] 46 | xor edx, edx 47 | cmp eax, 0 48 | jge .NO 49 | not edx 50 | .NO idiv dword [idx_mod] 51 | mov dword [read_value], edx 52 | 53 | mov eax, [champions] 54 | mov eax, [eax + s_champions.pc] 55 | sub eax, 6 56 | add eax, dword [read_value] 57 | 58 | ; pushx eax, ebx, ecx, edx 59 | ; invoke printf, str2, eax 60 | ; popx eax, ebx, ecx, edx 61 | 62 | invoke read_arena, [core], eax, 4 63 | mov dword [read_value], eax 64 | 65 | mov eax, [instruction] 66 | movzx edx, byte [eax + s_instruction.type] 67 | 68 | ; pushx eax, ebx, ecx, edx 69 | ; invoke printf, str1, [first_value], [second_value], [read_value] 70 | ; popx eax, ebx, ecx, edx 71 | 72 | 73 | invoke is_register, edx, 3 74 | cmp eax, 1 75 | jne .END 76 | 77 | mov eax, [instruction] 78 | mov eax, [eax + s_instruction.params + 8] 79 | mov edx, eax 80 | invoke is_good_register, eax 81 | cmp eax, 1 82 | jne .END 83 | 84 | mov eax, edx 85 | shl eax, 2 86 | mov edx, [champions] 87 | mov edx, [edx + s_champions.reg] 88 | add edx, eax 89 | mov eax, [read_value] 90 | mov [edx], eax 91 | 92 | cmp eax, 0 93 | je .ONE 94 | 95 | xor eax, eax 96 | jmp .CARRY 97 | 98 | .ONE mov eax, 1 99 | 100 | .CARRY mov edx, [champions] 101 | mov [edx + s_champions.carry], eax 102 | 103 | .END xor eax, eax 104 | 105 | popx edx 106 | 107 | endproc -------------------------------------------------------------------------------- /src/functions/get_value.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: get_value.asm 4 | ;; Author: chapui_s 5 | ;; Created: 21/07/2014 19:44:16 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .data 14 | 15 | str1: db 'GET_VALUE: %d', 10, 0 16 | str2: db '[REGISTER] ', 0 17 | 18 | section .text 19 | 20 | extern get_size_param 21 | extern is_direct 22 | extern is_indirect 23 | extern is_register 24 | extern is_good_register 25 | extern read_arena 26 | 27 | extern my_putstr 28 | extern my_putnbr 29 | 30 | proc get_value, core, champions, instruction, param 31 | 32 | _var_ value, pc, size_param, other, idx_mod, param_1, mem_size 33 | 34 | pushx ecx, edx 35 | 36 | mov dword [idx_mod], IDX_MOD 37 | mov dword [mem_size], MEM_SIZE 38 | 39 | mov eax, [param] 40 | dec eax 41 | shl eax, 2 42 | mov edx, [instruction] 43 | add edx, s_instruction.params 44 | add eax, edx 45 | mov eax, [eax] 46 | mov [param_1], eax 47 | 48 | mov eax, [instruction] 49 | movzx edx, byte [eax + s_instruction.type] 50 | 51 | .IF invoke is_register, edx, [param] 52 | cmp eax, 1 53 | jne .ELSIF1 54 | 55 | invoke is_good_register, [param_1] 56 | cmp eax, 1 57 | jne .END 58 | 59 | ; invoke my_putnbr, [param_1] 60 | 61 | mov eax, [champions] 62 | mov eax, [eax + s_champions.reg] 63 | mov edx, [param_1] 64 | shl edx, 2 65 | add eax, edx 66 | ; add eax, [param_1] 67 | 68 | mov eax, [eax] 69 | mov [value], eax 70 | jmp .END 71 | 72 | .ELSIF1 invoke is_direct, edx, [param] 73 | cmp eax, 1 74 | jne .ELSIF2 75 | 76 | mov eax, [param_1] 77 | mov [value], eax 78 | jmp .END 79 | 80 | .ELSIF2 invoke is_indirect, edx, [param] 81 | cmp eax, 1 82 | jne .END 83 | 84 | mov eax, [champions] 85 | mov eax, [eax + s_champions.pc] 86 | mov [pc], eax 87 | 88 | invoke get_size_param, edx 89 | mov [size_param], eax 90 | 91 | mov eax, [param_1] 92 | cdq 93 | idiv dword [idx_mod] 94 | mov [other], edx 95 | 96 | mov eax, [pc] 97 | sub eax, dword [size_param] 98 | add eax, dword [other] 99 | 100 | cdq 101 | idiv dword [mem_size] 102 | 103 | invoke read_arena, [core], edx, 4 104 | mov [value], eax 105 | 106 | .END mov eax, [value] 107 | 108 | ; pushx eax, ebx, ecx, edx 109 | ; invoke printf, str1, eax 110 | ; popx eax, ebx, ecx, edx 111 | 112 | popx ecx, edx 113 | 114 | endproc -------------------------------------------------------------------------------- /src/gui/disp_arena.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: disp_arena.asm 4 | ;; Author: chapui_s 5 | ;; Created: 16/07/2014 21:46:57 (+08:00 UTC) 6 | ;; Updated: 6/08/2014 17:47:11 (+08:00 UTC) 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .data 14 | 15 | str1: db 'error: SDL_FillRect', 10, 0 16 | str2: db 'error: SDL_BlitSurface', 10, 0 17 | str3: db 'error: SDL_Flip', 10, 0 18 | 19 | const: dd 5 20 | 21 | section .text 22 | 23 | %define SDL_BlitSurface SDL_UpperBlit 24 | 25 | extern SDL_FillRect 26 | extern SDL_BlitSurface 27 | extern SDL_Flip 28 | extern get_list_pc 29 | extern print_bytes 30 | extern my_putstr 31 | extern disp_info_players 32 | 33 | extern disp_core 34 | extern disp_gui 35 | 36 | global disp_arena 37 | 38 | disp_arena: 39 | 40 | push ebp 41 | mov ebp, esp 42 | 43 | push edx 44 | 45 | cmp dword [ebp + 20], 1 46 | je .START 47 | 48 | mov edx, 0 49 | mov eax, [ebp + 16] 50 | div dword [const] 51 | xor eax, eax 52 | cmp edx, 0 53 | jne .END 54 | 55 | .START mov eax, [ebp + 12] 56 | 57 | push dword 0 58 | push dword 0 59 | push dword [eax + s_gui.screen] 60 | call SDL_FillRect wrt ..plt 61 | add esp, 12 62 | cmp eax, 0 63 | jl .FAILFR 64 | 65 | mov eax, [ebp + 12] 66 | lea edx, [eax + s_gui.pos_background] 67 | push dword edx 68 | push dword [eax + s_gui.screen] 69 | push dword 0 70 | push dword [eax + s_gui.background] 71 | call SDL_BlitSurface 72 | add esp, 16 73 | cmp eax, 0 74 | jl .FAILB 75 | 76 | push dword [ebp + 20] 77 | push dword [ebp + 16] 78 | push dword [ebp + 12] 79 | push dword [ebp + 8] 80 | call disp_info_players 81 | add esp, 16 82 | cmp eax, -1 83 | je .FAIL 84 | 85 | 86 | push dword [ebp + 12] 87 | push dword [ebp + 8] 88 | call get_list_pc 89 | add esp, 8 90 | 91 | push dword [ebp + 12] 92 | .TEST push dword [ebp + 8] 93 | call print_bytes 94 | add esp, 8 95 | cmp eax, -1 96 | je .FAIL 97 | 98 | mov eax, [ebp + 12] 99 | push dword [eax + s_gui.screen] 100 | call SDL_Flip 101 | add esp, 4 102 | cmp eax, -1 103 | je .FAILF 104 | 105 | xor eax, eax 106 | jmp .END 107 | 108 | .FAILF push str3 109 | jmp .STR 110 | 111 | .FAILB push str2 112 | jmp .STR 113 | 114 | .FAILFR push str1 115 | jmp .STR 116 | 117 | .STR call my_putstr 118 | add esp, 4 119 | 120 | .FAIL mov eax, -1 121 | 122 | .END pop edx 123 | mov esp, ebp 124 | pop ebp 125 | ret 126 | -------------------------------------------------------------------------------- /src/functions/my_fork.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: my_fork.asm 4 | ;; Author: chapui_s 5 | ;; Created: 21/07/2014 20:25:39 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .text 14 | 15 | extern disp_reg 16 | 17 | extern push_champion 18 | extern copy_reg 19 | 20 | proc my_fork, core, champions, instruction 21 | 22 | _var_ new_pc, param0, mem_size, idx_mod 23 | 24 | pushx ebx, edx 25 | 26 | mov dword [mem_size], MEM_SIZE 27 | mov dword [idx_mod], IDX_MOD 28 | 29 | mov eax, [instruction] 30 | add eax, s_instruction.params 31 | cmp dword [eax], 0 32 | jne .NEXT 33 | 34 | mov dword [eax], 3 35 | 36 | .NEXT mov eax, [eax] 37 | ; xor edx, edx 38 | ; cmp eax, 0 39 | ; jge .NO 40 | ; mov edx, -1 41 | cdq 42 | .NO idiv dword [idx_mod] 43 | mov [param0], edx 44 | mov eax, [champions] 45 | mov eax, [eax + s_champions.pc] 46 | sub eax, 3 47 | add eax, [param0] 48 | mov dword [new_pc], eax 49 | 50 | .LOOP cmp dword [new_pc], 0 51 | jge .ENDL 52 | 53 | add dword [new_pc], MEM_SIZE 54 | jmp .LOOP 55 | 56 | .ENDL mov eax, [new_pc] 57 | cdq 58 | idiv dword [mem_size] 59 | mov [new_pc], edx 60 | 61 | mov eax, [champions] 62 | invoke push_champion, [core], [eax + s_champions.filename], [eax + s_champions.prog_number], [new_pc] 63 | _check_ .FAIL, -1 64 | 65 | mov eax, [core] 66 | mov eax, [eax + s_corewar.last_champions] 67 | mov ebx, eax 68 | 69 | mov eax, [champions] 70 | mov edx, [eax + s_champions.name] 71 | mov [ebx + s_champions.name], edx 72 | mov edx, [eax + s_champions.comment] 73 | mov [ebx + s_champions.comment], edx 74 | mov edx, [core] 75 | inc dword [edx + s_corewar.prog_number_max] 76 | mov edx, [edx + s_corewar.prog_number_max] 77 | mov [ebx + s_champions.prog_number], edx 78 | mov edx, [eax + s_champions.carry] 79 | mov [ebx + s_champions.carry], edx 80 | mov dword [ebx + s_champions.last_live], 0 81 | mov dword [ebx + s_champions.cycle_to_wait], 2 82 | mov edx, [eax + s_champions.color_gui] 83 | mov [ebx + s_champions.color_gui], edx 84 | mov edx, [new_pc] 85 | mov [ebx + s_champions.pc], edx 86 | 87 | invoke copy_reg, [eax + s_champions.reg] 88 | _check_ .FAIL, 0 89 | 90 | ; invoke disp_reg, eax 91 | 92 | mov [ebx + s_champions.reg], eax 93 | mov eax, [core] 94 | inc dword [eax + s_corewar.nb_champions] 95 | 96 | xor eax, eax 97 | jmp .END 98 | 99 | .FAIL mov eax, -1 100 | 101 | .END popx ebx, edx 102 | 103 | 104 | endproc -------------------------------------------------------------------------------- /src/desassembleur/print_instruction.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: print_instruction.asm 4 | ;; Author: chapui_s 5 | ;; Created: 6/08/2014 18:04:49 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .data 14 | 15 | op_code: dd 1, 2, 3, 4, 5,6 ,7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 0 16 | 17 | live: db 'live', 0 18 | ld: db 'ld', 0 19 | l_st: db 'st', 0 20 | l_add: db 'add', 0 21 | l_sub: db 'sub', 0 22 | l_and: db 'and', 0 23 | l_or: db 'or', 0 24 | l_xor: db 'xor', 0 25 | zjmp: db 'zjmp', 0 26 | ldi: db 'ldi', 0 27 | l_sti: db 'sti', 0 28 | fork: db 'fork', 0 29 | lld: db 'lld', 0 30 | lldi: db 'lldi', 0 31 | lfork: db 'lfork', 0 32 | aff: db 'aff', 0 33 | 34 | op_memn_tab: dd live, ld, l_st, l_add, l_sub, l_and, l_or, l_xor, zjmp, ldi, l_sti, fork, lld, lldi, lfork, aff 35 | 36 | section .text 37 | 38 | extern my_putstr 39 | extern my_putchar 40 | extern print_args 41 | extern my_putnbr 42 | 43 | proc print_instruction, instruction 44 | 45 | _var_ tmp_type 46 | 47 | pushx eax, ecx, edx 48 | 49 | xor ecx, ecx 50 | mov eax, [instruction] 51 | movzx eax, byte [eax + s_instruction.type] 52 | shr eax, 2 53 | shl eax, 2 54 | mov [tmp_type], eax 55 | 56 | mov eax, op_code 57 | mov edx, [instruction] 58 | movzx edx, byte [edx + s_instruction.code] 59 | 60 | WHILE dword [eax + (ecx * 4)], ne, 0 61 | 62 | cmp [eax + (ecx * 4)], edx 63 | jz .OK 64 | 65 | inc ecx 66 | 67 | END_WHILE 68 | 69 | .OK IF dword [eax + (ecx * 4)], ne, 0 70 | 71 | mov edx, op_memn_tab 72 | mov edx, [edx + (ecx * 4)] 73 | 74 | invoke my_putstr, edx 75 | invoke my_putchar, 9 ; \t 76 | 77 | mov edx, [instruction] 78 | movzx edx, byte [edx + s_instruction.code] 79 | 80 | cmp dl, LIVE 81 | jz .DO 82 | 83 | cmp dl, ZJMP 84 | jz .DO 85 | 86 | cmp dl, FORK 87 | jz .DO 88 | 89 | cmp dl, LFORK 90 | jz .DO 91 | 92 | jmp .NO 93 | 94 | 95 | .DO IF dl, e, LIVE 96 | 97 | invoke my_putchar, DIRECT_CHAR 98 | 99 | ENDIF 100 | 101 | mov edx, [instruction] 102 | mov edx, [edx + s_instruction.params] 103 | 104 | invoke my_putnbr, edx 105 | 106 | .NO xor ecx, ecx 107 | 108 | .LOOP mov eax, [tmp_type] 109 | and al, 0b11000000 110 | cmp al, 0 111 | jz .ENDL 112 | 113 | lea eax, [tmp_type] 114 | invoke print_args, eax, [instruction], ecx 115 | 116 | inc ecx 117 | jmp .LOOP 118 | 119 | ENDIF 120 | 121 | .ENDL popx eax, ecx, edx 122 | 123 | endproc -------------------------------------------------------------------------------- /src/exec_instructions/get_instruction.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: get_instruction.asm 4 | ;; Author: chapui_s 5 | ;; Created: 20/07/2014 00:38:26 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .data 14 | 15 | str1: db 'code = %d type = %x p1 = %d p2 = %d p3 = %d p4 = %d', 10, 0 16 | str2: db 'pc = %d', 10, 0 17 | str3: db 'code = %d', 10, 0 18 | 19 | section .text 20 | 21 | extern get_type_and_param 22 | 23 | proc disp_inst, instruction1 24 | 25 | pushx eax, ecx, edx 26 | 27 | mov eax, [instruction1] 28 | 29 | mov edx, [eax + s_instruction.params + 12] 30 | push edx 31 | mov edx, [eax + s_instruction.params + 8] 32 | push edx 33 | mov edx, [eax + s_instruction.params + 4] 34 | push edx 35 | mov edx, [eax + s_instruction.params] 36 | push edx 37 | mov edx, 0 38 | mov dl, [eax + s_instruction.type] 39 | push edx 40 | mov dl, [eax + s_instruction.code] 41 | push edx 42 | push str1 43 | call printf 44 | add esp, 32 45 | 46 | popx eax, ecx, edx 47 | 48 | endproc 49 | 50 | proc get_instruction, core, champions, instruction 51 | 52 | _var_ pc, mem_size 53 | 54 | pushx eax, edx 55 | 56 | mov dword [mem_size], MEM_SIZE 57 | 58 | mov eax, [champions] 59 | mov eax, [eax + s_champions.pc] 60 | 61 | .LOOP cmp eax, 0 62 | jge .ENDL 63 | 64 | add eax, MEM_SIZE 65 | jmp .LOOP 66 | 67 | .ENDL cdq 68 | idiv dword [mem_size] 69 | mov eax, [champions] 70 | mov [eax + s_champions.pc], edx 71 | 72 | mov eax, [core] 73 | mov eax, [eax + s_corewar.arena] 74 | movzx edx, byte [eax + edx] 75 | 76 | mov eax, [instruction] 77 | mov [eax + s_instruction.code], dl 78 | mov dword [eax + s_instruction.params], 0 79 | mov dword [eax + s_instruction.params + 4], 0 80 | mov dword [eax + s_instruction.params + 8], 0 81 | mov dword [eax + s_instruction.params + 12], 0 82 | 83 | cmp dl, 1 84 | jl .ELSE 85 | 86 | cmp dl, 16 87 | jg .ELSE 88 | 89 | invoke get_type_and_param, [core], [champions], [instruction] 90 | jmp .END 91 | 92 | .ELSE mov eax, [champions] 93 | 94 | mov dword [eax + s_champions.carry], 0 95 | mov eax, [eax + s_champions.pc] 96 | 97 | inc eax 98 | cdq 99 | idiv dword [mem_size] 100 | mov eax, [champions] 101 | mov dword [eax + s_champions.pc], edx 102 | 103 | .END popx eax, edx 104 | 105 | ; pushx eax, ebx, ecx, edx 106 | ; invoke disp_inst, [instruction] 107 | ; popx eax, ebx, ecx, edx 108 | 109 | endproc -------------------------------------------------------------------------------- /src/utils/write_arena_four.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: write_arena_four.asm 4 | ;; Author: chapui_s 5 | ;; Created: 19/07/2014 23:21:05 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .text 14 | 15 | proc write_arena_four, core, champions, to_write, index 16 | 17 | _var_ mem_size, index_mod, index_one_mod, index_two_mod, index_three_mod, color 18 | 19 | pushx eax, edx 20 | 21 | mov dword [mem_size], MEM_SIZE 22 | mov eax, [champions] 23 | mov eax, [eax + s_champions.color_gui] 24 | 25 | ; inc eax 26 | 27 | mov [color], eax 28 | 29 | .LOOP cmp dword [index], 0 30 | jge .ENDL 31 | 32 | mov eax, [index] 33 | add eax, MEM_SIZE 34 | mov [index], eax 35 | jmp .LOOP 36 | 37 | .ENDL mov eax, [index] 38 | cdq 39 | idiv dword [mem_size] 40 | mov [index_mod], edx 41 | 42 | mov eax, [index] 43 | inc eax 44 | cdq 45 | idiv dword [mem_size] 46 | mov [index_one_mod], edx 47 | 48 | mov eax, [index] 49 | add eax, 2 50 | cdq 51 | idiv dword [mem_size] 52 | mov [index_two_mod], edx 53 | 54 | mov eax, [index] 55 | add eax, 3 56 | cdq 57 | idiv dword [mem_size] 58 | mov [index_three_mod], edx 59 | 60 | mov eax, [core] 61 | mov eax, [eax + s_corewar.arena] 62 | add eax, [index_mod] 63 | mov edx, [to_write] 64 | shr edx, 24 65 | mov byte [eax], dl 66 | 67 | mov eax, [core] 68 | mov eax, [eax + s_corewar.info_arena] 69 | add eax, [index_mod] 70 | mov edx, [color] 71 | mov byte [eax], dl 72 | 73 | mov eax, [core] 74 | mov eax, [eax + s_corewar.arena] 75 | add eax, [index_one_mod] 76 | mov edx, [to_write] 77 | and edx, 0b00000000111111110000000000000000 78 | shr edx, 16 79 | mov byte [eax], dl 80 | 81 | mov eax, [core] 82 | mov eax, [eax + s_corewar.info_arena] 83 | add eax, [index_one_mod] 84 | mov edx, [color] 85 | mov byte [eax], dl 86 | 87 | mov eax, [core] 88 | mov eax, [eax + s_corewar.arena] 89 | add eax, [index_two_mod] 90 | mov edx, [to_write] 91 | and edx, 0b00000000000000001111111100000000 92 | shr edx, 8 93 | mov byte [eax], dl 94 | 95 | mov eax, [core] 96 | mov eax, [eax + s_corewar.info_arena] 97 | add eax, [index_two_mod] 98 | mov edx, [color] 99 | mov byte [eax], dl 100 | 101 | mov eax, [core] 102 | mov eax, [eax + s_corewar.arena] 103 | add eax, [index_three_mod] 104 | mov edx, [to_write] 105 | and edx, 0b00000000000000000000000011111111 106 | mov byte [eax], dl 107 | 108 | mov eax, [core] 109 | mov eax, [eax + s_corewar.info_arena] 110 | add eax, [index_three_mod] 111 | mov edx, [color] 112 | mov byte [eax], dl 113 | 114 | popx eax, edx 115 | 116 | endproc -------------------------------------------------------------------------------- /includes/macro.inc: -------------------------------------------------------------------------------- 1 | 2 | %ifndef MACRO_INC 3 | %define MACRO_INC 4 | 5 | %macro invoke 2-* 6 | %define _fonction_name %1 7 | %assign _nb_params (%0-1) * 4 8 | %rep %0-1 9 | %rotate -1 10 | push dword %1 11 | %endrep 12 | call _fonction_name 13 | add esp, _nb_params 14 | %endmacro 15 | 16 | %macro pushx 1-* 17 | %rep %0 18 | push %1 19 | %rotate 1 20 | %endrep 21 | %endmacro 22 | 23 | %macro popx 1-* 24 | %rep %0 25 | %rotate -1 26 | pop %1 27 | %endrep 28 | %endmacro 29 | 30 | %macro _var_ 1-* 31 | %assign _space (%0 * 4) 32 | sub esp, _space 33 | %assign _space_cur 4 34 | %rep %0 35 | %xdefine %1 ebp - _space_cur 36 | %rotate 1 37 | %assign _space_cur _space_cur + 4 38 | %endrep 39 | %endmacro 40 | 41 | %macro proc 1-* 42 | %push context 43 | global %1 44 | %1: 45 | %assign _cur 8 46 | %rotate 1 47 | %rep %0-1 48 | %xdefine %1 ebp + _cur 49 | %assign _cur _cur + 4 50 | %rotate 1 51 | %endrep 52 | push ebp 53 | mov ebp, esp 54 | %endmacro 55 | 56 | %macro RET 0 57 | %ifctx context 58 | jmp %$end_proc 59 | %else 60 | %error "RET sans 'proc' !" 61 | %endif 62 | %endmacro 63 | 64 | %macro endproc 0 65 | %$end_proc: 66 | .ENDPROC nop 67 | mov esp, ebp 68 | pop ebp 69 | ret 70 | %ifctx context 71 | %pop 72 | %else 73 | %error 'fucking error' 74 | %endif 75 | %endmacro 76 | 77 | %macro entryproc 0 78 | proc main 79 | %endmacro 80 | 81 | %macro IF 3 82 | %push if 83 | %assign __curr 1 84 | cmp %1, %3 85 | j%-2 %{$loc}__curr 86 | %endmacro 87 | 88 | %macro ELSEIF 3 89 | %ifctx if 90 | jmp %$end_if 91 | %{$loc}__curr: 92 | %assign __curr __curr+1 93 | cmp %1, %3 94 | j%-2 %{$loc}__curr 95 | %else 96 | %error "T'as oublie un if !" 97 | %endif 98 | %endmacro 99 | 100 | %macro ELSE 0 101 | %ifctx if 102 | jmp %$end_if 103 | %{$loc}__curr: 104 | %assign __curr __curr+1 105 | %else 106 | %error "T'as oublie un if !" 107 | %endif 108 | %endmacro 109 | 110 | %macro ENDIF 0 111 | %ifctx if 112 | %{$loc}__curr: 113 | %$end_if: 114 | %pop 115 | %else 116 | %error "T'as oublie un if !" 117 | %endif 118 | %endmacro 119 | 120 | ; macro _check_ : .label value [condition] 121 | %macro _check_ 2-3 122 | %if %0 > 2 123 | cmp eax, %2 124 | j%+3 %1 125 | %else 126 | cmp eax, %2 127 | je %1 128 | %endif 129 | %endmacro 130 | 131 | %macro WHILE 3 132 | %push while 133 | %$test_while: 134 | cmp %1, %3 135 | j%-2 %$end_while 136 | %endmacro 137 | 138 | %macro END_WHILE 0 139 | %ifctx while 140 | jmp %$test_while 141 | %$end_while: 142 | %pop 143 | %else 144 | %error "T'as oublie un while !" 145 | %endif 146 | %endmacro 147 | 148 | %define .endproc .ENDPROC 149 | 150 | %endif 151 | -------------------------------------------------------------------------------- /src/functions/get_first_value.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: get_first_value.asm 4 | ;; Author: chapui_s 5 | ;; Created: 21/07/2014 21:13:27 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .text 14 | 15 | extern is_register 16 | extern is_direct 17 | extern is_indirect 18 | extern read_arena 19 | extern is_good_register 20 | 21 | proc get_first_value, core, champions, instruction, is_ll 22 | 23 | _var_ first_value, ptr, param0, idx_mod, pc, param0_mod 24 | 25 | pushx edx 26 | 27 | mov dword [first_value], 0 28 | mov dword [idx_mod], IDX_MOD 29 | 30 | mov eax, [champions] 31 | mov eax, [eax + s_champions.pc] 32 | mov dword [pc], eax 33 | 34 | mov eax, [instruction] 35 | mov eax, [eax + s_instruction.params] 36 | 37 | mov [param0], eax 38 | 39 | cmp dword [is_ll], 0 40 | jne .NO_LL 41 | 42 | cdq 43 | idiv dword [idx_mod] 44 | mov eax, edx 45 | 46 | .NO_LL mov [param0_mod], eax 47 | 48 | mov eax, [instruction] 49 | movzx edx, byte [eax + s_instruction.type] 50 | 51 | .IF invoke is_register, edx, 1 52 | cmp eax, 1 53 | jne .ELSI1 54 | 55 | invoke is_good_register, [param0] 56 | cmp eax, 1 57 | jne .END 58 | 59 | mov eax, [param0] 60 | shl eax, 2 61 | mov edx, [champions] 62 | mov edx, [edx + s_champions.reg] 63 | add edx, eax 64 | mov eax, [edx] 65 | 66 | cmp dword [is_ll], 0 67 | jne .NOT2 68 | 69 | cdq 70 | idiv dword [idx_mod] 71 | mov eax, edx 72 | 73 | .NOT2 mov edx, [pc] 74 | sub edx, 6 75 | add edx, eax 76 | 77 | invoke read_arena, [core], edx, 2 78 | mov dword [first_value], eax 79 | jmp .END 80 | 81 | .ELSI1 invoke is_direct, edx, 1 82 | cmp eax, 1 83 | jne .ELSI2 84 | 85 | mov eax, [pc] 86 | sub eax, 6 87 | 88 | cmp dword [is_ll], 0 89 | jne .NO3 90 | add eax, dword [param0_mod] 91 | jmp .READ 92 | .NO3 add eax, dword [param0] 93 | 94 | .READ invoke read_arena, [core], eax, 2 95 | mov dword [first_value], eax 96 | jmp .END 97 | 98 | .ELSI2 invoke is_indirect, edx, 1 99 | cmp eax, 1 100 | jne .END 101 | 102 | mov eax, [pc] 103 | sub eax, 6 104 | cmp dword [is_ll], 0 105 | jne .NO4 106 | add eax, dword [param0_mod] 107 | jmp .READ2 108 | .NO4 add eax, dword [param0] 109 | 110 | .READ2 invoke read_arena, [core], eax, 2 111 | mov dword [first_value], eax 112 | 113 | .END mov eax, [first_value] 114 | 115 | popx edx 116 | 117 | endproc -------------------------------------------------------------------------------- /src/gui/disp_info_players.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: disp_info_players.asm 4 | ;; Author: chapui_s 5 | ;; Created: 18/07/2014 19:13:50 (+08:00 UTC) 6 | ;; Updated: 19/07/2014 18:07:37 (+08:00 UTC) 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .data 14 | 15 | str1: db 'PLAYING', 0 16 | str2: db 'PAUSED', 0 17 | str3: db 'error: SDL_BlitSurface', 10, 0 18 | 19 | str: times 50 db 0 20 | position: times 2 dd 0 21 | 22 | section .text 23 | 24 | extern TTF_RenderText_Solid 25 | extern int_to_str 26 | extern SDL_BlitSurface 27 | extern my_putstr 28 | extern disp_players 29 | 30 | proc disp_info_players, core, gui, cycles, paused 31 | 32 | _var_ color, font_info, screen, byte_arena 33 | 34 | invoke disp_players, [gui] 35 | _check_ .FAIL, -1 36 | 37 | mov dword [color], 0xffffffff 38 | mov eax, [gui] 39 | mov edx, [eax + s_gui.font_info] 40 | mov [font_info], edx 41 | mov edx, [eax + s_gui.screen] 42 | mov [screen], edx 43 | 44 | mov eax, [core] 45 | mov eax, [eax + s_corewar.nb_champions] 46 | invoke int_to_str, eax, str 47 | 48 | .PLAY cmp dword [paused], 0 49 | jne .PAUSE 50 | 51 | mov eax, str1 52 | jmp .THEN 53 | 54 | .PAUSE mov eax, str2 55 | 56 | .THEN invoke TTF_RenderText_Solid, [font_info], eax, [color] 57 | mov edx, [gui] 58 | mov [edx + s_gui.byte_arena], eax 59 | mov [byte_arena], eax 60 | 61 | mov eax, [gui] 62 | mov eax, [eax + s_gui.byte_arena] 63 | mov dx, word [eax + SDL_Surface.w] 64 | mov eax, 0 65 | mov ax, dx 66 | mov edx, 0 67 | mov ebx, 2 68 | div ebx 69 | 70 | mov edx, (WIN_X / 2) 71 | sub edx, eax 72 | 73 | lea eax, [position] 74 | mov word [eax + SDL_Rect.x], dx 75 | mov word [eax + SDL_Rect.y], 710 76 | 77 | lea eax, [position] 78 | invoke SDL_BlitSurface, [byte_arena], 0, [screen], eax 79 | _check_ .FAILB, 0, l 80 | 81 | invoke TTF_RenderText_Solid, [font_info], str, [color] 82 | mov edx, [gui] 83 | mov [edx + s_gui.byte_arena], eax 84 | mov [byte_arena], eax 85 | 86 | mov word [position + SDL_Rect.x], (WIN_X - 70) 87 | mov word [position + SDL_Rect.y], 725 88 | 89 | lea eax, [position] 90 | invoke SDL_BlitSurface, [byte_arena], 0, [screen], eax 91 | _check_ .FAILB, -1 92 | 93 | invoke int_to_str, [cycles], str 94 | invoke TTF_RenderText_Solid, [font_info], str, [color] 95 | mov edx, [gui] 96 | mov [edx + s_gui.byte_arena], eax 97 | mov [byte_arena], eax 98 | 99 | mov word [position + SDL_Rect.y], 745 100 | 101 | lea eax, [position] 102 | invoke SDL_BlitSurface, [byte_arena], 0, [screen], eax 103 | _check_ .FAILB, -1 104 | 105 | mov eax, [core] 106 | mov eax, [eax + s_corewar.cycle_to_die_cur] 107 | invoke int_to_str, eax, str 108 | invoke TTF_RenderText_Solid, [font_info], str, [color] 109 | mov edx, [gui] 110 | mov [edx + s_gui.byte_arena], eax 111 | mov [byte_arena], eax 112 | 113 | mov word [position + SDL_Rect.y], 765 114 | 115 | lea eax, [position] 116 | invoke SDL_BlitSurface, [byte_arena], 0, [screen], eax 117 | _check_ .FAILB, -1 118 | 119 | xor eax, eax 120 | jmp .ENDPROC 121 | 122 | .FAILB invoke my_putstr, str3 123 | .FAIL mov eax, -1 124 | 125 | endproc 126 | -------------------------------------------------------------------------------- /includes/corewar.inc: -------------------------------------------------------------------------------- 1 | 2 | %ifndef COREWAR_INC 3 | %define COREWAR_INC 4 | 5 | %include "macro.inc" 6 | %include "SDL_definitions.inc" 7 | 8 | struc s_instruction 9 | .code: resb 1 10 | .type: resb 1 11 | .params: resd 4 12 | endstruc 13 | 14 | struc s_corewar 15 | .arena: resd 1 ; char * 16 | .info_arena: resd 1 ; char * 17 | .nb_champions: resd 1 ; unsigned int 18 | .champions: resd 1 ; t_champions * 19 | .last_champions: resd 1 ; t_champions * 20 | .last_live_nb: resd 1 ; int 21 | .last_live_name: resd 1 ; char * 22 | .prog_number_max: resd 1 ; unsigned int 23 | .nbr_cycle_dump: resd 1 ; int 24 | .nbr_live_cur: resd 1 ; int 25 | .is_desassembler: resd 1 ; int 26 | .cycle_to_die_cur: resd 1 ; int 27 | endstruc 28 | 29 | struc s_champions 30 | .filename: resd 1 ; char * 31 | .size: resd 1 ; int 32 | .prog_number: resd 1 ; unsigned int 33 | .load_address: resd 1 ; unsigned int 34 | .name: resd 1 ; char * 35 | .comment: resd 1 ; char * 36 | .reg: resd 1 ; int * 37 | .pc: resd 1 ; int 38 | .carry: resd 1 ; unsigned int 39 | .last_live: resd 1 ; unsigned int 40 | .cycle_to_wait: resd 1 ; int 41 | .color_gui: resd 1 ; int 42 | .next: resd 1 ; s_champions * 43 | endstruc 44 | 45 | struc s_gui 46 | .screen: resd 1 ; SDL_Surface * 47 | .byte_arena: resd 1 ; SDL_Surface * 48 | .background: resd 1 ; SDL_Surface * 49 | .players: resd 5 ; SDL_Surface[5] * 50 | .font: resd 1 ; TTF_Font * 51 | .font_info: resd 1 ; TTF_Font * 52 | .list_pc: resd 1 ; int * 53 | .pos_background: resd 2 ; SDL_Rect 54 | .my_color: resd 1 ; SDL_Color 55 | endstruc 56 | 57 | struc s_functions 58 | .numero: resd 1 59 | .function: resd 1 60 | endstruc 61 | 62 | struc SDL_Surface 63 | .flags: resd 1 64 | .format: resd 1 65 | .w: resd 1 66 | .h: resd 1 67 | .pitch: resw 1 68 | .pixels: resd 1 69 | .offset: resd 1 70 | .hwdata: resd 1 71 | .clip_rect: resd 2 72 | .unused1: resd 1 73 | .locked: resd 1 74 | .map: resd 1 75 | .format_version: resd 1 76 | .refcount: resd 1 77 | endstruc 78 | 79 | struc SDL_Rect 80 | .x: resw 1 81 | .y: resw 1 82 | .w: resw 1 83 | .h: resw 1 84 | endstruc 85 | 86 | struc SDL_Color 87 | .r: resb 1 88 | .g: resb 1 89 | .b: resb 1 90 | .unused: resb 1 91 | endstruc 92 | 93 | extern printf 94 | extern atoi 95 | 96 | %define SIZE_S_COREWAR (48) 97 | %define SIZE_S_CHAMPIONS (52) 98 | %define MALLOC_FAILED 'error: malloc failed !', 10, 0 99 | 100 | %define MAX_PC (15000) 101 | %define WIN_X (1345) 102 | %define WIN_Y (800) 103 | %define MEM_SIZE (6 * 1024) 104 | %define CYCLE_TO_DIE (1536) 105 | %define CYCLE_DELTA (5) 106 | %define NBR_LIVE (40) 107 | %define COREWAR_EXEC_MAGIC (0xea83f3) 108 | %define PROG_NAME_LENGTH (128) 109 | %define COMMENT_LENGTH (2048) 110 | %define REG_NUMBER (16) 111 | %define IDX_MOD (512) 112 | 113 | %define O_RDONLY (0) 114 | %define SEEK_SET (0) 115 | %define SDL_INIT_VIDEO (32) 116 | %define SDL_HWSURFACE (1) 117 | %define SDL_BlitSurface SDL_UpperBlit 118 | 119 | %define LIVE (1) 120 | %define LD (2) 121 | %define ST (3) 122 | %define ADD (4) 123 | %define SUB (5) 124 | %define AND (6) 125 | %define OR (7) 126 | %define XOR (8) 127 | %define ZJMP (9) 128 | %define LDI (10) 129 | %define STI (11) 130 | %define FORK (12) 131 | %define LLD (13) 132 | %define LLDI (14) 133 | %define LFORK (15) 134 | %define AFF (16) 135 | 136 | %define SEPARATOR_CHAR (',') 137 | %define DIRECT_CHAR ('%') 138 | 139 | %endif 140 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | 2 | NAME = corewar 3 | 4 | SRCS = src/main.asm \ 5 | src/arguments.asm \ 6 | src/get_dump.asm \ 7 | src/save_args.asm \ 8 | src/save_champion.asm \ 9 | src/list_champions.asm \ 10 | src/load_file_in_arena.asm \ 11 | src/init_reg.asm \ 12 | src/load_arena.asm \ 13 | src/init_values_champions.asm \ 14 | src/find_max_prog_number.asm \ 15 | src/load_champions_in_arena.asm \ 16 | src/utils/check_args.asm \ 17 | src/utils/usage.asm \ 18 | src/utils/attribute_i_to_someone.asm \ 19 | src/utils/check_same_prog_number.asm \ 20 | src/utils/check_big_prog_number.asm \ 21 | src/utils/attribute_prog_number.asm \ 22 | src/utils/attribute_one_def.asm \ 23 | src/utils/attribute_two_def.asm \ 24 | src/utils/is_already_define.asm \ 25 | src/utils/attribute_address_defined.asm \ 26 | src/utils/attribute_address.asm \ 27 | src/utils/swap_int.asm \ 28 | src/utils/init_arena.asm \ 29 | src/utils/check_place_arena.asm \ 30 | src/utils/convert_endian.asm \ 31 | src/utils/get_magic.asm \ 32 | src/utils/get_size.asm \ 33 | src/utils/get_name.asm \ 34 | src/utils/get_name_comment_champions.asm \ 35 | src/utils/get_comment.asm \ 36 | src/utils/read_arena.asm \ 37 | src/utils/write_arena_two.asm \ 38 | src/utils/write_arena_four.asm \ 39 | src/utils/my_strcmp.asm \ 40 | src/utils/my_strlen.asm \ 41 | src/utils/my_putnbr.asm \ 42 | src/utils/my_putstr.asm \ 43 | src/utils/my_strcat.asm \ 44 | src/utils/to_negativ.asm \ 45 | src/utils/int_to_str.asm \ 46 | src/utils/my_showmem.asm \ 47 | src/gui/my_gui.asm \ 48 | src/gui/get_image_path.asm \ 49 | src/gui/load_players_name.asm \ 50 | src/gui/put_background.asm \ 51 | src/gui/get_list_pc.asm \ 52 | src/gui/is_pc.asm \ 53 | src/gui/set_color_with_pc.asm \ 54 | src/gui/hex_to_str.asm \ 55 | src/gui/get_color.asm \ 56 | src/gui/events.asm \ 57 | src/gui/print_bytes.asm \ 58 | src/gui/disp_arena.asm \ 59 | src/gui/get_arena.asm \ 60 | src/gui/disp_players.asm \ 61 | src/gui/disp_info_players.asm \ 62 | src/gui/get_color_champions.asm \ 63 | src/exec_instructions/winner.asm \ 64 | src/exec_instructions/check_first_in_list.asm \ 65 | src/exec_instructions/check_live_process.asm \ 66 | src/exec_instructions/is_special.asm \ 67 | src/exec_instructions/fulfil_dir.asm \ 68 | src/exec_instructions/fulfil_params.asm \ 69 | src/exec_instructions/get_type_and_param.asm \ 70 | src/exec_instructions/get_instruction.asm \ 71 | src/exec_instructions/exec_function.asm \ 72 | src/exec_instructions/exec_instructions.asm \ 73 | src/exec_instructions/manage_instructions.asm \ 74 | src/exec_instructions/get_cycle_to_wait.asm \ 75 | src/exec_instructions/search_who_still_alive.asm \ 76 | src/functions/is_good_register.asm \ 77 | src/functions/is_indirect.asm \ 78 | src/functions/is_register.asm \ 79 | src/functions/my_add.asm \ 80 | src/functions/my_and.asm \ 81 | src/functions/get_value.asm \ 82 | src/functions/get_size_param.asm \ 83 | src/functions/copy_reg.asm \ 84 | src/functions/my_lfork.asm \ 85 | src/functions/my_fork.asm \ 86 | src/functions/my_ld.asm \ 87 | src/functions/my_lld.asm \ 88 | src/functions/my_ldi.asm \ 89 | src/functions/my_lldi.asm \ 90 | src/functions/my_or.asm \ 91 | src/functions/my_st.asm \ 92 | src/functions/my_sti.asm \ 93 | src/functions/my_sub.asm \ 94 | src/functions/my_live.asm \ 95 | src/functions/my_zjmp.asm \ 96 | src/functions/my_xor.asm \ 97 | src/functions/get_first_value.asm \ 98 | src/functions/get_second_value.asm \ 99 | src/functions/is_direct.asm \ 100 | src/desassembleur/print_instruction.asm \ 101 | src/desassembleur/my_desassembler.asm \ 102 | src/desassembleur/desassemble_it.asm \ 103 | src/desassembleur/print_args.asm 104 | 105 | 106 | OBJS = $(SRCS:.asm=.o) 107 | 108 | NASM = nasm 109 | 110 | CC = gcc 111 | 112 | INC = -I includes/ 113 | 114 | NASMFLAGS = -f elf $(INC) -Werror 115 | 116 | CFLAGS = -lSDL -lSDL_ttf -s 117 | #CFLAGS = -L./lib -lSDL -lSDL_ttf -lpthread -lm -ldl -Xlinker "-rpath=./lib" 118 | 119 | RM = rm -f 120 | 121 | all: $(NAME) 122 | 123 | $(NAME): $(OBJS) 124 | @echo -e "\033[0;032mASM files assembled\033[0;0m" 125 | $(CC) -m32 -o $(NAME) $(OBJS) $(CFLAGS) 126 | @echo -e "\033[0;032m[$(NAME)] Linked\033[0;0m" 127 | 128 | %.o: %.asm 129 | $(NASM) $(NASMFLAGS) -o $@ $< 130 | 131 | clean: 132 | @echo -e "\033[0;031m[clean] " | tr -d '\n' 133 | $(RM) $(OBJS) 134 | @echo -e "\033[0;0m" | tr -d '\n' 135 | 136 | fclean: clean 137 | @echo -e "\033[0;031m[fclean] " | tr -d '\n' 138 | $(RM) $(NAME) 139 | @echo -e "\033[0;0m" | tr -d '\n' 140 | 141 | re: fclean all 142 | -------------------------------------------------------------------------------- /src/functions/my_sti.asm: -------------------------------------------------------------------------------- 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 | ;; 3 | ;; Filename: my_sti.asm 4 | ;; Author: chapui_s 5 | ;; Created: 23/07/2014 23:04:54 (+08:00 UTC) 6 | ;; Updated: 7 | ;; URL: https://github.com/sebastiencs/ 8 | ;; 9 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | 11 | %include "corewar.inc" 12 | 13 | section .data 14 | 15 | str1: db 'STI ptr = %d', 10, 0 16 | str2: db 'STI value_to_store = %d value2 = %d value3 = %d', 10, 0 17 | 18 | section .text 19 | 20 | extern is_good_register 21 | extern is_register 22 | extern is_direct 23 | extern is_indirect 24 | extern write_arena_four 25 | 26 | extern disp_inst 27 | 28 | proc get_value_to_store, core1, champions1, instruction1 29 | 30 | _var_ value, param0 31 | 32 | pushx edx 33 | 34 | mov dword [value], 0 35 | 36 | mov eax, [instruction1] 37 | movzx edx, byte [eax + s_instruction.type] 38 | mov eax, [eax + s_instruction.params] 39 | mov [param0], eax 40 | 41 | invoke is_register, edx, 1 42 | cmp eax, 1 43 | jne .END 44 | 45 | invoke is_good_register, [param0] 46 | cmp eax, 1 47 | jne .END 48 | 49 | mov eax, [param0] 50 | shl eax, 2 51 | 52 | mov edx, [champions1] 53 | mov edx, [edx + s_champions.reg] 54 | add edx, eax 55 | mov eax, [edx] 56 | mov dword [value], eax 57 | 58 | .END mov eax, [value] 59 | popx edx 60 | 61 | endproc 62 | 63 | proc get_second_value_sti, core2, champions2, instruction2 64 | 65 | _var_ second_value, param1 66 | 67 | pushx edx 68 | 69 | mov dword [second_value], 0 70 | mov eax, [instruction2] 71 | mov eax, [eax + s_instruction.params + 4] 72 | mov dword [param1], eax 73 | 74 | mov eax, [instruction2] 75 | movzx edx, byte [eax + s_instruction.type] 76 | 77 | .1 invoke is_register, edx, 2 78 | cmp eax, 1 79 | jne .2 80 | 81 | invoke is_good_register, [param1] 82 | cmp eax, 1 83 | jne .END 84 | 85 | mov eax, [param1] 86 | shl eax, 2 87 | ; mov edx, 4 88 | ; mul edx 89 | 90 | mov edx, [champions2] 91 | mov edx, [edx + s_champions.reg] 92 | add edx, eax 93 | mov edx, [edx] 94 | mov dword [second_value], edx 95 | jmp .END 96 | 97 | .2 invoke is_direct, edx, 2 98 | cmp eax, 1 99 | jne .3 100 | 101 | mov eax, [param1] 102 | mov dword [second_value], eax 103 | jmp .END 104 | 105 | .3 invoke is_indirect, edx, 2 106 | cmp eax, 1 107 | jne .END 108 | 109 | mov eax, [param1] 110 | mov dword [second_value], eax 111 | 112 | .END mov eax, [second_value] 113 | popx edx 114 | 115 | endproc 116 | 117 | proc get_third_value, core3, champions3, instruction3 118 | 119 | _var_ third_value, param2 120 | 121 | pushx edx 122 | 123 | mov dword [third_value], 0 124 | mov eax, [instruction3] 125 | mov eax, [eax + s_instruction.params + 8] 126 | mov dword [param2], eax 127 | 128 | .TEST mov eax, [instruction3] 129 | movzx edx, byte [eax + s_instruction.type] 130 | 131 | .1 invoke is_register, edx, 3 132 | cmp eax, 1 133 | jne .2 134 | 135 | invoke is_good_register, [param2] 136 | cmp eax, 1 137 | jne .END 138 | 139 | mov eax, [param2] 140 | shl eax, 2 141 | ; mov edx, 4 142 | ; mul edx 143 | 144 | mov edx, [champions3] 145 | mov edx, [edx + s_champions.reg] 146 | add edx, eax 147 | mov edx, [edx] 148 | mov dword [third_value], edx 149 | jmp .END 150 | 151 | .2 invoke is_direct, edx, 3 152 | cmp eax, 1 153 | jne .END 154 | 155 | mov eax, [param2] 156 | mov dword [third_value], eax 157 | 158 | .END mov eax, dword [third_value] 159 | popx edx 160 | 161 | endproc 162 | 163 | proc my_sti, core, champions, instruction 164 | 165 | _var_ value1, value2, value3, ptr, mem_size, idx_mod 166 | 167 | pushx edx 168 | 169 | mov dword [mem_size], MEM_SIZE 170 | mov dword [idx_mod], IDX_MOD 171 | 172 | invoke get_value_to_store, [core], [champions], [instruction] 173 | mov dword [value1], eax 174 | 175 | invoke get_second_value_sti, [core], [champions], [instruction] 176 | xor edx, edx 177 | .TEST cmp eax, 0 178 | jge .NO 179 | mov edx, -1 180 | .NO idiv dword [idx_mod] 181 | mov dword [value2], edx 182 | 183 | invoke get_third_value, [core], [champions], [instruction] 184 | xor edx, edx 185 | cmp eax, 0 186 | jge .NO2 187 | mov edx, -1 188 | .NO2 idiv dword [idx_mod] 189 | mov dword [value3], edx 190 | 191 | mov eax, [champions] 192 | mov eax, [eax + s_champions.pc] 193 | sub eax, 7 194 | add eax, dword [value2] 195 | add eax, dword [value3] 196 | 197 | xor edx, edx 198 | cmp eax, 0 199 | jge .NO3 200 | mov edx, -1 201 | .NO3 idiv dword [mem_size] 202 | 203 | ; pushx eax, ebx, ecx, edx 204 | ; invoke printf, str2, [value1], [value2], [value3] 205 | ; popx eax, ebx, ecx, edx 206 | ; pushx eax, ebx, ecx, edx 207 | ; invoke disp_inst, [instruction] 208 | ; popx eax, ebx, ecx, edx 209 | ; pushx eax, ebx, ecx, edx 210 | ; invoke printf, str1, edx 211 | ; popx eax, ebx, ecx, edx 212 | 213 | .LOOP cmp edx, 0 214 | jge .ENDL 215 | 216 | add edx, MEM_SIZE 217 | jmp .LOOP 218 | 219 | .ENDL invoke write_arena_four, [core], [champions], [value1], edx 220 | 221 | mov eax, [champions] 222 | cmp dword [value1], 0 223 | je .ONE 224 | 225 | mov dword [eax + s_champions.carry], 0 226 | jmp .END 227 | 228 | .ONE mov dword [eax + s_champions.carry], 1 229 | 230 | .END xor eax, eax 231 | popx edx 232 | 233 | endproc --------------------------------------------------------------------------------