I'm have trouble printing items in a structure, here's the problem I was given this problem (just for background): (also, I'm using VC++ 6.0)

"Given the following C/C++ declaration:

struct Employee
{
char name[20];
char SSN[10];
unsigned short age;
char sex;
long int salary;
};

Write a function that will print a structure of this type out to the
screen, passing the address of the structure in through the ecx register.
(So when the subroutine starts, it can assume the address is already in
ecx.)"

In class my teacher showed us how to set items equal to each other. For example, Ptr->age = 20 would equal
mov DWORD PTR , 20
He didn't show us how to print items though. So I'm having a lot of trouble figuring that out. Here's what I have in my main.cpp file:

extern "C"

{
struct Employee
{
char name[20]; // 20 bytes
char SSN[10]; // 10 bytes
unsigned short age; // 2 bytes
char sex; // 2 bytes
long int salary; // 4 bytes
};
void __fastcall PrintEmployee(Employee *source);
}

#include <iostream>
using namespace std;

void main () {
Employee *pat,x;
pat = &x;
pat->age = 20;
pat->sex = 'm';
pat->salary = 10;
PrintEmployee(pat);
}

and here's what I have in my asm file: (which doesn't work)

.CODE
PUBLIC SYSCALL @PrintEmployee@4
@PrintEmployee@4:
mov eax, ecx
mov cx, WORD PTR ; age
call print_dec

ret
END

print_dec is a function we can use that prints numbers (it takes item in in the ecx register). Any help would be great.

later
Posted on 2002-03-23 18:28:24 by PAT or JK
i'm assuming you want output in a messagebox?

for the text:
anyway, using some routine (more optimised than the following one), you can copy the string and null padd it (doing this in my head):


;ebx=pointer to beginning of text to pad
;ecx=length of string, e.g. 20 for name in your program
;eax=pointer to a buffer to accept output
LoopP:
mov edx,
dec ecx
mov ,edx ;i guess you could use dl instead, but why mix 32- and 8-bit code?
jne LoopP
mov byte ptr ,0


then, you can call the MessageBox function:
push hWnd
push eax
push 0
push MB_OK
call MessageBox

for numbers
i'm lazy. so i'm going to use wsprintf. here we go:

invoke wsprintf,addr Buffer,addr Fmt,ecx ;assuming ecx already contains age or something
invoke MessageBox,hWnd,addr Buffer,0,MB_OK

also, make sure that Fmt contains a null-terminated string containing '%i' or '%d' (/wo the apostrophies)
Posted on 2002-03-23 19:03:26 by jademtech
print_dec is a function we can use that prints numbers (it takes item in in the ecx register).



There was probably some garbage in the high word part of ecx that kept your print rioutine from correctly printing.

OK, so you need the number into ecx, not just cx.

Your code correctly puts the number in cx, so all you need do is extend the word in cx to a dword in ecx.

Two ways to do that, either clear ecx before you load it, which assumes you will be adding a positive number, true here for an age, but not always. Or try this:


movsx ecx, WORD PTR ; age


'movsx' is 'move and extend sign,' so it will correctly convert a word to a dword and keep the same sign.
Posted on 2002-03-24 01:27:20 by Ernie