PROWAREtech

articles » current » assembly » x86 » tutorial » page-04

Intel IA-32 Assembly Tutorial - A Guide to the Basics of x86 Assembly - Page 04

Language Elements (Symbolic Constants, More on Instructions: Data Transfer Instructions: MOV, MOVZX, MOVSX, LAHF, SAHF and XCHG).

Symbolic Constants

Symbolic constants are defined by the equal-sign directive like:

	COUNT = 1000
	mov	eax,COUNT

Would generate this statement:

	mov eax,1000

COUNT is not a variable; it is a constant. The following would produce an error.

	COUNT = 1000
mov COUNT,eax        ;cannot do this

EQU directive associates a symbol name with an integer expression or text.

name EQU integer expression
name EQU already defined symbol
name EQU <text>
	PI EQU <3.14159>

	hello EQU <"HELLO",0>
	.
	.
	.data
	greeting BYTE hello

More on Instructions

Data Transfer Instructions

The MOV instruction copies data from one location to another. The source and destination should be the same size. A DWORD is unsigned 32-bits.

	.data
	var1 DWORD 9Ah
	var2 DWORD ?
	.code
	mov eax,var1     ;this takes 1 clock cycle
	mov var2,eax     ;this takes 1 clock cycle

MOV cannot copy from one memory location to another. It must copy to a register then to the second memory location.

	mov var2,var1    ;CANNOT DO THIS!

The MOVZX instruction copies data from a smaller location to a larger one (unsigned). A WORD is unsigned 16-bits.

	.data
	var1 WORD 9Ah
	var2 BYTE Ah
	.code
	movzx eax,bx     ;this takes 3 clock cycles on a 486
	movzx eax,var1   ;this takes 3 clock cycles on a 486

Like MOV, MOVZX cannot copy from one memory location to another.

	movzx var1,var2  ;CANNOT DO THIS!

The MOVSX instruction copies data from a smaller location to a larger one (signed). A SWORD is signed 16-bits. A SBYTE is signed 8-bits.

	.data
	var1 SWORD -100
	var2 SBYTE -5
	.code
	movsx eax,bx     ;this takes 3 clock cycles on a 486
	movsx eax,var1   ;this takes 3 clock cycles on a 486

Like MOV and MOVZX, MOVSX cannot copy from one memory location to another.

	movsx var1,var2  ;CANNOT DO THIS!

The LAHF and SAHF instructions copy the low byte of the EFLAGS register to and from AH. This is so that you can restore the EFLAGS register. Neither of these instructions take operands.

	.data
	savedflags BYTE ?
	.code
	lahf              ;load eflags
	mov savedflags,ah
	.
	.
	.
	mov ah,savedflags
	sahf              ;save eflags

The XCHG instruction swaps the contents of two operands.

	.data
	var1 DWORD 10
	var2 DWORD 5
	.code
	xchg eax,ebx     ;this takes 3 clock cycles on a 486
	xchg eax,var1    ;this takes 5 clock cycles on a 486
	xchg var1,var2   ;CANNOT DO THIS!

Direct-offset operands add a displacement to the name of a variable which is needed for working with arrays.

	.data
	var1 DWORD 10,11,12
	.code
	mov eax,[var1+4] ;offset by 4 bytes so now eax equals 11
<<<[Page 4 of 15]>>>

PROWAREtech

Hello there! How can I help you today?
Ask any question

PROWAREtech

This site uses cookies. Cookies are simple text files stored on the user's computer. They are used for adding features and security to this site. Read the privacy policy.
ACCEPT REJECT