26
Carnegie Mellon 1 Lecture 7 198:231 Intro to Computer Organization IA32 Assembly Language Programming Part 4 198:231 Introduction to Computer Organization Lecture 7 Instructor: Nicole Hynes [email protected]

IA32 Assembly Language Programming Part 4 7-IA32... · Carnegie Mellon 1 198:231 Intro to Computer Organization Lecture 7 IA32 Assembly Language Programming Part 4 198:231 Introduction

Embed Size (px)

Citation preview

Carnegie Mellon

1

Lecture 7 198:231 Intro to Computer Organization

IA32 Assembly Language Programming Part 4 198:231 Introduction to Computer Organization Lecture 7

Instructor:

Nicole Hynes

[email protected]

Carnegie Mellon

2

Lecture 7 198:231 Intro to Computer Organization

Implementing Arrays

Global arrays

Can translate access to array element A[i]using scaled index addressing mode as follows:

Place base address A of array in register %edx

Place index i in register %ecx

A[i]= value stored in effective address (%edx,%ecx,4)

int A[5] = {20, -12, 4, 7, -9};

int main()

{

int i, sum = 0;

for (i = 0; i < 5; i++)

sum += A[i];

return sum;

}

Carnegie Mellon

3

Lecture 7 198:231 Intro to Computer Organization

Implementing Arrays

Translation to IA32 assembly code: Version 1

.data

.align 4

A: .long 20, -12, 4, 7, -9 # int A[5]

.text

.globl main

main:

# int sum mapped to %eax

# int i mapped to %ecx

pushl %ebp # save old %ebp

movl %esp, %ebp # set new %ebp

movl $0, %eax # sum = 0

movl $0, %ecx # i = 0

cmpl $5, %ecx # compare i:5

jge done # jump to done if i >= 5

movl $A, %edx # %edx = address A

loop: addl (%edx,%ecx,4),%eax # sum += A[i]

incl %ecx # i++

cmpl $5, %ecx # compare i:5

jl loop # jump to loop if i < 5

done: leave # restore %esp and %ebp

ret # return (%eax = sum)

Carnegie Mellon

4

Lecture 7 198:231 Intro to Computer Organization

Implementing Arrays

Using array base address as displacement Aforementioned method is general – can be used for a global array

(as in the example) or a local array allocated in the run-time stack (described later).

Note that for a global array, its address is directly accessible through its label:

Thus, can translate access to array element A[i]using another version of scaled index addressing mode as follows:

Place index i in register %ecx

A[i]= value stored in effective address A(,%ecx,4)

.data

.align 4

A: .long 20, -12, 4, 7, -9 # int A[5]

Note that base register argument is left blank; this is allowed.

Carnegie Mellon

5

Lecture 7 198:231 Intro to Computer Organization

Implementing Arrays

Translation to IA32 assembly code: Version 2

.data

.align 4

A: .long 20, -12, 4, 7, -9 # int A[5]

.text

.globl main

main:

# int sum mapped to %eax

# int i mapped to %ecx

pushl %ebp # save old %ebp

movl %esp, %ebp # set new %ebp

movl $0, %eax # sum = 0

movl $0, %ecx # i = 0

cmpl $5, %ecx # compare i:5

jge done # jump to done if i >= 5

loop: addl A(,%ecx,4),%eax # sum += A[i]

incl %ecx # i++

cmpl $5, %ecx # compare i:5

jl loop # jump to loop if i < 5

done: leave # restore %esp and %ebp

ret # return (%eax = sum)

Carnegie Mellon

6

Lecture 7 198:231 Intro to Computer Organization

Implementing Arrays

Using pointers This is particularly suitable if array is accessed sequentially.

Essentially, based on the below version of the previous C program:

Thus, can translate sequential access to array elements as follows: Place in register %edx the address of A (= address of A[0]) A[i] = value stored in effective address (%edx) Increment %edx by 4 to point to the next element of A

int A[5] = {20, -12, 4, 7, -9};

int main()

{

int i, sum = 0;

int *ptr;

ptr = A;

for (i = 0; i < 5; i++) {

sum += *ptr;

ptr++;

return sum;

}

Carnegie Mellon

7

Lecture 7 198:231 Intro to Computer Organization

Implementing Arrays

Translation to IA32 assembly code: Version 3

.data

.align 4

A: .long 20, -12, 4, 7, -9 # int A[5]

.text

.globl main

main:

# int sum mapped to %eax

# int i mapped to %ecx

# int *ptr mapped to %edx

pushl %ebp # save old %ebp

movl %esp, %ebp # set new %ebp

movl $0, %eax # sum = 0

movl $0, %ecx # i = 0

cmpl $5, %ecx # compare i:5

jge done # jump to done if i >= 5

movl $A, %edx # ptr = &A[0]

loop: addl (%edx),%eax # sum += A[i]

addl $4, %edx # ptr++

incl %ecx # i++

cmpl $5, %ecx # compare i:5

jl loop # jump to loop if i < 5

done: leave # restore %esp and %ebp

ret # return (%eax = sum)

Carnegie Mellon

8

Lecture 7 198:231 Intro to Computer Organization

Implementing Arrays

Passing a global array as a function argument

Address of array is passed to function (pass by reference).

As a result, function will be directly accessing the global array and not a copy of it.

int A[5] = {20, -12, 4, 7, -9};

int arraysum(int A[], int n)

{

int i, sum = 0;

for (i = 0; i < n; i++)

sum += A[i];

return sum;

int main()

{

printf("array sum = %d\n", arraysum(A, 5));

}

Carnegie Mellon

9

Lecture 7 198:231 Intro to Computer Organization

Implementing Arrays

Translation to IA32 assembly code

.data

.align 4

A: .long 20, -12, 4, 7, -9

.section .rodata

str: .string "array sum = %d\n"

.text

.globl main

main: pushl %ebp # save old %ebp

movl %esp, %ebp # set new %ebp

pushl $5 # push constant 5

pushl $A # push address A

call arraysum # call arraysum

addl $8, %esp # deallocate parms

pushl %eax # push r.v. from arraysum()

pushl $str # push str

call printf # call printf

addl $8, %esp # deallocate parms

leave # restore %esp and %ebp

ret # return

Carnegie Mellon

10

Lecture 7 198:231 Intro to Computer Organization

Implementing Arrays

Translation to IA32 assembly code, cont.

arraysum:

# int sum mapped to %eax

# int i mapped to %ecx

# parms: A is at 8(%ebp), n is at 12(%ebp)

pushl %ebp # save old %ebp

movl %esp, %ebp # set new %ebp

movl $0, %eax # sum = 0

movl $0, %ecx # i = 0

cmpl 12(%ebp), %ecx # compare i:n

jge done # jump to done if i >= n

movl 8(%ebp), %edx # %edx = address A

loop: addl (%edx,%ecx,4),%eax # sum += A[i]

incl %ecx # i++

cmpl 12(%ebp), %ecx # compare i:n

jl loop # jump to loop if i < n

done: leave # restore %esp and %ebp

ret # return (%eax = sum)

Carnegie Mellon

11

Lecture 7 198:231 Intro to Computer Organization

Implementing Arrays

Local arrays

What happens if we declare the array inside a function?

The array becomes a local array and will need to be allocated in the stack frame of the function.

If an initialized array, need to explicitly perform move instructions to initialize the array elements.

int main()

{

int A[5] = {20, -12, 4, 7, -9};

int i, sum = 0;

for (i = 0; i < 5; i++)

sum += A[i];

return sum;

}

Carnegie Mellon

12

Lecture 7 198:231 Intro to Computer Organization

Implementing Arrays

Translation to IA32 assembly code

.text

.globl main

main:

# int sum mapped to %eax

# int i mapped to %ecx

pushl %ebp # save old %ebp

movl %esp, %ebp # set new %ebp

/* Allocate space for array A on stack */

/* Base address of A is at -20(%ebp) */

subl $20, %esp

movl $20, -20(%ebp) # A[0] = 20

movl $-12, -16(%ebp) # A[1] = -12

movl $4, -12(%ebp) # A[2] = 4

movl $7, -8(%ebp) # A[3] = 7

movl $-9, -4(%ebp) # A[4] = -9

movl $0, %eax # sum = 0

movl $0, %ecx # i = 0

cmpl $5, %ecx # compare i:5

jge done # jump to done if i >= 5

leal -20(%ebp), %edx # %edx = address A

loop: addl (%edx,%ecx,4),%eax # sum += A[i]

incl %ecx # i++

cmpl $5, %ecx # compare i:5

jl loop # jump to loop if i < 5

done: addl $20, %esp # deallocate array from stack

leave # restore %esp and %ebp

ret # return (%eax = sum)

Saved %ebp

A[4]

A[3]

A[2]

A[1]

A[0]

4 bytes

%ebp

%esp

Increasing addresses

Carnegie Mellon

13

Lecture 7 198:231 Intro to Computer Organization

Implementing Structures

Global structure: initialized Allocate in the data segment with proper size and alignment

struct sample {

short int a;

int b[4];

char c;

int d;

char e;

};

struct sample s = {5, {10, 20, 30, 40}, 'h', 77, 'i'};

int main()

{

s.a = 8;

s.b[2] = -23;

s.c = 'w';

s.d = 154;

s.e = 'z';

}

Carnegie Mellon

14

Lecture 7 198:231 Intro to Computer Organization

Implementing Structures

Allocating the structure in the data segment

Translation to IA32 assembly code

struct sample {

short int a;

int b[4];

char c;

int d;

char e;

};

struct sample s = {5, {10, 20, 30, 40}, 'h', 77, 'i'};

.data

.align 4 # max alignment among members

s:

.word 5 # short int a = 5

.zero 2 # slack bytes

.long 10, 20, 30, 40 # int b[4] = {10, 20, 30, 40}

.byte 'h' # char c = 'h'

.zero 3 # slack bytes

.long 77 # int d = 77

.byte 'i' # char e = 'i'

.zero 3 # slack bytes

Carnegie Mellon

15

Lecture 7 198:231 Intro to Computer Organization

Implementing Structures

Data alignment requirements for structures 1. Structure is allocated a contiguous block of memory

2. Every member of structure must be properly aligned

Member type is k bytes → aligned at an address which is multiple of k

3. Entire structure must be properly aligned and sized

Let kmax = maximum k-alignment required for its members

Base address of structure must be a multiple of kmax

Size of memory allocated to structure must be a multiple of kmax

Results in automatic alignment of array of structures

To achieve the above, compiler: Inserts “slack bytes” if necessary to align successive members of

structure using .zero, .skip, or .space directives

Aligns entire structure using .align directive

Carnegie Mellon

16

Lecture 7 198:231 Intro to Computer Organization

Implementing Structures

Assembler directives

fill argument can be omitted; if so, fill is assumed to be zero

Will use .zero in our examples (gcc uses this directive)

Directive Description

.align k Place the following data object at a memory address that is a multiple of k

.zero k Emit k bytes each with value 0

.skip k, fill Emit k bytes each with value fill

.space k, fill Synonym of .skip

Carnegie Mellon

17

Lecture 7 198:231 Intro to Computer Organization

Implementing Structures

Allocating the structure in the data segment

Slack bytes inserted to align successive members

Slack bytes also added after last member to make structure size a multiple of 4 (=32 bytes)

.data

.align 4 # max alignment

s:

.word 5 # short int a = 5

.zero 2 # slack bytes

.long 10, 20, 30, 40 # int b[4]

.byte 'h' # char c = 'h'

.zero 3 # slack bytes

.long 77 # int d = 77

.byte 'i' # char e = 'i'

.zero 3 # slack bytes

s+28 e s+24 d

s+20 c

s+16 b[3]

s+12 b[2]

s+8 b[1]

s+4 b[0]

s a

4 bytes

Incr

eas

ing

add

ress

es

slack bytes

Mapping of struct members

data segment

Carnegie Mellon

18

Lecture 7 198:231 Intro to Computer Organization

Implementing Structures

Allocating the structure in the data segment

Slack bytes inserted to align successive members

Slack bytes also added after last member to make structure size a multiple of 4 (=32 bytes)

.data

.align 4 # max alignment

s:

.word 5 # short int a = 5

.zero 2 # slack bytes

.long 10, 20, 30, 40 # int b[4]

.byte 'h' # char c = 'h'

.zero 3 # slack bytes

.long 77 # int d = 77

.byte 'i' # char e = 'i'

.zero 3 # slack bytes

s+28 'i' s+24 77

s+20 'h'

s+16 40

s+12 30

s+8 20

s+4 10

s 5

4 bytes

Incr

eas

ing

add

ress

es

slack bytes

Values of struct members

data segment

Carnegie Mellon

19

Lecture 7 198:231 Intro to Computer Organization

Implementing Structures

Accessing the structure

Translation to IA32 assembly code

int main() {

s.a = 8;

s.b[2] = -23;

s.c = 'w';

s.d = 154;

s.e = 'z';

}

.text

.globl main

main:

pushl %ebp # save old %ebp

movl %esp, %ebp # set new %ebp

movl $s, %edx # %edx = address s

movw $8, (%edx) # s.a = 8

movl $2, %ecx # %ecx = 2

movl $-23, 4(%edx,%ecx,4) # s.b[2] = -23

movb $'w', 20(%edx) # s.c = 'w'

movl $154, 24(%edx) # s.d = 154

movb $'z', 28(%edx) # s.e = 'z'

leave # restore %esp and %ebp

ret # return

4 bytes

Incr

eas

ing

add

ress

es

slack bytes

s+28 e s+24 d

s+20 c

s+16 b[3]

s+12 b[2]

s+8 b[1]

s+4 b[0]

s a

Mapping of struct members

data segment

Carnegie Mellon

20

Lecture 7 198:231 Intro to Computer Organization

Implementing Structures

Accessing the structure

Translation to IA32 assembly code

int main() {

s.a = 8;

s.b[2] = -23;

s.c = 'w';

s.d = 154;

s.e = 'z';

}

.text

.globl main

main:

pushl %ebp # save old %ebp

movl %esp, %ebp # set new %ebp

movl $s, %edx # %edx = address s

movw $8, (%edx) # s.a = 8

movl $2, %ecx # %ecx = 2

movl $-23, 4(%edx,%ecx,4) # s.b[2] = -23

movb $'w', 20(%edx) # s.c = 'w'

movl $154, 24(%edx) # s.d = 154

movb $'z', 28(%edx) # s.e = 'z'

leave # restore %esp and %ebp

ret # return

4 bytes

Incr

eas

ing

add

ress

es

slack bytes

s+28 'z' s+24 154

s+20 'w'

s+16 40

s+12 -23

s+8 20

s+4 10

s 8

Structure values after executing main

data segment

Carnegie Mellon

21

Lecture 7 198:231 Intro to Computer Organization

Implementing Structures

Global structure: uninitialized Allocate in the bss segment with proper size and alignment

4 bytes

Incr

eas

ing

add

ress

es

Mapping of struct members

bss segment struct sample {

short int a;

int b[4];

char c;

int d;

char e;

};

struct sample s;

int main()

{

s.a = 8;

s.b[2] = -23;

s.c = 'w';

s.d = 154;

s.e = 'z';

}

32 bytes

s+28 e s+24 d

s+20 c

s+16 b[3]

s+12 b[2]

s+8 b[1]

s+4 b[0]

s a

bss segment

Carnegie Mellon

22

Lecture 7 198:231 Intro to Computer Organization

Implementing Structures

Global structure: uninitialized Translation to IA32 assembly code

No change in main code

4 bytes

Incr

eas

ing

add

ress

es

slack bytes

s+28 e s+24 d

s+20 c

s+16 b[3]

s+12 b[2]

s+8 b[1]

s+4 b[0]

s a

Mapping of struct members

bss segment .comm s,32,4 # allocate 32 bytes for

# struct s in bss segment

# aligned at an address

# which is a multiple of 4

.text

.globl main

main:

pushl %ebp # save old %ebp

movl %esp, %ebp # set new %ebp

movl $s, %edx # %edx = address s

movw $8, (%edx) # s.a = 8

movl $2, %ecx # %ecx = 2

movl $-23, 4(%edx,%ecx,4) # s.b[2] = -23

movb $'w', 20(%edx) # s.c = 'w'

movl $154, 24(%edx) # s.d = 154

movb $'z', 28(%edx) # s.e = 'z'

leave # restore %esp and %ebp

ret # return

Carnegie Mellon

23

Lecture 7 198:231 Intro to Computer Organization

Implementing Structures

Global structure: uninitialized Translation to IA32 assembly code

No change in main code

4 bytes

Incr

eas

ing

add

ress

es

slack bytes

s+28 'z'

s+24 154

s+20 'w'

s+16 0

s+12 -23

s+8 0

s+4 0

s 8

bss segment .comm s,32,4 # allocate 32 bytes for

# struct s in bss segment

# aligned at an address

# which is a multiple of 4

.text

.globl main

main:

pushl %ebp # save old %ebp

movl %esp, %ebp # set new %ebp

movl $s, %edx # %edx = address s

movw $8, (%edx) # s.a = 8

movl $2, %ecx # %ecx = 2

movl $-23, 4(%edx,%ecx,4) # s.b[2] = -23

movb $'w', 20(%edx) # s.c = 'w'

movl $154, 24(%edx) # s.d = 154

movb $'z', 28(%edx) # s.e = 'z'

leave # restore %esp and %ebp

ret # return Structure values after

executing main bss segment initially contains zeroed bytes

Carnegie Mellon

24

Lecture 7 198:231 Intro to Computer Organization

%ebp → saved %ebp -4 e -8 d

-12 c

-16 b[3]

-20 b[2]

-24 b[1]

-28 b[0]

-32 a

Implementing Structures

Local structure Allocate in the stack frame with proper size and alignment

4 bytes

Incr

eas

ing

add

ress

es

Mapping of struct members

stack struct sample {

short int a;

int b[4];

char c;

int d;

char e;

};

int main()

{

struct sample s;

s.a = 8;

s.b[2] = -23;

s.c = 'w';

s.d = 154;

s.e = 'z';

}

32 bytes

Carnegie Mellon

25

Lecture 7 198:231 Intro to Computer Organization

%ebp → saved %ebp -4 e -8 d

-12 c

-16 b[3]

-20 b[2]

-24 b[1]

-28 b[0]

-32 a

Implementing Structures

Local structure Structure members are at addresses which are fixed offsets from %ebp

4 bytes

Incr

eas

ing

add

ress

es

Mapping of struct members

stack .text

.globl main

main:

pushl %ebp # save old %ebp

movl %esp, %ebp # set new %ebp

subl $32, %esp # allocate 32 bytes

# for local struct s

movw $8, -32(%ebp) # s.a = 8

movl $2, %ecx # %ecx = 2

movl $-23, -28(%ebp,%ecx,4) # s.b[2] = -23

movb $'w', -12(%ebp) # s.c = 'w'

movl $154, -8(%ebp) # s.d = 154

movb $'z', -4(%ebp) # s.e = 'z‘

addl $32, %esp # deallocate struct

leave # restore %esp and %ebp

ret # return

Carnegie Mellon

26

Lecture 7 198:231 Intro to Computer Organization

%ebp → saved %ebp -4 'z'

-8 154

-12 'w'

-16 garbage

-20 -23

-24 garbage

-28 garbage

-32 8

Implementing Structures

Local structure Structure members are at addresses which are fixed offsets from %ebp

4 bytes

Incr

eas

ing

add

ress

es

stack .text

.globl main

main:

pushl %ebp # save old %ebp

movl %esp, %ebp # set new %ebp

subl $32, %esp # allocate 32 bytes

# for local struct s

movw $8, -32(%ebp) # s.a = 8

movl $2, %ecx # %ecx = 2

movl $-23, -28(%ebp,%ecx,4) # s.b[2] = -23

movb $'w', -12(%ebp) # s.c = 'w'

movl $154, -8(%ebp) # s.d = 154

movb $'z', -4(%ebp) # s.e = 'z‘

addl $32, %esp # deallocate struct

leave # restore %esp and %ebp

ret # return

Structure values after executing main unwritten members will have undefined values