irvine, kip r. assembly language for intel-based computers type and size operators type –returns...

14
Irvine, Kip R. Assembly Language For Intel-Based Computers TYPE and SIZE Operators • TYPE returns the size, in bytes of a single element of a data label (variable) • LENGTH returns a count of the number of individual elements in a data label that uses the DUP operator • SIZE returns the product of TYPE * LENGTH

Upload: john-bridges

Post on 17-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Irvine, Kip R. Assembly Language For Intel-Based Computers TYPE and SIZE Operators TYPE –returns the size, in bytes of a single element of a data label

Irvine, Kip R. Assembly Language For Intel-Based Computers

TYPE and SIZE Operators

• TYPE – returns the size, in bytes of a single element of a

data label (variable)

• LENGTH – returns a count of the number of individual

elements in a data label that uses the DUP operator

• SIZE – returns the product of TYPE * LENGTH

Page 2: Irvine, Kip R. Assembly Language For Intel-Based Computers TYPE and SIZE Operators TYPE –returns the size, in bytes of a single element of a data label

Irvine, Kip R. Assembly Language For Intel-Based Computers

.datamyByte db 1,2,3,4myWord dw 1000h,2000h,3000hmyDouble dd 12345678hmyQuad dq 1,2,3

.codemov ax,TYPE myByte ; 1mov ax,TYPE myWord ; 2mov ax,TYPE myDouble ; 4mov ax,TYPE myQuad ; 8

TYPE

TYPE returns the size attribute:

Page 3: Irvine, Kip R. Assembly Language For Intel-Based Computers TYPE and SIZE Operators TYPE –returns the size, in bytes of a single element of a data label

Irvine, Kip R. Assembly Language For Intel-Based Computers

.datamyByte db 20 dup(?)myWord dw 5 dup(0)

.codemov ax,LENGTH myByte ; 20mov ax,LENGTH myWord ; 5

LENGTH

Returns a count of the number of individual elements in a data label that uses the DUP operator:

Page 4: Irvine, Kip R. Assembly Language For Intel-Based Computers TYPE and SIZE Operators TYPE –returns the size, in bytes of a single element of a data label

Irvine, Kip R. Assembly Language For Intel-Based Computers

.datamyByte db 20 dup(?)myWord dw 5 dup(0)

.codemov ax,SIZE myByte ; 20 (20 * 1)mov ax,SIZE myWord ; 10 (5 * 2)

SIZE

Returns TYPE multiplied by LENGTH:

Page 5: Irvine, Kip R. Assembly Language For Intel-Based Computers TYPE and SIZE Operators TYPE –returns the size, in bytes of a single element of a data label

Irvine, Kip R. Assembly Language For Intel-Based Computers

JMP and LOOP Instructions

• JMP is an unconditional jump to a code label • LOOP creates a counting loop, using CX as

the default counter– LOOPD uses ECX as the counter register– LOOPW uses CX as the counter register

• (only necessary in 32-bit mode programming)

Page 6: Irvine, Kip R. Assembly Language For Intel-Based Computers TYPE and SIZE Operators TYPE –returns the size, in bytes of a single element of a data label

Irvine, Kip R. Assembly Language For Intel-Based Computers

JMP: Distance Modifiers

– JMP SHORT destination• +/- 127 bytes

– JMP NEAR PTR destination• same code segment• (default in the small and compact memory

models)– JMP FAR PTR destination

• different code segment• (default in the large, medium, and huge

memory models)

Page 7: Irvine, Kip R. Assembly Language For Intel-Based Computers TYPE and SIZE Operators TYPE –returns the size, in bytes of a single element of a data label

Irvine, Kip R. Assembly Language For Intel-Based Computers

JMP Example

Label1: . . jmp Label1

Unconditional Transfer of control to a label:

Page 8: Irvine, Kip R. Assembly Language For Intel-Based Computers TYPE and SIZE Operators TYPE –returns the size, in bytes of a single element of a data label

Irvine, Kip R. Assembly Language For Intel-Based Computers

LOOP Instruction

• Automatically uses CX as the counter– decrements it automatically

• If CX > 0, LOOP transfers control to a label– otherwise, excecution drops through

Page 9: Irvine, Kip R. Assembly Language For Intel-Based Computers TYPE and SIZE Operators TYPE –returns the size, in bytes of a single element of a data label

Irvine, Kip R. Assembly Language For Intel-Based Computers

mov cx,5 ; loop counter mov bx,1 ; value to be added mov ax,0 ; holds the sumL1: add ax,bx inc bx Loop L1

; AX=000F, BX=0006, CX=0000

LOOP Example

Task: sum the integers { 1,2,3,4,5 }

Page 10: Irvine, Kip R. Assembly Language For Intel-Based Computers TYPE and SIZE Operators TYPE –returns the size, in bytes of a single element of a data label

Irvine, Kip R. Assembly Language For Intel-Based Computers

Indirect Addressing

• Indirect Operands[si]. [di], [bx], [bp]

• Based and Indexed Operandsarray[si], array[di], array[bx]

• Base-Index Operands[bx+si], [bx+di]

• Base-Index with Displacementarray[bx+si], array[bx+di]

Page 11: Irvine, Kip R. Assembly Language For Intel-Based Computers TYPE and SIZE Operators TYPE –returns the size, in bytes of a single element of a data label

Irvine, Kip R. Assembly Language For Intel-Based Computers

A B C D E F G ...........0200 0205

aString [bx]

Indirect Operand Example

.data

aString db "ABCDEFG“

.code

mov bx,offset aString

add bx,5

mov dl,[bx]

Page 12: Irvine, Kip R. Assembly Language For Intel-Based Computers TYPE and SIZE Operators TYPE –returns the size, in bytes of a single element of a data label

Irvine, Kip R. Assembly Language For Intel-Based Computers

.dataaList db 10h,20h,30hsum   db 0.codemov bx,offset aList   mov al,[bx] ; AL = 10hinc bxadd al,[bx] ; AL = 30hinc bxadd al,[bx] ; AL = 60hmov si,offset sum ; get offset of summov [si],al ; store the sum

Adding 8-bit Integers

If you want to paste a code example such as this into a program, remember that the code segment must always begin with the following statements:mov ax,@datamov ds,ax

Page 13: Irvine, Kip R. Assembly Language For Intel-Based Computers TYPE and SIZE Operators TYPE –returns the size, in bytes of a single element of a data label

Irvine, Kip R. Assembly Language For Intel-Based Computers

.datawordList dw 1000h,2000h,3000h, 0.codemov bx,offset wordListmov ax,[bx] ; first numberadd ax,[bx+2] ; second numberadd ax,[bx+4] ; third numbermov [bx+6],ax ; store the sum

1000 2000 C3000 (sum)

[bx] [bx+2] [bx+4] [bx+6]

Adding 16-bit Integers

Page 14: Irvine, Kip R. Assembly Language For Intel-Based Computers TYPE and SIZE Operators TYPE –returns the size, in bytes of a single element of a data label

Irvine, Kip R. Assembly Language For Intel-Based Computers

32-Bit Registers

.386

mov ax,[ebx+3]

mov dl,string[edx]

mov ecx,[esi]

mov ebx,[eax]

The .386 directive permits any of the following registers to be used as indirect operands: EAX, EBX, ECX, EDX, ESI, EDI, EBP