Author Topic: Introduction + Silly Question :)  (Read 792 times)

Rob260z

  • Regular Member
  • *
  • Posts: 9
Introduction + Silly Question :)
« on: July 26, 2017, 06:18:35 PM »
Hi All :)

Well , in order to prove I'm not a bot, I thought I should introduce myself. My name is Robert and I have returned to Assembly programming after nearly 15 years. Looking for a bit of an intellectual challenge , plus I absolutely love the pure nature of Assembly.

Last time I did any programming I was using Boralnd Turbo Assembler. After a bit of a look around I have decided to settle on MASM.


Well , I'm going to start with a simple question. What am I doing wrong in the example below. I know I'm going to kick myself when I get the answer , but it's got me stumped. This approach works fine in NASM , but I obviously don't understand exactly how MASM pointers/referencing/stack works. Despite finding examples that appear to do the same thing.

A simple test function. I'm writing the modified string to a text file using WIN32 WriteFile. The issue appears to be the use of the INC instruction. With or without the INC instruction successfully writes to the file , but with the INC instruction the program crashes immediately after writing the file ( succsessfully ). Am I somehow corrupting the stack ?

        Str_DataLogText db "..........",13,10

     mov EBX , offset Str_DataLogText
   inc EBX          ;??????????????????????????????????????????????
   mov EDX , 49d
   mov byte ptr [ EBX ] , DL

        push 0
        push offset Var_DataLogBytesWritten
        push 12d
       push offset Str_DataLogText
       push Hdl_DataLogFile
       call WriteFile

     

      cheers ,
      Rob

jj2007

  • Member
  • *****
  • Posts: 7730
  • Assembler is fun ;-)
    • MasmBasic
Re: Introduction + Silly Question :)
« Reply #1 on: July 26, 2017, 06:42:35 PM »
Hi Robert,
First things first: Welcome to the Forum :icon14:

Your code works fine. It took me ten minutes of my precious time, though, to set up the missing headers, insert the variables etc - be prepared that this is the last time that I help you unless you decide to post complete code with detailed comments in future.

Code: [Select]
include \masm32\include\masm32rt.inc ; obligatory in this forum, for practical reasons!

.data
Str_DataLogText db "..........",13,10
Var_DataLogBytesWritten dd ?
Hdl_DataLogFile dd ?
.code
start:
mov EBX , offset Str_DataLogText
   inc EBX          ;??????????????????????????????????????????????
   mov EDX , 49d
   mov byte ptr [ EBX ] , DL
mov Hdl_DataLogFile, fopen("tmp.txt")
        push 0
        push offset Var_DataLogBytesWritten
        push 12d
       push offset Str_DataLogText
       push Hdl_DataLogFile
       call WriteFile

       fclose Hdl_DataLogFile
  exit

end start

You should also get used to the invoke macro. The old habit of pushing parameters around is error-prone, especially if a function needs a dozen parameters, like e.g. CreateWindowEx.

So your code crashes. Weird, mine doesn't  8)

It could remain forever a mystery, but there is a solution: In case of little programs that crash, it is a good habit to zip the exe and attach it here, so that we can launch Olly and analyse the problem. For example, without a crystal ball we won't know which parameters you used for the unknown API call that you used to open the file.

Rob260z

  • Regular Member
  • *
  • Posts: 9
Re: Introduction + Silly Question :)
« Reply #2 on: July 26, 2017, 06:49:32 PM »
Hi jj2007 :)

Thank you for your time , very much appreciated.

Sorry , but I appear to have wasted it ( your time ). I was just typing a new comment when you replied.

I'm very embarrassed , but I forgot to put a RET instruction at the end of the procedure **** hangs head in shame ****

I would have posted the total code but its already around 300 lines. Too much for a forum post ?

Regarding the use of invoke.....I still feel dirty using it , lol. I'm a bit of a purist :)

Rob260z

  • Regular Member
  • *
  • Posts: 9
Re: Introduction + Silly Question :)
« Reply #3 on: July 26, 2017, 07:00:28 PM »
In future I'll create self-contained , executable code before posting up any example code. This way I would have actually realised what the problem was.

jj2007

  • Member
  • *****
  • Posts: 7730
  • Assembler is fun ;-)
    • MasmBasic
Re: Introduction + Silly Question :)
« Reply #4 on: July 26, 2017, 07:38:07 PM »
I would have posted the total code but its already around 300 lines. Too much for a forum post ?

No problem, you have 32k if I remember well. If it's more, zip the source and attach it. The limit is 524,288 bytes then.

Regarding the use of invoke.....I still feel dirty using it , lol. I'm a bit of a purist :)

As a purist, if you code in C, do you still use the original Kernighan & Ritchie compiler?

sinsi

  • Member
  • *****
  • Posts: 1003
Re: Introduction + Silly Question :)
« Reply #5 on: July 26, 2017, 08:20:22 PM »
Regarding the use of invoke.....I still feel dirty using it , lol. I'm a bit of a purist :)
As a purist, if you code in C, do you still use the original Kernighan & Ritchie compiler?
Don't listen to him Rob, some of us don't need our hands held  :biggrin:
I can walk on water but stagger on beer.

jj2007

  • Member
  • *****
  • Posts: 7730
  • Assembler is fun ;-)
    • MasmBasic
Re: Introduction + Silly Question :)
« Reply #6 on: July 26, 2017, 09:22:52 PM »
Don't listen to him Rob, some of us don't need our hands held  :biggrin:

That's true! As a rule, if your IQ is well above 140 and your code never exceeds, say, a thousand lines, you can comfortably push your paras without the invoke macro. I've done the pushpushpop thing myself when I started assembler programming.

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 4922
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Introduction + Silly Question :)
« Reply #7 on: July 26, 2017, 09:23:45 PM »
Depends on how much typing you want to do, push/call notation works fine but its a bit cluttery with large amounts of API and similar HLL code. This is where "invoke" notation does the job better and makes your code more readable. The real action in 32 bit and later assembler is in writing algorithms and here pure mnemonic code is the way to go.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin:

Rob260z

  • Regular Member
  • *
  • Posts: 9
Re: Introduction + Silly Question :)
« Reply #8 on: July 27, 2017, 12:34:45 AM »
With all due respect....and I honestly mean that. People usually use that term just before their about to patronize someone. But I've been lurking on here for a long time , and I understand that the people I'm talking to know much more about this subject than i'll ever know. Which is why I'm here !

But...honestly...what about pushing parameters to a procedure/function requires a high IQ ? . I find it a much easier syntax to read.

Maybe I dislike the C syntax.

Not here to pick a fight , just looking to learn from people far more knowledgeable than myself. A pretty easy task :)

felipe

  • Member
  • ***
  • Posts: 358
  • assemble the unassembled.
Re: Introduction + Silly Question :)
« Reply #9 on: July 27, 2017, 12:47:08 AM »
Last time I did any programming I was using Boralnd Turbo Assembler.
So, you were a guy writing computer viruses!   :icon_mrgreen:
 :lol: Just a joke.

Welcome to the forum.  :t
Felipe.

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 4922
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Introduction + Silly Question :)
« Reply #10 on: July 27, 2017, 01:37:28 AM »
Rob,

You will find the comment was offered in humour, not as an insult. There are those among us who can still code in HEX so don't feel you are left out of it coding external function calls with push/call notation. MASM can write very low level code if you know how to write it but it IS a MACRO assembler which is fully controlled by the assembler programmer so its more a case of packaging code you write yourself into a macro if it suits your purpose.

Many of the old fellas (me included) used to write code that looked like a Codeview debug screen but with the advent of Windows where you have no choice other to call system functions (Windows API calls), there are so many system defined equates that you would go NUTZ doing them as hex numbers as you would have to look them up in C notation in those truly horrible C header files.

hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin:

aw27

  • Member
  • ****
  • Posts: 852
  • Let's Make ASM Great Again!
Re: Introduction + Silly Question :)
« Reply #11 on: July 27, 2017, 01:41:54 AM »
but I forgot to put a RET instruction
A lil problem, is like leaving home without putting the house keys in the pocket.  ;)

jj2007

  • Member
  • *****
  • Posts: 7730
  • Assembler is fun ;-)
    • MasmBasic
Re: Introduction + Silly Question :)
« Reply #12 on: July 27, 2017, 04:38:29 AM »
But...honestly...what about pushing parameters to a procedure/function requires a high IQ ? . I find it a much easier syntax to read

The ironic IQ remark was not addressed to you, really. As to the syntax, as Hutch wrote already, if your programs get longer, you will be drowned by really complicated API calls, but invoke checks if the parameters have the correct type, and if you haven't by accident added one push eax too much. Pushing by hand is really error-prone, you end up chasing bugs all the time. And yes, it's an old fight between those who accept that Masm is a macro assembler, and those who don't. My impression is that the purists are not very productive, but that's only an impression 8)

coder

  • Member
  • **
  • Posts: 103
Re: Introduction + Silly Question :)
« Reply #13 on: July 27, 2017, 05:16:24 AM »
invoke and stuff are high-level macros. They may not be well-suited for beginners like Rob who wants to learn instructions and make parallel references to the Manuals, the debugger output and examples floating around the internet. invoke hides too many things crucial to understanding how instructions really work - the true fun of x86 assembly programming. But once he truly appreciates what's going on behind the scene, he can use invoke and other high-level stuff as he pleases. Rob is not a purist. He misused the terminology. He's an x86 Instruction Set learner.

Just my 2 cents. Welcome to forum, Rob. There are many bad guys in here, led by Hutch. You and I are the good guys  :dazzled:
 









 

aw27

  • Member
  • ****
  • Posts: 852
  • Let's Make ASM Great Again!
Re: Introduction + Silly Question :)
« Reply #14 on: July 27, 2017, 05:19:36 AM »
Actually INVOKE is not a macro in Masm, it has been a directive, for the last 25 years or so. Then Microsoft transferred all MASM developers to the C# department leaving there just an old guy to change the version number and rebuild from time to time (probably with the assistance from some people from this forum ).
When it was time to compile MASM for 64-bit they could not make INVOKE and the Comparition directives to work, so they left these behind saying rough and hard to use is indeed a feature - some people in this forum agree with that 100%.