[C#/asm] Trying to Translate an Algorithm
Posted by urmuther112@reddit | learnprogramming | View on Reddit | 5 comments
I am trying to translate this binary insertion sort written with C# to assembly (MASM if that matters). Here is what I have so far. To be quite honest this spaghetti'd together using my basic knowledge and all the hard to read solutions I could find on Google. My issue lies in not knowing how to print the sorted array to the console. When I went to search up how others have done it, the solutions would be very different from each other making it hard to implement in my own code. I then tried going to GPT with this issue but it kept telling to use syscalls and VGA memory that I know nothing about. Is there not just a way to convert the array into ASCII and push/pop them to the console? I just wanna see if the sort actually works.
randomjapaneselearn@reddit
to use syscalls and VGA memory that I know nothing about
it depends if you are in dos, linux or windows and also if it's 32 bit or 64bit.
if you are on windows for example you can use microsoft provided API to print https://learn.microsoft.com/en-us/windows/console/writeconsole (see the other user answer), you still need to convert numbers to ascii.
don't forget that you can check everything even without printing by using a debugger like https://x64dbg.com/, you execute your code step by step and you can see if it works or where are mistakes if any.
if you are on linux you can use syscalls (see here https://hackeradam.com/x86-64-linux-syscalls/ or other similar links)
urmuther112@reddit (OP)
I went the Windows API route but now I am getting an error for an unresolved external. I have already added kernel32 to the linker and rebuilt the project. I have also changed all my WriteConsoleA calls to use INVOKE instead to see if that helped but I am still getting the same error.
randomjapaneselearn@reddit
did you add both?
Careless_Quail_4830@reddit
You can get this done the way you asked it in several ways, such as directly calling windows functions (eg
WriteConsoleA
) or linking with the C runtime so you can useprintf
.What I normally do, as long as a project doesn't need to be purely in assembly, is write a C++ program that calls the assembly code. All the things that are annoying to do in assembly stay on the C++ side, and calling a function that is implemented in assembly from C++ is easy. This makes it easier to print results, generate test data, run tests, and so on.
It's getting confused anyway.
Lumpy_Ad7002@reddit
Printing is one of those things that's hard in assembler. Fortunately, yours is also a common question, and StackOverflow has the answers
https://stackoverflow.com/questions/74984639/how-to-call-windows-api-in-x64-masm-program