Assembly Language Addressing Modes Introduction CISC processors usually
Assembly Language Addressing Modes
Introduction • CISC processors usually supports more addressing modes than RISC processors. – RISC processors use the load/store architecture. – Operands are usually stored the processor’s registers. • The Pentium processors support the following addressing modes – Register Addressing Mode The input operands and results are stored back in registers. Since – Immediate Addressing Mode: The operand value is encoded as part of the instruction. The operand is available as soon as the instruction is read. – Memory Addressing Modes: The operand is in memory and access via several modes. The logical address is specified to determine the location of a memory operand. Memory addressing modes differ in how they specify the effective address.
Addressing Modes 16 bit addressing modes
Addressing Modes 32 bit addressing modes
Memory Addressing Modes • Different addressing modes can efficiently support high-level language constructs and data structures. • The memory addressing modes depend on the address size Segment + Base + (Index * Scale) + displacement CS EAX 1 No displacement SS EBX 2 8 -bit displacement DS ECX 4 32 -bit displacement ES EDX 8 FS ESI GS EDI EBP ESP
16 and 32 bit Addressing Modes • In a code segment, the D bit and specifies the default size for operands and offsets. – D = 0 the operands and offsets are 16 bits; otherwise they are 32 -bit. • D bit in the CS segment descriptor to determine if the address is 16 or 32 bits long • It is also possible to override these defaults.
Based Addressing • One of the registers is used as the base register in computing the effective address of an operand. • The effective address is computed by – Adding the contents of the base register with – A signed displacement value given as part of the instruction. • For 16 -bit addresses – The signed displacement is either an 8 - or a 16 -bit number. • For 32 -bit addresses, – The signed displacement is either an 8 - or a 32 -bit number. • Based addressing – Is convenient to access individual elements of a structure. – It useful for accessing arrays whose element size is not 2, 4, or 8 bytes.
Example
Indexed Addressing • The effective address is computed as (Index * scale factor) + signed displacement. • Scaling factor – no scaling factor is allowed for 16 -bit addresses – 2, 4, or 8 can be specified for 32 -bit adrresses. Example: add EAX, [EDI+20] mov EAX, [marks_table+ESI*4] add EAX, [table 1+ESI]
Based-Indexed Addressing • Based-Indexed with No Scale Factor Base + Index + signed displacement. The displacement can be – A signed 8 - or 16 -bit number for 16 -bit addresses – An 8 - or 32 -bit number for 32 -bit addresses. Ex. EBX points to table 1, which consists of four-byte elements mov EAX, [EBX+ESI] cmp EAX, [EBX+ESI+4] • Based-Indexed with Scale Factor Base + (Index * scale factor) + signed displacement. Provides an efficient indexing mechanism into a 2 D array – When the element size is 2, 4, or 8 bytes.
Code Example Sorting an integer array using the insertion sort. %include "io. mac". DATA MAX_SIZE EQU 100 input_prompt db "Please enter input array: " db "(negative number terminates input)", 0 out_msg db "The sorted array is: ", 0. UDATA array resd MAX_SIZE. CODE. STARTUP Put. Str input_prompt ; request input array mov EBX, array mov ECX, MAX_SIZE array_loop: Get. LInt EAX ; read an array number cmp EAX, 0 ; negative number? jl exit_loop ; if so, stop reading numbers mov [EBX], EAX ; otherwise, copy into array add EBX, 4 ; increment array address loop array_loop ; iterates a maximum of MAX_SIZE exit_loop:
Code Example Sorting an integer array using the insertion sort. mov EDX, EBX sub EDX, array shr EDX, 2 push EDX push array call insertion_sort Put. Str out_msg newline mov ECX, EDX mov EBX, array display_loop: Put. LInt [EBX] newline add EBX, 4 loop display_loop done: . EXIT ; EDX keeps the actual array size ; EDX = array size in bytes ; divide by 4 to get array size ; push array size & array pointer ; display sorted array
Code Example Sorting an integer array using the insertion sort. ; This procedure receives a pointer to an array of integers and the array size ; via the stack. The array is sorted by using insertion sort. %define SORT_ARRAY EBX insertion_sort: pushad ; save registers mov EBP, ESP mov EBX, [EBP+36] ; copy array pointer mov ECX, [EBP+40] ; copy array size mov ESI, 4 ; array left of ESI is sorted for_loop: mov EDX, [SORT_ARRAY+ESI] ; temp = array[i] mov EDI, ESI ; j = i-1 sub EDI, 4 while_loop: cmp EDX, [SORT_ARRAY+EDI] ; temp < array[j] jge exit_while_loop ; array[j+1] = array[j] mov EAX, [SORT_ARRAY+EDI] mov [SORT_ARRAY+EDI+4], EAX sub EDI, 4 ; j = j-1 cmp EDI, 0 ; j >= 0 jge while_loop
Code Example Sorting an integer array using the insertion sort. exit_while_loop: ; array[j+1] = temp mov [SORT_ARRAY+EDI+4], EDX add ESI, 4 ; i = i+1 dec ECX cmp ECX, 1 ; if ECX = 1, we are done jne for_loop sort_done: popad ; restore registers ret 8
Arrays • One dimensional arrays – displacement = subscript * element size in bytes. Example: test_marks[5] uses displacement 5 * 4 = 20
Arrays • Multidimensional arrays – Row-major ordering row-by-row, starts with the first row – Column major ordering column-by-column, starts with the first column. • Displacement displacement = (i * COLS + j) * ELEM_SIZE array[3, 1] is at displacement (3*3+1)*4=40. array
- Slides: 16