News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

String Pointer Problem

Started by suma, January 06, 2005, 07:57:58 AM

Previous topic - Next topic

suma

Hey there...

I am new to the asm language, and i have run into a problem involving string pointers. Any help would be much appreciated!

The code i am writing is just simple code which will show a message box. However i dont want to use masm's invoke, but rather i am doing it the old skool way.

push 0
push 0
push 0
push 0
call MessageBox

The problem appears when i try to replace the middle two parameters with actual strings. I have tried:

>> putting the string as a constant i.e.
                push "hkia"

>> putting the name of the string variable, in the hope it will translate to the base address of the string array i.e.
                byte hk "hkia"
                push hk

>> getting a pointer to the string variable and pushing this... i have tried with both near ptr byte and far ptr byte. Both will not compile - i think i may be doing something wrong.
                byte hk "hkia"
                npb typedef near ptr byte
                fpb typedef far ptr byte

                np npb hk
                fp fpb hk

                push np
                push fp


Nothing seems to work... i suspect it may be because the stack needs word values, not byte values, but i thought the compiler took care of that....    :(

Thanks in advance,
Suma

Petroizki

hello,

You don't need to push the string itself to the stack, only the address of the string.


.data
STRING_A db "Hello World!",0
.code
push MB_OK
push OFFSET STRING_A
push OFFSET STRING_A
push 0
call MessageBox

suma

ok ok that's cool...  :clap:

Just as a matter of interest, is OFFSET the main used directive for MASM, or are there other keywords which do pretty much the same thing?

Also, i was under impression that the name of the variable (i.e. STRING_A) equates to the address of the first element in the array. And using this address i should be able to reference the string.

hutch--

suma,

An OFFSET in MASM notation almost exclusively refers to data in the .DATA or .DATA? sections. For your MessageBox code you would write two entries in the .DATA section like this,


    mbtitle db "MessageBox Title",0
    mbmessage db "Written in 32 bit MASM",0


In you code section where you are pushing the paraneters for the MessageBox call, you then use OFFSET as per the examples above. The operator "OFFSET" literally means a distance from a reference and in the context of the DATA section of an EXE file, it is how far in bytes from the start address.

When you use a LOCAL variable created on the stack in a procedure, the address is NOT an OFFSET but created at runtime so you use the instruction LEA to load the effective address into a register.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Vortex

Hi suma,

Using invoke makes easier the life.

suma

>> hutch
ok thanks man that clears a lot up  :green

>> Vortex
yeh i knw i knw... i don't intend to actually write a lot of code in asm. i just want to know how it all works at the lowest level. so using invoke defeats my purpose.

roticv

Quoteyeh i knw i knw... i don't intend to actually write a lot of code in asm. i just want to know how it all works at the lowest level. so using invoke defeats my purpose.
Maybe it is better to try to code your own os  :toothy

thomasantony

Hi,
   Yeah I support roticv :bg ;) :P :clap: :bg . Developing an OS is a big learning experience for me.I gives me the absolute power over the computer. :bdg

Thomas Antony :U
There are 10 types of people in the world. Those who understand binary and those who don't.


Programmer's Directory. Submit for free