Author Topic: Code can be compiled but it crashes on run  (Read 318 times)

cioltealin

  • Regular Member
  • *
  • Posts: 6
Code can be compiled but it crashes on run
« on: May 20, 2018, 08:45:58 PM »
I try to run this code

Code: [Select]
data segment
v1 dw 1,2,3,4,4,3,2,1
v2 dw 0,0,0,0,0,0,0,0
data ends
code segment
start:
lea si,v1
lea di,v2
mov cx,8
et:
mov [edi],si ; here I changed di with edi because di is 16bit
add di,2
inc si ;,2
inc di ;,2
loop et
mov ah,4ch
int 21h
code ends
end start

I tried to remove loop et but the result is the same
Can someone give me a clue please?

jj2007

  • Member
  • *****
  • Posts: 8429
  • Assembler is fun ;-)
    • MasmBasic
Re: Code can be compiled but it crashes on run
« Reply #1 on: May 20, 2018, 09:39:30 PM »
Your code is basically 16-bit code. However, by inserting .586p before "data segment", it does compile assemble in 32-bit mode, and the OS may try to run it. It then crashes because edi is not 402010h as in lea edi, [402010] but rather 2010h as in lea di, [402010].

Make up your mind if you are coding for good ol' DOS or for 32-bit Windows.

P.S.: Welcome to the Forum :icon14:

cioltealin

  • Regular Member
  • *
  • Posts: 6
Re: Code can be compiled but it crashes on run
« Reply #2 on: May 20, 2018, 09:58:46 PM »
Thanks!
 I tried
Code: [Select]
public start

data segment
v1 dw 1,2,3,4,4,3,2,1
v2 dw 0,0,0,0,0,0,0,0
data ends
code segment
start:
lea si,v1
lea edi,v2
mov cx,8
et:
mov [edi],si ;,di
add edi,2
inc si ;,2
inc edi ;,2
loop et
mov ah,4ch
int 21h
code ends
end start

and it still crashes :(

AW

  • Member
  • *****
  • Posts: 1292
  • Let's Make ASM Great Again!
Re: Code can be compiled but it crashes on run
« Reply #3 on: May 20, 2018, 10:20:34 PM »
Are you coding for 16-bit or for 32-bit?
It looks like you are trying to code for 16-bit.

Is so, try something like this:

Code: [Select]
public start

DOSSEG
        .model  small
        .stack  100h
        .186
 
data segment
v1 dw 1,2,3,4,4,3,2,1
v2 dw 0,0,0,0,0,0,0,0
data ends

code segment

start:
mov     ax, @data
      mov     ds, ax          ; set segment register
      mov     es, ax          ; set segment register
lea si,v1
lea di,v2
mov cx,8
et:
mov [di],si ;,di
add di,2
inc si ;,2
inc di ;,2
loop et
mov ah,4ch
int 21h
code ends
end start

jj2007

  • Member
  • *****
  • Posts: 8429
  • Assembler is fun ;-)
    • MasmBasic
Re: Code can be compiled but it crashes on run
« Reply #4 on: May 20, 2018, 10:48:44 PM »
and it still crashes :(

Sure it crashes. Your code runs in 32-bit land, and int 21h has no useful meaning for the OS 8)

This is homework, right? Your teacher didn't tell you anything about, for example, the .model directive?

cioltealin

  • Regular Member
  • *
  • Posts: 6
Re: Code can be compiled but it crashes on run
« Reply #5 on: May 20, 2018, 10:49:49 PM »
Thanks,I want to make it for 32 bits,for 16 bits I get error at  mov [di],si ,it says that di should be for 32 bit
I guess that even if I want to make it for 16 bits I have to use 32 bits because of the compiler

@jj2007
He just said to find and correct the errors,nothing about the .model directive

jj2007

  • Member
  • *****
  • Posts: 8429
  • Assembler is fun ;-)
    • MasmBasic
Re: Code can be compiled but it crashes on run
« Reply #6 on: May 20, 2018, 11:06:26 PM »
He just said to find and correct the errors,nothing about the .model directive

I can imagine :P

Btw which IDE/editor are you using, and which assembler? Masm version 6.14 assembles your snippet "as is", but it clearly believes you want to create 32-bit code. To make it assemble as 16-bit code (and that's what it is), you need the extra stuff posted by José alias aw27, i.e.
Code: [Select]
.model  small
.stack  100h

However, it will not run on any modern OS version :(

cioltealin

  • Regular Member
  • *
  • Posts: 6
Re: Code can be compiled but it crashes on run
« Reply #7 on: May 20, 2018, 11:31:10 PM »
He just said to find and correct the errors,nothing about the .model directive

I can imagine :P

Btw which IDE/editor are you using, and which assembler? Masm version 6.14 assembles your snippet "as is", but it clearly believes you want to create 32-bit code. To make it assemble as 16-bit code (and that's what it is), you need the extra stuff posted by José alias aw27, i.e.
Code: [Select]
.model  small
.stack  100h

However, it will not run on any modern OS version :(

I use the header from other exercises
Code: [Select]
.386 ;tried also with .186
.model flat, stdcall
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

includelib msvcrt.lib
extern exit: proc

I use notepad++ with an plugin made by the teacher for masm that is from 2013,the dll doesn't specify the version


jj2007

  • Member
  • *****
  • Posts: 8429
  • Assembler is fun ;-)
    • MasmBasic
Re: Code can be compiled but it crashes on run
« Reply #8 on: May 20, 2018, 11:44:44 PM »
For Masm (and its clones), .model flat, stdcall means 32-bit code. Your program is 16-bit code.

cioltealin

  • Regular Member
  • *
  • Posts: 6
Re: Code can be compiled but it crashes on run
« Reply #9 on: May 21, 2018, 12:31:48 AM »
Why 16 bit can't run on 32 bits ?
Tried with small and worked,thanks!
Kinda hard for a simple example... :icon_rolleyes:

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 5425
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Code can be compiled but it crashes on run
« Reply #10 on: May 21, 2018, 12:35:12 AM »
Its a choice you need to make, either you write 16 bit code OR you write 32 bit code. They have different architecture and memory addressing modes and are not compatible. Mixing 16 and 32 bit code is simply not possible. Why a teacher wants to teach 16 bit code does not make sense, none of the modern OS versions will run it, the last was Win98se which still supported COMMAND.COM and this is at best late 1998.

There is a 16 bit section in the forum if that is what you have to do otherwise you will need to start writing 32 bit code which is different.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin:

AW

  • Member
  • *****
  • Posts: 1292
  • Let's Make ASM Great Again!
Re: Code can be compiled but it crashes on run
« Reply #11 on: May 21, 2018, 01:29:25 AM »
Let me add a bit to the confusion hoping someone will become nuts today.  :biggrin:
Quote
Why 16 bit can't run on 32 bits ?
Who said that? It can. Only under the flat model it can not.

And 32 bits can run under the .small, .medium, .large and other models and use the int 21. Is this what your teacher want? Do you know what he wants? Does he know what he wants?  ::)




hutch--

  • Administrator
  • Member
  • ******
  • Posts: 5425
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Code can be compiled but it crashes on run
« Reply #12 on: May 21, 2018, 01:59:14 AM »
aw is correct here but only if you can find an OS that supports multiple memory models. Win32 uses a different architecture where you have linear addressing thus the FLAT memory model. Back in Win98se or the dreadful Millenium version that flopped you have a real mode DOS under the 32 bit but from Windows 2000 upwards the 16 bit sub system was not present and 16 bit real mode DOS will not run on a 32 bit protected mode OS version.

I gather you can find a 16 bit emulator and run old code but if you really want to run 16 bit real mode code, you need a 16 bit real mode OS and the last I remember from Microsoft was DOS 6.22 in about 1993-4.

The other thing you need to build 16 bit applications is an OMF linker as a COFF linker does not build 16 bit OMF files.

What our friend needs to do is ASK his teacher what OS version you are supposed to run AND what form of asm he is supposed to write, 16 or 32 bit code.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin:

daydreamer

  • Member
  • ***
  • Posts: 413
Re: Code can be compiled but it crashes on run
« Reply #13 on: May 21, 2018, 02:35:08 AM »
welcome to forum
,dont choose 16bit,its restricted by segmented code for access 640k+ memory.while 32bit you c allocte around 4gb if you run in a 64bit OS,it has restrictions on indirect adressing so,while in 32bit mode you are free to use any combinations,DOS 16bit emulator maybe not support latest opcodes,MMX etc,but 32bit mode does
if you have VC++ experience,you have access to same winapi functions thru a invoke macro
and what you want is write the best code you can to get A+,instead of waste effort and time on learn how to do things in obsolote OS(DOS),while you have more use of know how to whip up a 8k windows program
good luck

Quote from Flashdance
Nick  :  When you give up your dream, you die.

mineiro

  • Member
  • ***
  • Posts: 391
Re: Code can be compiled but it crashes on run
« Reply #14 on: May 21, 2018, 02:50:28 AM »
Hello, welcome
When you turn your computer on, processor is set to work on 'real' mode, this means 16 bits, the one that ms-dos works by default. So, we can code to change processor mode to work on 'protected' mode, this means 32 bits (windows and linux) and we can change to 'long' mode, this means 64 bits (windows and linux). I'm not talking that ms-dos can't work with 32 or 64 bits, yes, it can, of course, if hardwired. Some processor instructions need priviledge to be used on 32 bits per example while on ms-dos not. An example is 'in' and 'out' instructions. You also have other modes like 'virtual', ..., but not the subject.
Your code use 'int 21h', this means that you need create a program to work on ms-dos because ms-dos offers to us programmers these routines (think like library or dll on windows side). Again, you have used on your program the function (routine) 4ch, this is an end routine or on other words, end my program and give control back to O.S.
By this, you need create an .exe file instead of .com file because .com program use int 20h as an end function.
Said this, and going back to past, a .com program is by default done to little programs, by default a mix of data and code. We have 16 bits of address space, this means that our program can have a size of 65535 bytes, or 64k. When we need more space to our program, we need more memory addresses. On that time each 'segment' can hold 64k bytes, so we need more segments inside our program if our program grow and switch between these segments to access data or code. On 32 bits each segment can hold 4gb (flat model), so we can forget about segments and offsets if our program size is below 4gb.
When ms-dos loaded a .com program it created on memory a PSP. This PSP need a size of 100h bytes on memory loaded before our program.( you can use org 100h, or stack 100h, ... ).
Your example have code and data segment, you can choose to mix both into only one segment and create a .com file (change ms-dos 'end' function properly). You can create an .exe file and set segments by using some registers segment (.model directive).
Have a lot of examples on old board and inside this new board on ms-dos section, enjoy.
I'd rather be this ambulant metamorphosis than to have that old opinion about everything