News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Comaring the syntax of C++ and MASM

Started by RedSkeleton007, June 25, 2015, 03:28:01 PM

Previous topic - Next topic

RedSkeleton007

To truly understand ASM (as well as any programming language), you need to know how a program works. When you've reached the intermediate level of one programming language, the most important challenge is using the same logic to get past the syntax differences. Are my code comments correct:


TITLE Program Template (Template.asm) ;C++ = part of program description in /**/ block
; Program Description:
; Author:
; Date Last Modified:
; Modification's Description:

INCLUDE Irvine32.inc ;C++ = #include<>

.data
;(insert variables here) C++ = global variable declarations

.code
main PROC ;C++ = int main(){

;(insert executable instructions here)

exit ;C++ = return 0
main ENDP ;C++ = closing curly brace of main()

;(insert additional procedures here) C++ = definitions of functions called in main

END main ;last line of the program to be assembled

Vortex

Hi RedSkeleton007,

C++ and asm are both different worlds. Assembly is the native language of the CPU. The elements of the object oriented programming like C++ are abstractions converted to asm by the compiler. Procedural programming advices you to stay away from the GOTO statement but you will see that the jump ( jmp ) command is used extensively by the asm language.

;(insert variables here) C++ = global variable declarations

Variables can be defined in the .data? section too. ( uninitialized data section )  You can also use the stack to store your variables.

;(insert additional procedures here) C++ = definitions of functions called in main

Additional procedures can be inserted before and after the main procedure. The definitions are placed on the top of the code ( often found in another include file. )

Zen

Hi, REDSKELETON007, 
I, too, started out programming in C++,...and, eventually, also learned assembly language.
C++ is great for programming complex applications, and assembly is great for just about everything. Understanding them both gives you enormous advantages over the average programmer. And, once you see, by example, how assembly language actually works, all those C++ mysteries that you've always wondered about will become clear. :biggrin:
If you read through the example code (located in the masm/example directory) that comes with the MASM32 package, you will learn alot about the structure of typical MASM applications.
...And, this site is exceptional: Iczelion's Win32 Assembly Homepage. His tutorials are legendary with assembly language novices: Win32 Assembly Tutorials
...Also, this is helpful: MASM Programmer's Guide

Tedd

Quote from: RedSkeleton007 on June 25, 2015, 03:28:01 PM
Are my code comments correct?
Short answer: yes, or close enough.

A little more detail...

.data
;(insert variables here) C++ = global variable declarations

More specifically, initialised global variables, e.g. "int a = 27;"
Uninitialised variables (actually still initialised to zero on program loading) can go in the ".data?" section in the same way, e.g. "int b;" (this section is useful for arrays and structures as it doesn't take up additional space in the exe.)
Also, there is a ".const" section, for read-only initialised variables, though few people around here know how to use it, e.g. "const int c = 42;" (good for initialised variables that will not change, and message strings; does not add an additional section to the exe, as there is already a const section used for DLL importing.)


.code
main PROC ;C++ = int main(){

;(insert executable instructions here)

exit ;C++ = return 0
main ENDP ;C++ = closing curly brace of main()

Yes, but not exactly. C/C++ main is only called after the runtime library has done its thing, while this is the entry-point - there is no runtime (if there were a runtime, it would be placed here and would call main when ready.) There's no requirement for this to be a function/procedure, so you will see most programs that jump straight into instructions - either way is fine.
Since there is no runtime library, there is also no 'exit' support and cleanup - it is the end of all execution for your program. To return to the OS (with an exit code), use ExitProcess(0).


;(insert additional procedures here) C++ = definitions of functions called in main

Yes, though you can also place them before main (as in C/C++), which avoids the need for PROTO for the forward declarations.


END main ;last line of the program to be assembled

Yep. The label after END specifies the entry point (where execution begins), in this case it's the label of your main function; 'start' is also commonly used.
Potato2

rrr314159

you need an end statement, instead of

END main ;last line of the program to be assembled

just

END

I am NaN ;)