Hi all,
I'm pretty new to assembly programming so plz bare with me on this one ;). I'm trying to print a character to a given place in a console window.
The code above results in the character 'A' being printed at the top left corner of the console screen, but i want it to be printed at column 10, row 8.
I'm on vista, if that matters.
Any help will be greatly appreciated!
/C++arl
I'm pretty new to assembly programming so plz bare with me on this one ;). I'm trying to print a character to a given place in a console window.
;Försök till ett spel, testar bara lite.
;C++arl Dec 2008
;inkludera makro för win32 API
include 'C:\Users\Carl\Programmering\Assembly\Fasm\INCLUDE\win32ax.inc'
.data
;first you need to name your struct
struct COORD
x db ?
y db ?
ends
;then you need to reserve space for it
coord1 COORD ;you don't need the '?' because it's already inside the struct
rb sizeof.COORD * (2 - 1) ;unfortunately this is currently required because the 'dup' instruction can't handle structs yet
;but hopefully it will be soon.
con_handle DD ?
tan_handle DD ?
char DB ?
read DB ?
written DB ?
bokstav DB 'A', 00h
.code
start:
;skapa console fönster
invoke AllocConsole
;skaffa tangentbords handle
invoke GetStdHandle, STD_INPUT_HANDLE
;flytta return värdet som hamnar i EAX till handle
mov , eax ;handle blir en pekare till tangentb.
;skaffa skärm handle
invoke GetStdHandle, STD_OUTPUT_HANDLE
mov , eax
;skriv till console
mov , 10 ;IS THIS WHERE I GO WRONG?
mov , 8 ;IS THIS WHERE I GO WRONG?
invoke SetConsoleCursorPosition, , coord1
invoke WriteConsole, , bokstav, 1, written, 0
;läs från tangentbord
invoke ReadConsole, , char, 1, read, 0
;avsluta
invoke ExitProcess, 0
.end start
The code above results in the character 'A' being printed at the top left corner of the console screen, but i want it to be printed at column 10, row 8.
I'm on vista, if that matters.
Any help will be greatly appreciated!
/C++arl
I'm not familiar with FASM syntax, but if I understand it correctly and if I'm not mistaken, SetConsoleCursorPosition requires a structure as its second param, not a pointer to it.
Depending on the structure alignment, one of the following may be correct:
invoke SetConsoleCursorPosition, , ,
or
invoke SetConsoleCursorPosition, , ( or ( shl 16))
Depending on the structure alignment, one of the following may be correct:
invoke SetConsoleCursorPosition, , ,
or
invoke SetConsoleCursorPosition, , ( or ( shl 16))
Firts of, thx for taking time ;)
Yeah, i think youre right regarding that the SetCursorPos... wants a structure, not a pointer, so I try this:
which compiles fine but when running the program the console window just opens and closes immiditely, and it should wait for enter before closing. So just out of curiosity I tried this:
Note that I've changed the word directive to dword. That too compiles fine but when run it prints 'A' at the top left corner (just as if coord1.x and coord1.y were both set to 0). So i guess I have two questions now :P
1. Why does the program shut down immiditely when using word, but not with dword? and,
2. How do I get SetCursorConsolePosition to work as intended?
//C++arl :)
Yeah, i think youre right regarding that the SetCursorPos... wants a structure, not a pointer, so I try this:
invoke SetConsoleCursorPosition, , word
which compiles fine but when running the program the console window just opens and closes immiditely, and it should wait for enter before closing. So just out of curiosity I tried this:
invoke SetConsoleCursorPosition, , dword
Note that I've changed the word directive to dword. That too compiles fine but when run it prints 'A' at the top left corner (just as if coord1.x and coord1.y were both set to 0). So i guess I have two questions now :P
1. Why does the program shut down immiditely when using word, but not with dword? and,
2. How do I get SetCursorConsolePosition to work as intended?
//C++arl :)
1. Why does the program shut down immiditely when using word, but not with dword?
I don't know about how to use SetCursorConsolePosition (atm) but I assume it crashes and closes itself due to a misaligned stack. Unlike Masm, Fasm probably really only pushes a word onto the stack and on the ret instruction the stack is misaligned and <boom>. The stack needs to be aligned on a dword boundary.
2. How do I get SetCursorConsolePosition to work as intended?
maybe like this:
mov eax, 10
shl eax, 16
mov ax, 8
invoke SetConsoleCursorPosition, , eax
Oh btw:
struct COORD
x db ?
y db ?
ends
Shouldn't that be:
struct COORD
x dw ?
y dw ?
ends
Ad 1:
Definitely, it crashes because of misaligned stack.
Ad 2:
Have you tried pushing it this way:
invoke SetConsoleCursorPosition, , ( or ( shl 16))
or just
invoke SetConsoleCursorPosition, , (5 or (4 shl 16))
or
invoke SetConsoleCursorPosition, , 65538
?
Definitely, it crashes because of misaligned stack.
Ad 2:
Have you tried pushing it this way:
invoke SetConsoleCursorPosition, , ( or ( shl 16))
or just
invoke SetConsoleCursorPosition, , (5 or (4 shl 16))
or
invoke SetConsoleCursorPosition, , 65538
?
Got it now! ;) And I understand why it works, and did'nt work, aswell now thanks to you guys, really great help.
Yes, is should be
I thought SHORT was only a byte, no wonder things got fubar with me pushing whack to the stack. I especially like this way
thats not the way its done in Java :P.
A few follow-up questions though.
1. How would MASM do it then? and,
2. Do you reckon I should switch to MASM, what are the pros and cons for each the 'dialects'?
thx,
~ C++arl
Yes, is should be
struct COORD
x dw ?
y dw ?
ends
I thought SHORT was only a byte, no wonder things got fubar with me pushing whack to the stack. I especially like this way
mov eax, 10
shl eax, 16
mov ax, 8
invoke SetConsoleCursorPosition, , eax
of doing it, its clean and i know what I'm doing, plus I dont need to waste space on a variable. Thing is I'm so used with higher level languages that I dont see all the possibilities with assembler. I mean, invoke SetConsoleCursorPosition, , (5 or (4 shl 16))
thats not the way its done in Java :P.
A few follow-up questions though.
Unlike Masm, Fasm probably really only pushes a word onto the stack
1. How would MASM do it then? and,
2. Do you reckon I should switch to MASM, what are the pros and cons for each the 'dialects'?
thx,
~ C++arl
When I was writing that I thought it would only confuse and considered taking it out but then I must have forgotten along the way.
In invoke Masm would pad the pushed word with another word effectively pushing a dword as a parameter.
I don't think you should switch to masm. Any other assembler would not have changed the mistakes you made.
In invoke Masm would pad the pushed word with another word effectively pushing a dword as a parameter.
I don't think you should switch to masm. Any other assembler would not have changed the mistakes you made.