Question 4.2 2: (Solution, p 5) Suppose that the HYMN CPU begins with the following in memory. addr data (translation) LOAD 11110

Similar documents
Assembly Language: Function Calls

Assembly Language: Function Calls" Goals of this Lecture"

Assembly Language: Function Calls" Goals of this Lecture"

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems

COMP 210 Example Question Exam 2 (Solutions at the bottom)

CMSC 313 Lecture 12. Project 3 Questions. How C functions pass parameters. UMBC, CMSC313, Richard Chang

CS 31: Intro to Systems Functions and the Stack. Martin Gagne Swarthmore College February 23, 2016

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site)

Procedure Calls. Young W. Lim Sat. Young W. Lim Procedure Calls Sat 1 / 27

CPSC W Term 2 Problem Set #3 - Solution

CPS104 Recitation: Assembly Programming

CSC 2400: Computing Systems. X86 Assembly: Function Calls"

Procedure Calls. Young W. Lim Mon. Young W. Lim Procedure Calls Mon 1 / 29

Homework. In-line Assembly Code Machine Language Program Efficiency Tricks Reading PAL, pp 3-6, Practice Exam 1

4) C = 96 * B 5) 1 and 3 only 6) 2 and 4 only

X86 Stack Calling Function POV

Machine-level Programming (3)

Assembly Language Programming - III

CMSC 313 Lecture 12 [draft] How C functions pass parameters

CS61 Section Solutions 3

Machine Program: Procedure. Zhaoguo Wang

Assembly III: Procedures. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CS 33: Week 3 Discussion. x86 Assembly (v1.0) Section 1G

CSE 351: Week 4. Tom Bergan, TA

CPEG421/621 Tutorial

ASSEMBLY III: PROCEDURES. Jo, Heeseung

Compiler Construction D7011E

Assembly III: Procedures. Jo, Heeseung

CS213. Machine-Level Programming III: Procedures

What is a Compiler? Compiler Construction SMD163. Why Translation is Needed: Know your Target: Lecture 8: Introduction to code generation

x86 assembly CS449 Fall 2017

CSC 2400: Computing Systems. X86 Assembly: Function Calls

Assembly level Programming. 198:211 Computer Architecture. (recall) Von Neumann Architecture. Simplified hardware view. Lecture 10 Fall 2012

AS08-C++ and Assembly Calling and Returning. CS220 Logic Design AS08-C++ and Assembly. AS08-C++ and Assembly Calling Conventions

CS , Fall 2004 Exam 1

administrivia today start assembly probably won t finish all these slides Assignment 4 due tomorrow any questions?

Assignment 11: functions, calling conventions, and the stack

The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, 2002

Machine-Level Programming III: Procedures

Second Part of the Course

Stack -- Memory which holds register contents. Will keep the EIP of the next address after the call

W4118: PC Hardware and x86. Junfeng Yang

Instruction Set Architecture

Intel assembly language using gcc

Lab 10: Introduction to x86 Assembly

CS , Fall 2001 Exam 1

Instruction Set Architecture

Instruction Set Architecture

You may work with a partner on this quiz; both of you must submit your answers.

Machine-Level Programming II: Control and Arithmetic

X86 Review Process Layout, ISA, etc. CS642: Computer Security. Drew Davidson

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College February 9, 2016

Instructor: Alvin R. Lebeck

1 /* file cpuid2.s */ 4.asciz "The processor Vendor ID is %s \n" 5.section.bss. 6.lcomm buffer, section.text. 8.globl _start.

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College September 25, 2018

System Programming and Computer Architecture (Fall 2009)

CS241 Computer Organization Spring 2015 IA

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING PREVIEW SLIDES 16, SPRING 2013

x86 architecture et similia

Assembly Programmer s View Lecture 4A Machine-Level Programming I: Introduction

Machine Programming 3: Procedures

CS61, Fall 2012 Midterm Review Section

Process Layout and Function Calls

Systems I. Machine-Level Programming V: Procedures

Implementing Threads. Operating Systems In Depth II 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Region of memory managed with stack discipline Grows toward lower addresses. Register %esp contains lowest stack address = address of top element

CMSC 313 Fall2009 Midterm Exam 2 Section 01 Nov 11, 2009

CS642: Computer Security

Ausgewählte Betriebssysteme. Anatomy of a system call

CS429: Computer Organization and Architecture

Y86 Processor State. Instruction Example. Encoding Registers. Lecture 7A. Computer Architecture I Instruction Set Architecture Assembly Language View

Sungkyunkwan University

X86 Assembly -Procedure II:1

Chapter 4 Processor Architecture: Y86 (Sections 4.1 & 4.3) with material from Dr. Bin Ren, College of William & Mary

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature

CAS CS Computer Systems Spring 2015 Solutions to Problem Set #2 (Intel Instructions) Due: Friday, March 20, 1:00 pm

CS 3843 Final Exam Fall 2012

Instruction Set Architecture

CISC 360 Instruction Set Architecture

CSCI 192 Engineering Programming 2. Assembly Language

ECE 391 Exam 1 Review Session - Spring Brought to you by HKN

Access. Young W. Lim Fri. Young W. Lim Access Fri 1 / 18

Intro to GNU Assembly Language on Intel Processors

CIT Week13 Lecture

CS , Fall 2002 Exam 1

Lecture 4 CIS 341: COMPILERS

Register Allocation, iii. Bringing in functions & using spilling & coalescing

IA32 Stack. Stack BoDom. Region of memory managed with stack discipline Grows toward lower addresses. Register %esp contains lowest stack address

Process Layout, Function Calls, and the Heap

x86 assembly CS449 Spring 2016

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control.

211: Computer Architecture Summer 2016

CS 31: Intro to Systems ISAs and Assembly. Martin Gagné Swarthmore College February 7, 2017

Access. Young W. Lim Sat. Young W. Lim Access Sat 1 / 19

Final exam. Scores. Fall term 2012 KAIST EE209 Programming Structures for EE. Thursday Dec 20, Student's name: Student ID:

Program Exploitation Intro

CSC 2400: Computer Systems. Using the Stack for Function Calls

ICS143A: Principles of Operating Systems. Midterm recap, sample questions. Anton Burtsev February, 2017

Digital Forensics Lecture 3 - Reverse Engineering

CSC 2400: Computer Systems. Using the Stack for Function Calls

Transcription:

Questions 1 Question 4.1 1: (Solution, p 5) Define the fetch-execute cycle as it relates to a computer processing a program. Your definition should describe the primary purpose of each phase. Question 4.1 2: (Solution, p 5) Explain in detail what the HYMN CPU does during the fetch phase of the fetch-execute cycle. (Your explanation should describe how the computer accesses values in registers and memory.) Question 4.2 1: (Solution, p 5) Suppose that the HYMN CPU begins with the following in memory. addr data (translation) 00000 100 11110 LOAD 11110 00001 101 11111 STORE 11111 00010 110 11110 ADD 11110 00011 101 11111 STORE 11111 00100 110 11110 ADD 11110 00101 101 11111 STORE 11111 00111 000 00000 HALT If the user typed multiples of 25 starting at 25 (25, then 50, then 75,... ) when prompted, what would the computer display? Question 4.2 2: (Solution, p 5) Suppose that the HYMN CPU begins with the following in memory. addr data (translation) 00000 100 11110 LOAD 11110 00001 110 11110 ADD 11110 00010 011 00001 JPOS 00001 00011 000 00000 HALT If we repeatedly type the number 32 computer halts? when prompted, how many times would we type it before the Question 4.2 3: (Solution, p 5) Suppose that the HYMN CPU begins with memory contents at right. a. List all new values stored in memory as the program executes. Express your answers in binary or hexadecimal. b. What values does the AC hold in the course of executing this program? Express your answers in binary or hexadecimal. addr data (translation) 00000 100 01001 LOAD 01001 00001 010 01000 JZER 01000 00010 110 01010 ADD 01010 00011 101 01010 STORE 01010 00100 100 01001 LOAD 01001 00101 110 00000 ADD 00000 00110 101 00000 STORE 00000 00111 001 00000 JUMP 00000 01000 000 00000 HALT 01001 000 00001 1 01010 000 00010 2 01011 000 00100 4 01100 000 00000 0

2 Questions Question 4.3 1: (Solution, p 5) Translate the following HYMN assembly language program into machine language. Express your answer in bits. READ top: WRITE ADD one JPOS top HALT one: 1 addr 00000 00001 00010 00011 00100 00101 00110 00111 data Question 4.3 2: (Solution, p 5) Write a HYMN assembly language program that reads a number from the user and then displays s absolute value. (The absolute value of a number is that number with any negative sign removed. The absolute value of is 5, while the absolute value of 3 is 3 itself.) Question 4.3 3: (Solution, p 6) Write a HYMN assembly language program that reads a number displays the powers of two that are less than. Your program may assume that is more than 1. and Question 5 1: (Solution, p 6) Consider the following Intel assembly code. again: movl $7, %eax movl $4, %ebx movl $4, %ecx pushl %eax addl %ebx, %eax popl %ebx decl %ecx jnz again Show all values taken on by the registers as this program executes. eax ebx ecx

Questions 3 Question 5 2: (Solution, p 6) Translate each of the following Intel assembly programs, generated by gcc, back to their nearest C equivalents. a. b..section.rodata.lc0:.string %d.lc1:.string %d %d n.section.text.globl main main:.l20:.l18: ret pushl %ebp movl %esp, %ebp subl $4, %esp leal -4(%ebp), %eax pushl %eax pushl $.LC0 call scanf movl $1, %ecx xorl %eax, %eax movl -4(%ebp), %edx cmpl %edx, %eax jge.l18 addl %eax, %ecx incl %eax cmpl %edx, %eax jl.l20 pushl %ecx pushl %edx pushl $.LC1 call printf xorl %eax, %eax leave.section.rodata.lc0:.string %d.lc1:.string %d n.section.text.align 4 main: pushl %ebp movl %esp, %ebp subl $4, %esp pushl %ebx leal -4(%ebp), %eax pushl %eax pushl $.LC0 call scanf xorl %ebx, %ebx cmpl -4(%ebp), %ebx jge.l18.l20: pushl %ebx pushl $.LC1 call printf addl %ebx, %ebx cmpl -4(%ebp), %ebx jl.l20.l18: xorl %eax, %eax movl -8(%ebp), %ebx leave ret Question 6.1 1: (Solution, p 6) Suppose that eax held 104 and esp held 20C when an x86 processor begins to execute the instruction pushl %eax. Explain how the CPU alters the values in registers and memory. Question 6.1 2: (Solution, p 6) Suppose we have a C function myst that takes two integers as an argument. int myst(int x, int y); Write an x86 assembly language code fragment that places the value of myst(6, 10) into the edx register. The fragment should include code to restore the program stack to its original state. Question 6.2 1: (Solution, p 6) Explain what the Intel processor does when it executes the instruction call fact. That is, explain how the CPU alters the values in registers and memory. Question 6.2 2: (Solution, p 7) What operations does an Intel processor perform in executing a ret instruction? That is, how do the values in registers change? How does the computer determine which instruction to execute next?

4 Questions Question 6.2 3: (Solution, p 7) How are parameter values passed to a subroutine, according to the Intel processor conventions? How does the subroutine communicate its return value? Question 6.2 4: (Solution, p 7) Define the purpose of the frame pointer (conventionally the ebp register on x86 processors). Question 6.2 5: (Solution, p 7) Consider the following C function and its Intel assembly translation at right. int add(int x, int y) { return x + y; add: pushl %ebp movl %esp, %ebp??? addl %ebx, %eax leave ret What two instructions should go in place of??? to load the x parameter into the eax register and the y parameter into the ebx register? Question 6.3 1: (Solution, p 7) Distinguish between callee-save registers (ebx, esi, edi on Intel processors) and caller-save registers (ecx, eax, edx on Intel processors).

Solutions 5 Solution 4.1 1: (Question, p 1) The fetch-execute cycle is the process by which a classical computer executes instructions. In the fetch phase, the computer determines the next instruction to be completed by fetching the instruction from memory. In the execute phase, the computer executes this instruction. The computer alternates between these two phases as long as it is on. Solution 4.1 2: (Question, p 1) It looks into the PC for a memory address, requests the information at that address from RAM via the bus, and stores RAM s response in the IR. Solution 4.2 1: (Question, p 1)? 25 25? 50 75? 75-106 (This last output is somewhat tricky: In the last ADD instruction, the CPU computes, but this exceeds the maximum eight-bit two s-complement number. So the computer wraps around ends up at.) Solution 4.2 2: (Question, p 1) It would read from the user four times before halting (with the AC progressing from to to to ). Solution 4.2 3: (Question, p 1) a. address 00000: 8A 8B 8C address 01010: 03 06 0A b. AC: 01 03 01 8A 03 06 01 8B 04 0A 01 8C 00 Solution 4.3 1: (Question, p 2) addr data (translation) 00000 100 11110 LOAD 11110 00001 101 11111 STORE 11111 00010 110 00101 ADD 00101 00011 011 00001 JPOS 00001 00100 000 00000 HALT 00101 000 00001 1 Solution 4.3 2: (Question, p 2) READ JPOS ok STORE n SUB n SUB n ok: WRITE HALT n: 0

6 Solutions Solution 4.3 3: (Question, p 2) READ STORE n up: LOAD i # display i WRITE ADD i # double i STORE i LOAD n # repeat if n - i > 0 SUB i JPOS up HALT n: 0 i: 1 Solution 5 1: (Question, p 2) eax 7 11 18 29 47 ebx 4 7 11 18 29 ecx 4 3 2 1 0 Solution 5 2: (Question, p 3) There will be considerable variation in the answers to these questions, but the following are the actual C programs used to generate the code. a. b. #include <stdio.h> #include <stdio.h> int main() { int i, a, n; int main() { int i, n; scanf("%d", &n); a = 1; for(i = 0; i < n; i++) { a += i; printf("%d %d\n", n, a); return 0; scanf("%d", &n); for(i = 0; i < n; i *= 2) { printf("%d\n", i); return 0; Solution 6.1 1: (Question, p 3) The processor will first decrease the value in esp by 4, and then it will store the contents of eax in that memory address. In this case, esp would change to 208, and the four bytes of memory beginning at address 208 would change to hold 104. Solution 6.1 2: (Question, p 3) pushl $10 pushl $6 call myst movl %eax, %edx # Push arguments onto stack # Call subroutine # Pop arguments from stack # Copy return value into edx Solution 6.2 1: (Question, p 3) It pushes the current value of eip onto the stack (decreasing esp by 4 in the process) and then it places the address of fact (the first instruction of the subroutine) into eip. This way, when the computer fetches the next instruction to execute, it fetches the first instruction of the fact subroutine, and the return address is lying on the stack for a later ret instruction to pop.

Solutions 7 Solution 6.2 2: (Question, p 3) The processor pops the top four bytes off the stack into the eip register. In doing this, it will add four to the esp register to represent the fact that the top four bytes are gone from the stack. The next instruction executed by the processor will be the instruction found at the address popped from the stack. Solution 6.2 3: (Question, p 4) Before the subroutine is called, the calling code should push the parameters onto the stack, with the first parameter pushed last. The called subroutine, then, can access the parameter values by looking into the stack relative to the stack pointer it receives. When a subroutine is to return a value, it should place this into the eax register, according to the Intel convention. Solution 6.2 4: (Question, p 4) The frame pointer is meant to contain the value of the esp at the time the system enters the current subroutine. The purpose of maintaining the frame pointer is to provide a fixed reference point from which to access local variables located on the stack and parameters (accessing items on the stack relative to esp is inconvenient since it shifts with every push and pop instruction). (Secondarily, it is also useful to have this so that the program can restore esp to the proper value before returning from the subroutine without worrying about taking care to pop each thing off the stack that was pushed.) Solution 6.2 5: (Question, p 4) movl 8(%ebp), %eax movl 12(%ebp), %ebx Solution 6.3 1: (Question, p 4) A subroutine is allowed to change the caller-save registers without restoring them, but it must ensure that callee-save registers, if used, are restored to their values on entering the subroutine.