FlierMate
Joined: 21 Jan 2021
Posts: 219
|
You use "I" for input followed by variable name (case-sensitive)
You use "O" for output (optionally variable name)
Look, my C# 114-line converter use this as input file:
O Hello World
O Press key in your name
I a
O Hi,
O a
O A
O Variable name is case-sensitive
O a
O Can you please enter your age?
I A
O Your age is...
O A
Then, generate the following programmatically:
format PE console
entry start
include 'win32a.inc'
section '.data' readable writable
_stdin dd ?
_stdout dd ?
_dummy dd ?
section '.code' code readable writable executable
_S0000 db 'Hello World',13,10,0
_S0001 db 'Press key in your name',13,10,0
_S0002 rb 256
_S0003 db 'Hi,',13,10,0
_S0004 db 'a',13,10,0
_S0005 db 'A',13,10,0
_S0006 db 'Variable name is case-sensitive',13,10,0
_S0007 db 'a',13,10,0
_S0008 db 'Can you please enter your age?',13,10,0
_S0009 rb 256
_S0010 db 'Your age is...',13,10,0
_S0011 db 'A',13,10,0
_L0000 dd 14
_L0001 dd 25
_L0002 dd ?
_L0003 dd 6
_L0004 dd 4
_L0005 dd 4
_L0006 dd 34
_L0007 dd 4
_L0008 dd 33
_L0009 dd ?
_L0010 dd 17
_L0011 dd 4
start:
invoke GetStdHandle, -10
mov dword [_stdin], eax
invoke GetStdHandle, -11
mov dword [_stdout], eax
invoke WriteConsole, dword [_stdout], _S0000, [_L0000], _dummy, 0
invoke WriteConsole, dword [_stdout], _S0001, [_L0001], _dummy, 0
invoke ReadConsole, dword [_stdin], _S0002, 255, _L0002, 0
invoke WriteConsole, dword [_stdout], _S0003, [_L0003], _dummy, 0
invoke WriteConsole, dword [_stdout], _S0002, [_L0002], _dummy, 0
invoke WriteConsole, dword [_stdout], _S0005, [_L0005], _dummy, 0
invoke WriteConsole, dword [_stdout], _S0006, [_L0006], _dummy, 0
invoke WriteConsole, dword [_stdout], _S0002, [_L0002], _dummy, 0
invoke WriteConsole, dword [_stdout], _S0008, [_L0008], _dummy, 0
invoke ReadConsole, dword [_stdin], _S0009, 255, _L0009, 0
invoke WriteConsole, dword [_stdout], _S0010, [_L0010], _dummy, 0
invoke WriteConsole, dword [_stdout], _S0009, [_L0009], _dummy, 0
invoke ExitProcess, 0
section '.idata' import readable writable
library kernel32,'KERNEL32.DLL'
import kernel32,\
GetStdHandle, 'GetStdHandle', \
WriteConsole, 'WriteConsoleA', \
ReadConsole, 'ReadConsoleA', \
ExitProcess,'ExitProcess'
Result in command-prompt window:
C:\FASM>1
Hello World
Press key in your name
wong
Hi,
wong
A
Variable name is case-sensitive
wong
Can you please enter your age?
23
Your age is...
23
C:\FASM>
Cute?
My HLL (C#) source : (not tested thoroughly, just done simple test)
using System;
using System.IO;
namespace Pancake
{
public class Program
{
public static void Main(string[] args)
{
const string NUL = "_$0";
string[] Header = new string[]
{
"format PE console",
"entry start", "\r\n",
"include 'win32a.inc'", "\r\n",
"section '.data' readable writable",
" _stdin dd ?",
" _stdout dd ?",
" _dummy dd ?","\r\n",
"section '.code' code readable writable executable"
};
string[] Body = new string[]
{
"\r\n", "start:",
" invoke GetStdHandle, -10",
" mov dword [_stdin], eax",
" invoke GetStdHandle, -11",
" mov dword [_stdout], eax", "\r\n"
};
string[] Footer = new string[]
{
" invoke ExitProcess, 0","\r\n",
"section '.idata' import readable writable",
" library kernel32,'KERNEL32.DLL'", "\r\n",
" import kernel32,\\",
" GetStdHandle, 'GetStdHandle', \\",
" WriteConsole, 'WriteConsoleA', \\",
" ReadConsole, 'ReadConsoleA', \\",
" ExitProcess,'ExitProcess'"
};
if ((args.Length > 0) && (File.Exists(args[0])))
{
string[] InputFile = File.ReadAllLines(args[0]);
string[] StdIn = new string[InputFile.Length];
string[] StdOut = new string[InputFile.Length];
int LineCount = 0;
foreach (string InputLine in InputFile)
{
string Line = InputLine.Trim();
if (Line.ToUpper().StartsWith("I "))
{
StdIn[LineCount] = Line.Substring(2);
StdOut[LineCount] = NUL;
LineCount++;
}
else if (Line.ToUpper().StartsWith("O "))
{
StdOut[LineCount] = Line.Substring(2);
StdIn[LineCount] = NUL;
LineCount++;
}
}
string[] VarName = new string[LineCount];
string[] VarLen = new string[LineCount];
string[] Code = new string[LineCount];
if (LineCount > 0)
{
for (int a = 0; a < LineCount; a++)
{
if (StdIn[a] != NUL)
{
VarName[a] = " _S" + a.ToString("D4") + " rb 256";
VarLen[a] = " _L" + a.ToString("D4") + " dd ?";
Code[a] = " invoke ReadConsole, dword [_stdin], _S" + a.ToString("D4") + ", 255, _L" + a.ToString("D4") + ", 0";
}
if (StdOut[a] != NUL)
{
VarName[a] = " _S" + a.ToString("D4") + " db '" + StdOut[a] + "',13,10,0";
VarLen[a] = " _L" + a.ToString("D4") + " dd " + (StdOut[a].Length + 3).ToString();
for (int b = 0; b < LineCount; b++)
{
if ((StdOut[a] == StdIn[b]) && (b < a))
{
Code[a] = " invoke WriteConsole, dword [_stdout], _S" + b.ToString("D4") + ", [_L" + b.ToString("D4") + "], _dummy, 0";
break;
}
else
Code[a] = " invoke WriteConsole, dword [_stdout], _S" + a.ToString("D4") + ", [_L" + a.ToString("D4") + "], _dummy, 0";
}
}
}
}
string FileName = args[0] + ".ASM";
File.WriteAllLines(FileName, Header);
File.AppendAllLines(FileName, VarName);
File.AppendAllLines(FileName, VarLen);
File.AppendAllLines(FileName, Body);
File.AppendAllLines(FileName, Code);
File.AppendAllLines(FileName, Footer);
Console.WriteLine(FileName + " created successfully.");
}
Console.WriteLine("Press Enter to quit...");
Console.ReadLine();
}
}
}
|