Ideal mode crash course

Chris Dragan

Turbo Assembler's ideal mode contains a subset of MASM-mode commands. Its aim is to simplify coding and to bring the programmer closer to the output code.

The main difference between ideal and MASM modes is in addressing. In MASM mode the user may enter many weird combinations of registers and variables, some of which are similar to high level languages. The following are valid in MASM mode:

	mov	ax, MyVariable
	mov	ax, MyArray[bx]
	mov	ax, es:[bx][si]

In ideal mode, there is only one valid and true way of addressing, which conforms to the actual addressing mode the processor recognizes. The above styles are thus invalid, and the following must be used instead:

	mov	ax, [MyVariable]
	mov	ax, [MyArray+bx]
	mov	ax, [es:bx+si]

As we can see, a memory reference (pointer expression) is always in square brackets. This obviously prevents taking a constant for a variable. The order of registers and/or labels is not important, only segment registers must come first.

	mov	ax, [ss:3*4+si+MyArray+bx]

Often, operand size overriding is needed, especially when the instruction operands do not include any registers or defined variables. In MASM mode this is done with the PTR expression.

	inc	byte ptr [bx]
	cmp	word ptr -8[si][bp], 7

In ideal mode, the PTR keyword is not needed, and the size specifier comes inside the square brackets as first.

	inc	[byte bx]
	cmp	[word si-8+bp], 7

Using structures is also simple. Suppose that we declare a structure:

	   field1 dw ?
	   field2 db ?

An example of data declaration using the structure:

	MyStruc STRUCTURE ?		 ; Uninitialized
	MyStruc STRUCTURE <0FFFFh, 0FFh> ; Initialized

We can access the structure in the following ways:

	mov	ax, [MyStruc.field1]
	mov	al, [(STRUCTURE bx+si).field2]

If we want to add something else in the addressing expression AFTER the field specifier, we have to take the entire structure pointer in parentheses:

	mov	al, [bx+MyStruc.field1]   ; Parentheses not needed
	mov	al, [(MyStruc.field1)+bx] ; In parentheses
	mov	al, [((STRUCTURE bx).field2)+si]
	mov	al, [(STRUCTURE bx+si).field2]

Structure and union field identifiers are local. This means that the same field identifiers can be used in several different structures.

	struc STR1
	   dwSize   dd ?
	   bValue   db ?
	struc STR2
	   dwSize   dd ? ; same as in STR1
	   wValue   dw ?

Ideal mode is enabled using the ideal keyword. All directives with dot in MASM mode do not have the leading dot in ideal mode. Processor selection directives have p instead of dot. Label-directives come BEFORE labels (the alternative MASM syntax). Examples (MASM equivalents are commented):

	model flat	; .model flat
	codeseg 	; .code
	udataseg	; .data?
	const		; .const
	stack 1000h	; .stack 1000h
	p386		; .386
	pmmx		; .mmx
	proc MyProc	; MyProc proc
	struc MyStruc	; MyStruc struc
	union MyUnion	; MyUnion union
	segment MySeg	; MySeg segment
	macro MyMacro	; MyMacro macro

Ideal mode supports one instruction that isn't supported in MASM mode: far jump.

	jmp	far 1000h:24255133h
	jmp	far 0FFFFh:0

These are the major differences between ideal mode and MASM mode. It is of course the programmer's choice whether he will use this or that mode.

Click here to see a working example of a Win32 program that shows some advanced features available in ideal mode.

Chris Dragan