Skip to content

Commit b9fcc82

Browse files
committed
Update BfToFloppy and loader code
1 parent 391e56b commit b9fcc82

File tree

4 files changed

+19
-25
lines changed

4 files changed

+19
-25
lines changed

BfToFloppy/src/FloppyImage.cpp

+9-11
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ namespace btf
1414
{
1515
imageFileContent.assign(FLOPPY_SIZE, 0);
1616

17-
writeLoader(loaderFilename);
18-
int codeSize = assignCode(codeFilename);
17+
int loaderSize = writeLoader(loaderFilename);
18+
int codeSize = assignCode(codeFilename, loaderSize);
1919
replaceFlagWithMachineCode(codeSize);
2020
}
2121

@@ -29,20 +29,20 @@ namespace btf
2929
std::copy(imageFileContent.begin(), imageFileContent.end(), std::ostreambuf_iterator<char>(imageFile));
3030
}
3131

32-
void FloppyImage::writeLoader(const std::string& loaderFilename)
32+
int FloppyImage::writeLoader(const std::string& loaderFilename)
3333
{
3434
std::ifstream loaderFile(loaderFilename, std::ios::binary);
3535

3636
if (!loaderFile.is_open())
3737
throw Error("Couldn\'t find MBR file: " + loaderFilename);
3838

3939
std::vector<char> loader((std::istreambuf_iterator<char>(loaderFile)), std::istreambuf_iterator<char>());
40-
41-
imageFileContent.erase(imageFileContent.begin(), imageFileContent.begin() + loader.size());
42-
imageFileContent.insert(imageFileContent.begin(), loader.begin(), loader.end());
40+
std::copy(loader.begin(), loader.end(), imageFileContent.begin());
41+
42+
return loader.size();
4343
}
4444

45-
int FloppyImage::assignCode(const std::string& codeFilename)
45+
int FloppyImage::assignCode(const std::string& codeFilename, int offset)
4646
{
4747
std::ifstream codeFile(codeFilename);
4848

@@ -58,8 +58,7 @@ namespace btf
5858
if (code.size() > 65536)
5959
throw Error("Accteptable brainfuck source code size is 64KiB");
6060

61-
imageFileContent.erase(imageFileContent.begin() + 512, imageFileContent.begin() + 512 + code.size());
62-
imageFileContent.insert(imageFileContent.begin() + 512, code.begin(), code.end());
61+
std::copy(code.begin(), code.end(), imageFileContent.begin() + offset);
6362

6463
return code.size();
6564
}
@@ -82,7 +81,6 @@ namespace btf
8281

8382
std::vector<uint8_t> opcode = {0xB0, static_cast<uint8_t>(sectorsAmount & 0xFF), 0x90, 0x90};
8483

85-
imageFileContent.erase(iterator, iterator + 4);
86-
imageFileContent.insert(iterator, opcode.begin(), opcode.end());
84+
std::copy(opcode.begin(), opcode.end(), iterator);
8785
}
8886
}

BfToFloppy/src/FloppyImage.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ namespace btf
1414
void writeToFile(const std::string& floppyImageFilename);
1515

1616
private:
17-
void writeLoader(const std::string& loaderFilename);
18-
int assignCode(const std::string& codeFilename);
17+
int writeLoader(const std::string& loaderFilename);
18+
int assignCode(const std::string& codeFilename, int offset);
1919
void replaceFlagWithMachineCode(int brainfuckCodeSize);
2020

2121
std::vector<uint8_t> imageFileContent;

loader/src/bf.asm

+7-11
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,6 @@
44
DATA_ARRAY_SIZE equ 30000
55

66
bf_interpreter:
7-
; Set up data for memset
8-
mov ax, 0x2000
9-
mov es, ax
10-
xor di, di
11-
12-
xor ax, ax
13-
mov cx, DATA_ARRAY_SIZE
14-
15-
; Something like memset(0x20000, 0, DATA_ARRAY_SIZE)
16-
rep stosb
17-
187
; Set up segment for brainfuck code
198
mov ax, 0x07E0
209
mov ds, ax
@@ -28,6 +17,13 @@ bf_interpreter:
2817
2918
; Set up brainfuck array pointer
3019
xor di, di
20+
21+
; Set up data for memset
22+
xor ax, ax
23+
mov cx, DATA_ARRAY_SIZE
24+
25+
; Something like memset(0x20000, DATA_ARRAY_SIZE, 0)
26+
rep stosb
3127

3228
; Set up brainfuck loops counter
3329
xor cx, cx

loader/src/screen.asm

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ print_char:
1616
cmp al, 0xA
1717
jne .end
1818
mov al, 0xD
19-
call print_char
19+
jmp print_char
2020

2121
.end:
2222
ret

0 commit comments

Comments
 (0)