Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie help with first program (program crashes)

Status
Not open for further replies.

CppMaster

Programmer
Apr 19, 2006
12
0
0
CA
Hello,
I have tried, using 32-bit assembly language code I found online, to write my own program. It assembled and link, but when I run it, I get a message saying the program had "encountered a problem and needs to close." It is a message that is common on Windows, asking to send a report to Microsoft. I tried using a Turbo Debugger, but I got the same message, but for the debugger. The code is the following:
Code:
	.586
	.MODEL	FLAT, STDCALL
	.DATA
DATA1	DB	52H
DATA2	DB	29H
SUM	DB	?
	.CODE
START:
	MOV	AL, DATA1
	MOV	BL, DATA2
	ADD	AL, BL
	MOV	SUM, AL
END	START
As you can see, the point of the program is simply to add some values (the result would be checked through a debugger).
I'm using a pentium III processor with Windows XP. What would be the problem(s) with my code? Thank you very much
 
Your code has no controled end and therefore your program will continue into some old code and crashes.

So you must end it with some code, see your book.

Tessa
 
Try try adding on the following:
Code:
  MOV ah, 4CH  ;EXIT PROCESS FUNCTION
  INT 21H      ;EXECUTE DOS SERVICES INT
Asembler programs are "stupid"; they assume nothing. If you don't tell it exactly what to do, they won't do it, so you must tell the program to return to the operating system or it will continue to execute past the end point, as Tessa points out

The simplest solution is the best!
 
I corrected it, but I still get the same problem.
Code:
	.586
	.MODEL	FLAT, STDCALL
	.DATA
DATA1	DB	52H
DATA2	DB	29H
SUM	DB	?
	.CODE
START:
	MOV	AL, DATA1
	MOV	BL, DATA2
	ADD	AL, BL
	MOV	SUM, AL
        MOV   AH, 4CH
        INT   21H
END	START
 
You wrote that you're writing 32bit code, and you're using model 'flat', but int 21h is the 16-bit DOS interrupt, with ah=04Ch being the DOS call to end a program. These are two different worlds.
 
won't a simple RET instruction suffice?

with DOS programs the stack pointer points to an INT 20h instruction upon program startup.
i'm not saying that INT 20h is the answer for flat model programs, to be honest i don't know,
but maybe the stack pointer will point at the answer.
 
The RET instruction seems to be working: I don't get a message saying the program caused an error. The problem this time is that I get this message when attempting to run it in turbo debugger and I when I tried running it in debugger, I simply was not able to run it: if I use the G(o) command, it tells me that the program cannot be run in MS-DOS mod, and I cannot find my code using U(nassemble) command. What should I do? Any advice? Thank you for your past and future answers.
 
this might be because you assembled your program as a windows executable,
and you use a debugger that can only handle DOS programs (or is configured as such).

what kind of assembler and linker are you using?
 
You really do need to decide whether you're writing a 16-bit DOS program or a 32-bit windows program. They are fundamentally different, and won't run in each other's environment. It will really help you to find out which environment you're in, because soon you will want to look at the result of your calculation, which is where an operating system really is useful. You need to know whether the operating system is going to be windows, or DOS (or more likely, windows pretending to be dos!).

From your name, I'd guess you are already a happy C++ programer. If so, have you considered embedding little bits of assembler into C or C++ programs? It might be an easier way to see how it works, and also more practically useful. Generally, assembler is most valuable in speeding up a very, very small bit of completely critical code, or for the occasional very hardware-near thing. Very few people will benefit from writing whole applications in assembler.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top