asum.ys A Y86 Programming Example

Slides:



Advertisements
Similar presentations
Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are.
Advertisements

Pipeline Enhancements for the Y86 Architecture
University of Washington Last Time For loops  for loop → while loop → do-while loop → goto version  for loop → while loop → goto “jump to middle” version.
Machine-Level Programming I: Introduction Topics Assembly Programmer’s Execution Model Accessing Information Registers Memory Arithmetic operations CS.
Machine-Level Programming III: Procedures Apr. 17, 2006 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables CS213.
1 Function Calls Professor Jennifer Rexford COS 217 Reading: Chapter 4 of “Programming From the Ground Up” (available online from the course Web site)
Accessing parameters from the stack and calling functions.
Practical Session 3. The Stack The stack is an area in memory that its purpose is to provide a space for temporary storage of addresses and data items.
Lecture Notes from Randal E. Bryant, CMU CS:APP Chapter 4 Computer Architecture Instruction Set Architecture CS:APP Chapter 4 Computer Architecture Instruction.
Machine-Level Programming III: Procedures Sept. 17, 2007 IA32 stack discipline Register saving conventions Creating pointers to local variablesx86-64 Argument.
– 1 – , F’02 ICS05 Instructor: Peter A. Dinda TA: Bin Lin Recitation 4.
Machine-Level Programming III: Procedures Jan 30, 2003
Instruction Set Architecture CSC 333. – 2 – Instruction Set Architecture Assembly Language View Processor state Registers, memory, … Instructions addl,
Machine-Level Programming III: Procedures Sept. 15, 2006 IA32 stack discipline Register saving conventions Creating pointers to local variablesx86-64 Argument.
Machine-Level Programming I: Introduction Apr. 14, 2008 Topics Assembly Programmer’s Execution Model Accessing Information Registers Memory Arithmetic.
Stack Activation Records Topics IA32 stack discipline Register saving conventions Creating pointers to local variables February 6, 2003 CSCE 212H Computer.
Recitation 2: Assembly & gdb Andrew Faulring Section A 16 September 2002.
Y86 Processor State Program Registers
CS:APP2e CS:APP Chapter 4 Computer Architecture Instruction Set Architecture.
Processor Architecture: The Y86 Instruction Set Architecture
1 Carnegie Mellon Stacks : Introduction to Computer Systems Recitation 5: September 24, 2012 Joon-Sup Han Section F.
Ithaca College Machine-Level Programming IV: IA32 Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2013 * Modified slides.
Randal E. Bryant adapted by Jason Fritts CS:APP2e CS:APP Chapter 4 Computer Architecture Instruction Set Architecture CS:APP Chapter 4 Computer Architecture.
Fabián E. Bustamante, Spring 2007 Machine-Level Programming III - Procedures Today IA32 stack discipline Register saving conventions Creating pointers.
Recitation 2 – 2/11/02 Outline Stacks & Procedures Homogenous Data –Arrays –Nested Arrays Mengzhi Wang Office Hours: Thursday.
Machine-Level Programming 1 Introduction Topics Assembly Programmer’s Execution Model Accessing Information Registers Memory Arithmetic operations.
Machine-level Programming III: Procedures Topics –IA32 stack discipline –Register saving conventions –Creating pointers to local variables.
1 Procedure Call and Array. 2 Outline Data manipulation Control structure Suggested reading –Chap 3.7, 3.8.
1 Processor Architecture. 2 Topics Write Y86 code Basic Logic design Hardware Control Language HCL Suggested Reading: 4.1, 4.2.
1 Processor Architecture. Coverage Our Approach –Work through designs for particular instruction set Y86---a simplified version of the Intel IA32 (a.k.a.
1 Assembly Language: Function Calls Jennifer Rexford.
IA32 Stack –Region of memory managed with stack discipline –Grows toward lower addresses –Register %esp indicates lowest stack address address of top element.
Machine-Level Programming 2 Control Flow Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of Loops Switch Statements.
CPSC 121: Models of Computation
Recitation 3: Procedures and the Stack
A job ad at a game programming company
CPSC 121: Models of Computation
Reading Condition Codes (Cont.)
Machine-Level Programming 2 Control Flow
Instruction Set Architecture
C function call conventions and the stack
Recitation 2 – 2/11/02 Outline Stacks & Procedures
Homework In-line Assembly Code Machine Language
Recitation 2 – 2/4/01 Outline Machine Model
Assembly Language Programming V: In-line Assembly Code
Machine-Level Programming II: Arithmetic & Control
Chapter 3 Machine-Level Representation of Programs
Machine-Level Programming 1 Introduction
Computer Architecture adapted by Jason Fritts then by David Ferry
Machine-Level Programming III: Switch Statements and IA32 Procedures
Y86 Processor State Program Registers
Machine-Level Programming 4 Procedures
Instructor: David Ferry
Condition Codes Single Bit Registers
Systems I Pipelining III
C Prog. To Object Code text text binary binary Code in files p1.c p2.c
Machine-Level Programming 2 Control Flow
Machine-Level Programming 2 Control Flow
Machine-Level Programming III: Procedures Sept 18, 2001
Machine-Level Representation of Programs III
Machine-Level Programming 2 Control Flow
Pipelined Implementation : Part I
Recap: Performance Comparison
Machine-Level Programming: Introduction
Chapter 3 Machine-Level Representation of Programs
“Way easier than when we were students”
CS-447– Computer Architecture M,W 10-11:20am Lecture 5 Instruction Set Architecture Sep 12th, 2007 Majd F. Sakr
Sequential Design תרגול 10.
Presentation transcript:

asum.ys A Y86 Programming Example Zhen Peng 11/14/2017

Y86 Sample Program Structure Program starts at address 0x0 Stack starts at address 0x100 Initialize the array (data) .pos 0 init: # Initialization ... call Main halt   .align 4 # Program data array: Main: # Main function call Sum ret Sum: # Length function .pos 0x100 # Place stack Stack:

3. init: irmovl Stack, %esp # Set up stack pointer 4. irmovl Stack, %ebp # Set up base pointer PC 0xc ZF SF OF 1 address stack 0x100 0xfc 0xf8 0xf4 0xf0 0xec register %eax %ecx %edx %ebx %esi %edi %esp 0x100 %ebp %esp %ebp Because line 46 and line 47 makes the label “Stack” at address 0x100, So the irmovls make %esp == 0x100 and %ebp == 0x100 now.

5. call main %esp %esp %ebp %esp PC 0x24 ZF SF OF 1 address stack address stack 0x100 0xfc 0xf8 0xf4 0xf0 0xec register %eax %ecx %edx %ebx %esi %edi %esp 0xfc %ebp 0x100 %esp %esp %ebp %esp 0x11 0x11 is the address of “6: halt”.

15. Main: pushl %ebp %ebp %esp %esp %esp PC 0x26 ZF SF OF 1 address address stack 0x100 0xfc 0xf8 0xf4 0xf0 0xec register %eax %ecx %edx %ebx %esi %edi %esp 0xf8 %ebp 0x100 %ebp %esp %esp 0x11 %esp 0x100

16. rrmovl %esp,%ebp %ebp %ebp %esp %ebp PC 0x28 ZF SF OF 1 address address stack 0x100 0xfc 0xf8 0xf4 0xf0 0xec register %eax %ecx %edx %ebx %esi %edi %esp 0xf8 %ebp %ebp %ebp 0x11 %esp %ebp 0x100

17. irmovl $4,%eax 18. pushl %eax # Push 4 %esp %esp %ebp %esp PC 0x30 ZF SF OF 1 address stack 0x100 0xfc 0xf8 0xf4 0xf0 0xec register %eax 4 %ecx %edx %ebx %esi %edi %esp 0xf4 %ebp 0xf8 0x11 %esp %esp %ebp 0x100 %esp 4 4 is the value of count (4 elements in the array).

19. irmovl array,%edx 20. pushl %edx %ebp %esp %esp %esp PC 0x38 ZF SF OF 1 address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0xec register %eax 4 %ecx %edx 0x14 %ebx %esi %edi %esp 0xf0 %ebp 0xf8 %ebp %esp %esp %esp 0x14 0x14 is the first element’s address of the array. Here we finished storing the arguments to be passed.

21. call Sum %ebp %esp %esp %esp PC 0x42 ZF SF OF 1 address stack address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec register %eax 4 %ecx %edx 0x14 %ebx %esi %edi %esp 0xec %ebp 0xf8 %ebp %esp %esp %esp 0x3d 0x3d is the address of “22: rrmovl %ebp,%esp”.

27. Sum: pushl %ebp 28. rrmovl %esp,%ebp %ebp %ebp %esp %esp %esp %ebp PC 0x46 ZF SF OF 1 address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 register %eax 4 %ecx %edx 0x14 %ebx %esi %edi %esp 0xe8 %ebp %ebp %ebp %esp %esp %esp %ebp 0xf8

29. mrmovl 8(%ebp),%ecx # ecx = Start 30. mrmovl 12(%ebp),%edx # edx = Count PC 0x52 ZF SF OF 1 address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 %esp, %ebp register %eax 4 %ecx 0x14 %edx %ebx %esi %edi %esp 0xe8 %ebp

31. xorl %eax,%eax # sum = 0 PC 0x54 ZF SF OF 1 address stack 0x100 address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 %esp, %ebp register %eax %ecx 0x14 %edx 4 %ebx %esi %edi %esp 0xe8 %ebp

32. andl %edx,%edx # Set condition codes PC 0x56 ZF SF OF address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 %esp, %ebp register %eax %ecx 0x14 %edx 4 %ebx %esi %edi %esp 0xe8 %ebp

33. je End PC 0x5b ZF SF OF address stack 0x100 0xfc 0x11 0xf8 0xf4 4 address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 %esp, %ebp register %eax %ecx 0x14 %edx 4 %ebx %esi %edi %esp 0xe8 %ebp Why need line 32 and 33? How to improve them? It’s like a if statement for checking.

34. Loop: mrmovl (%ecx),%esi # get *Start PC 0x61 ZF SF OF address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 %esp, %ebp register %eax %ecx 0x14 %edx 4 %ebx %esi 0xd %edi %esp 0xe8 %ebp Note that the parenthesis of (%ecx) is necessary.

35. addl %esi,%eax # add to sum 36. irmovl $4,%ebx # PC 0x69 ZF SF OF address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 %esp, %ebp register %eax 0xd %ecx 0x14 %edx 4 %ebx %esi %edi %esp 0xe8 %ebp

37. addl %ebx,%ecx # Start++ 38. irmovl $-1,%ebx # PC 0x71 ZF SF OF address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 %esp, %ebp register %eax 0xd %ecx 0x18 %edx 4 %ebx -1 %esi %edi %esp 0xe8 %ebp

39. addl %ebx,%edx # Count-- 40. jne Loop # Stop when 0 PC 0x5b ZF SF OF address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 %esp, %ebp register %eax 0xd %ecx 0x18 %edx 3 %ebx -1 %esi %edi %esp 0xe8 %ebp

34. Loop: mrmovl (%ecx),%esi # get *Start 35. addl %esi,%eax # add to sum PC 0x63 ZF SF OF address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 %esp, %ebp register %eax 0xcd %ecx 0x18 %edx 3 %ebx -1 %esi 0xc0 %edi %esp 0xe8 %ebp

37. addl %ebx,%ecx # Start++ 36. irmovl $4,%ebx # 37. addl %ebx,%ecx # Start++ PC 0x6b ZF SF OF address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 %esp, %ebp register %eax 0xcd %ecx 0x1c %edx 3 %ebx 4 %esi 0xc0 %edi %esp 0xe8 %ebp

39. addl %ebx,%edx # Count-- 38. irmovl $-1,%ebx # 39. addl %ebx,%edx # Count-- PC 0x73 ZF SF OF address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 %esp, %ebp register %eax 0xcd %ecx 0x1c %edx 2 %ebx -1 %esi 0xc0 %edi %esp 0xe8 %ebp

40. jne Loop # Stop when 0 PC 0x5b ZF SF OF address stack 0x100 0xfc address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 %esp, %ebp register %eax 0xcd %ecx 0x1c %edx 2 %ebx -1 %esi 0xc0 %edi %esp 0xe8 %ebp

34. Loop: mrmovl (%ecx),%esi # get *Start 35. addl %esi,%eax # add to sum PC 0x63 ZF SF OF address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 %esp, %ebp register %eax 0xbcd %ecx 0x1c %edx 2 %ebx -1 %esi 0xb00 %edi %esp 0xe8 %ebp

37. addl %ebx,%ecx # Start++ 36. irmovl $4,%ebx # 37. addl %ebx,%ecx # Start++ PC 0x6b ZF SF OF address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 %esp, %ebp register %eax 0xbcd %ecx 0x20 %edx 2 %ebx 4 %esi 0xb00 %edi %esp 0xe8 %ebp

39. addl %ebx,%edx # Count-- 38. irmovl $-1,%ebx # 39. addl %ebx,%edx # Count-- PC 0x73 ZF SF OF address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 %esp, %ebp register %eax 0xbcd %ecx 0x20 %edx 1 %ebx -1 %esi 0xb00 %edi %esp 0xe8 %ebp

30. jne Loop # Stop when 0 PC 0x5b ZF SF OF address stack 0x100 0xfc address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 %esp, %ebp register %eax 0xbcd %ecx 0x20 %edx 1 %ebx -1 %esi 0xb00 %edi %esp 0xe8 %ebp

34. Loop: mrmovl (%ecx),%esi # get *Start 35. addl %esi,%eax # add to sum PC 0x63 ZF SF OF address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 %esp, %ebp register %eax 0xabcd %ecx 0x20 %edx 1 %ebx -1 %esi 0xa000 %edi %esp 0xe8 %ebp

37. addl %ebx,%ecx # Start++ 36. irmovl $4,%ebx # 37. addl %ebx,%ecx # Start++ PC 0x6b ZF SF OF address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 %esp, %ebp register %eax 0xabcd %ecx 0x24 %edx 1 %ebx 4 %esi 0xa000 %edi %esp 0xe8 %ebp

39. addl %ebx,%edx # Count-- 38. irmovl $-1,%ebx # 39. addl %ebx,%edx # Count-- PC 0x73 ZF SF OF 1 address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 %esp, %ebp register %eax 0xabcd %ecx 0x24 %edx %ebx -1 %esi 0xa000 %edi %esp 0xe8 %ebp

40. jne Loop # Stop when 0 PC 0x78 ZF SF OF 1 address stack 0x100 0xfc address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 %esp, %ebp register %eax 0xabcd %ecx 0x24 %edx %ebx -1 %esi 0xa000 %edi %esp 0xe8 %ebp

41. End: rrmovl %ebp,%esp PC 0x7a ZF SF OF 1 address stack 0x100 0xfc address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 %esp, %ebp register %eax 0xabcd %ecx 0x24 %edx %ebx -1 %esi 0xa000 %edi %esp 0xe8 %ebp

42. popl %ebp %ebp %esp %esp %esp %ebp %ebp PC 0x7c ZF SF OF 1 address address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 register %eax 0xabcd %ecx 0x24 %edx %ebx -1 %esi 0xa000 %edi %esp 0xec %ebp 0xf8 %ebp %esp %esp %esp %ebp %ebp

43. ret %ebp %esp %esp %esp PC ZF SF OF 1 0x7c address stack 0x100 0x7c address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec 0xe8 register %eax 0xabcd %ecx 0x24 %edx %ebx -1 %esi 0xa000 %edi %esp 0xec %ebp 0xf8 %ebp %esp %esp %esp 0x3d 0x3d

22. rrmovl %ebp,%esp %esp %esp %esp PC 0x3f ZF SF OF 1 address stack 22. rrmovl %ebp,%esp address stack 0x100 0xfc 0x11 0xf8 %ebp 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 register %eax 0xabcd %ecx 0x24 %edx %ebx -1 %esi 0xa000 %edi %esp 0xf8 %ebp %esp %esp %esp

23. popl %ebp %ebp %esp %esp %esp %ebp %ebp PC 0x41 ZF SF OF 1 address address stack 0x100 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 register %eax 0xabcd %ecx 0x24 %edx %ebx -1 %esi 0xa000 %edi %esp 0xfc %ebp 0x100 %ebp %esp %esp %esp %ebp %ebp

24. ret %esp %esp %esp PC ZF SF OF 1 0x41 address stack 0x100 %ebp 0x41 address stack 0x100 %ebp 0xfc 0xf8 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 register %eax 0xabcd %ecx 0x24 %edx %ebx -1 %esi 0xa000 %edi %esp 0x100 %ebp %esp %esp %esp 0x11 0x11 0x11

6. halt PC ZF SF OF 1 address stack 0x100 %esp, %ebp 0xfc 0x11 0xf8 address stack 0x100 %esp, %ebp 0xfc 0x11 0xf8 0xf4 4 0xf0 0x14 0xec 0x3d 0xe8 register %eax 0xabcd %ecx 0x24 %edx %ebx -1 %esi 0xa000 %edi %esp 0x100 %ebp

A small question How to change line 32 and line 33 so that if count <= 0 the loop will not execute? 32: andl %edx, %edx 33: je End rrmovl %edx, %ebx # use %ebx as temporary place subl %eax, %ebx # here %eax == 0, so calculate %ebx - 0 jle End

Some Takeaways In the called function: Fun: pushl %ebp rrmovl %esp,%ebp # Set up the stack space Before ret operation: rrmovl %ebp,%esp popl %ebp Use conditional jumps to implement if statement and loops call operation: push the address of next instruction onto the stack ret operation: pop stack top value to PC (program counter)