Computer Architecture and System Software Lecture 07: Assembly Language Programming

Similar documents
Computer Architecture and System Software Lecture 06: Assembly Language Programming

Experiment 3 3 Basic Input Output

Lecture 16: Passing Parameters on the Stack. Push Examples. Pop Examples. CALL and RET

The Stack. Lecture 15: The Stack. The Stack. Adding Elements. What is it? What is it used for?

Assembling, Linking and Executing 1) Assembling: .obj obj .obj.lst .crf Assembler Types: a) One pass assembler:

SPRING TERM BM 310E MICROPROCESSORS LABORATORY PRELIMINARY STUDY

CS401 Assembly Language Solved MCQS From Midterm Papers

db "Please enter up to 256 characters (press Enter Key to finish): ",0dh,0ah,'$'

Intel 8086: Instruction Set

Objectives. ICT106 Fundamentals of Computer Systems Topic 8. Procedures, Calling and Exit conventions, Run-time Stack Ref: Irvine, Ch 5 & 8

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB. Lab # 7. Procedures and the Stack

Experiment 8 8 Subroutine Handling Instructions and Macros

Microcomputer Architecture..Second Year (Sem.2).Lecture(2) مدرس المادة : م. سندس العزاوي... قسم / الحاسبات

Code segment Stack segment

BAHAR DÖNEMİ MİKROİŞLEMCİLER LAB3 FÖYÜ

EEM336 Microprocessors I. Data Movement Instructions

ORG ; TWO. Assembly Language Programming

EC 333 Microprocessor and Interfacing Techniques (3+1)

SHEET-2 ANSWERS. [1] Rewrite Program 2-3 to transfer one word at a time instead of one byte.

Microprocessor and Assembly Language Week-5. System Programming, BCS 6th, IBMS (2017)

Assembly Language Lab # 9

SRI VENKATESWARA COLLEGE OF ENGINEERING AND TECHNOLOGY DEPARTMENT OF ECE EC6504 MICROPROCESSOR AND MICROCONTROLLER (REGULATION 2013)

if 2 16bit operands multiplied the result will be

Chapter 3: Addressing Modes

CC411: Introduction To Microprocessors

UNIT 4. Modular Programming

LABORATORY 1: EXPLORING THE SOFTWARE ARCHITECTURE OF THE MICROPROCESSOR

Q. State and Explain steps involved in program development. [w-08, w-10, s-12, w-11]

UNIVERSITY OF CALIFORNIA, RIVERSIDE

EC-333 Microprocessor and Interfacing Techniques

SPRING TERM BM 310E MICROPROCESSORS LABORATORY PRELIMINARY STUDY

Transfer of Control. Lecture 10 JMP. JMP Formats. Jump Loop Homework 3 Outputting prompts Reading single characters

.code. lea dx,msg2. Page 1/8. Problem 1: Programming in Assembly [25 Points]

Lecture 15 Intel Manual, Vol. 1, Chapter 3. Fri, Mar 6, Hampden-Sydney College. The x86 Architecture. Robb T. Koether. Overview of the x86

Experiment #5. Using BIOS Services and DOS functions Part 1: Text-based Graphics

Summer 2003 Lecture 4 06/14/03

CNIT 127: Exploit Development. Ch 1: Before you begin. Updated

Computer Architecture 1 ح 303

Mr. Sapan Naik 1. Babu Madhav Institute of Information Technology, UTU

Copyright 2000 by Barry B. Brey The CPU Scheduling Processes

Come and join us at WebLyceum

Week /8086 Microprocessor Programming II

THE UNIVERSITY OF TRINIDAD & TOBAGO

reply db y prompt db Enter your favourite colour:, 0 colour db 80 dup(?) i db 20 k db? num dw 4000 large dd 50000

Tutorial Letter 103/3/2012 Computer Organization COS2621 Semesters 1 & 2

8086 INTERNAL ARCHITECTURE

ADVANCE MICROPROCESSOR & INTERFACING

LABORATORY WORK NO. 8 WORKING WITH MACROS AND LIBRARIES

Experiment N o 3 Segmentation and Addressing Modes

Procedures and the Stack. Chapter 4 S. Dandamudi

Microprocessor. By Mrs. R.P.Chaudhari Mrs.P.S.Patil

EE 314 Spring 2003 Microprocessor Systems

Computer Architecture and System Software Lecture 04: Floating Points & Intro to Assembly

Ex: Write a piece of code that transfers a block of 256 bytes stored at locations starting at 34000H to locations starting at 36000H. Ans.

CS401 Assembly Language Solved Subjective MAY 03,2012 From Midterm Papers. MC

CG2007 Microprocessor systems.

IA-32 Architecture. CS 4440/7440 Malware Analysis and Defense

Chapter Three Addressing Mode MOV AX, BX

The x86 Architecture

Practical Malware Analysis

Constants and Expressions. Lecture 7: Assembly Language Programs. Constants and Expressions (cont.) Statements. Names. Assembly Directives

MICROPROCESSOR PROGRAMMING AND SYSTEM DESIGN

PHY4635/5635 Spring Lecture 8: Program Control Instructions

Experiment N o 3. Segmentation and Addressing Modes

Segmentation in Assembly Language Programming

Lab 3: Defining Data and Symbolic Constants

PESIT Bangalore South Campus

Microprocessors ( ) Fall 2010/2011 Lecture Notes # 15. Stack Operations. 10 top

Summer 2003 Lecture 15 07/03/03

VARDHAMAN COLLEGE OF ENGINEERING (AUTONOMOUS) Shamshabad, Hyderabad

Scott M. Lewandowski CS295-2: Advanced Topics in Debugging September 21, 1998

1-Operand instruction types 1 INC/ DEC/ NOT/NEG R/M. 2 PUSH/ POP R16/M16/SR/F 2 x ( ) = 74 opcodes 3 MUL/ IMUL/ DIV/ DIV R/M

Computer Architecture and System Software Lecture 12: Review. Instructor: Rob Bergen Applied Computer Science University of Winnipeg

SYSC3601 Microprocessor Systems. Unit 2: The Intel 8086 Architecture and Programming Model

EC-333 Microprocessor and Interfacing Techniques

Basic Assembly SYSC-3006

22 Assembly Language for Intel-Based Computers, 4th Edition. 3. Each edge is a transition from one state to another, caused by some input.

EC 333 Microprocessor and Interfacing Techniques (3+1)

Computer Processors. Part 2. Components of a Processor. Execution Unit The ALU. Execution Unit. The Brains of the Box. Processors. Execution Unit (EU)

Data Movement Instructions

Constants and. Lecture 7: Assembly Language Programs. Expressions (cont.) Constants and. Statements. Expressions

UMBC. contain new IP while 4th and 5th bytes contain CS. CALL BX and CALL [BX] versions also exist. contain displacement added to IP.

Lecture (08) x86 programming 7

Parameter Passing. Procedure line. Calling procedure line. Most subroutines require parameters Can sometimes pass parameters via registers

Lesson 4. Stack, Procedures and Macros

ELEC 242 Time Delay Procedure

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB

1. Introduction to Assembly Language

Module 8: Atmega32 Stack & Subroutine. Stack Pointer Subroutine Call function

Department of Computer Science and Engineering

3- ADDRESSING MODES in 8086: In this section we use the MOV instruction to describe the data-addressing modes. Figure 3-1 shows the MOV instruction.

COE 205 Lab Manual Experiment N o 12. Experiment N o Using the Mouse

Arithmetic Instructions

Experiment N o 8. String Handling Instructions

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Electronics and Communication

Basic characteristics & features of 8086 Microprocessor Dr. M. Hebaishy

x86 Assembly Tutorial COS 318: Fall 2017

16.317: Microprocessor Systems Design I Fall 2013

Assembly Language. Dr. Esam Al_Qaralleh CE Department Princess Sumaya University for Technology. Overview of Assembly Language

9/25/ Software & Hardware Architecture

Transcription:

Computer Architecture and System Software Lecture 07: Assembly Language Programming Instructor: Rob Bergen Applied Computer Science University of Winnipeg

Announcements New assembly examples uploaded to website Assignment 3 solution uploaded Assignment 4 uploaded

Midterm This Friday during your normal lab period Short review today

Assignment 3 solutions Available on website

Procedures push ax push dx call myproc ;push contents of register ax to stack ;push contents of register dx to stack ;call (procedure name) ;instruction executed after procedure ; procedure myproc proc mov ah, 02h mov dl, S int 21 h ret myproc endp ;(procedure name) proc ;code for displaying S ;return to instruction after call ;(procedure name) endp

IP register and RET Calling a procedure pushes the IP register to the stack This is later popped from the stack at ret, so that the assembly program can continue executing instructions where it left off before the call Therefore, procedures should not push to stack without popping otherwise the wrong address will be popped to the IP register

An infinite loop mov dx, 0 ;dx = 0 call myproc ;push IP register to stack myproc proc push dx ;push 0 to stack ret myproc endp ;pop 0 to IP register ;procedure ends. Next instruction to be executed will be at address CS:00 (Effective address 0 of code segment)

Recursion Procedures may call themselves Process is similar to recursion in C Procedure call pushes IP register and sets IP to beginning of procedure Can be tricky because of some restrictions on registers

The Stack Upon interrupts, we saw that registers and flags were pushed to the stack before context switch Operating system then handles I/O request Registers and flags popped/restored and program continues normally The stack is used similarly in procedure calls, except: Only IP register is pushed to stack automatically Any other registers being pushed are handled by called program or callee

The Stack How does the stack evolve? Stack grows towards smaller addresses Stack size is static and predetermined Stack Base Pointer register (BP) points at the bottom of the stack Stack Pointer (SP) points to top of stack

The Stack If stack grows beyond stack limit we have stack overflow Upon a stack overflow in modern computers, the OS steps in and kills program (protecting memory spaces of other processes) In DOSBOX, process will keep running after stack overflow. Let s look at a high level view of the stack during execution of a C program

The Stack Main() program assigned its own stack frame Size depends on local variables Main() will call another function, and will pass two arguments. These arguments are pushed to the stack Space is also reserved for return value of function

The Stack Called function foo() gets its own stack frame for any local variables Upon completion, program returns to instruction after program call and the stack shrinks

The Stack Stack can get out of hand if you are using too many recursive functions Can access any point in stack using BP/SS registers Mov bp, 0 mov ax, [bp] mov ax, SS:[0] Stack does not handle dynamic memory allocation, this is handled by the heap (not covered in this course) eg. malloc()

The Stack Let s look at a low level view of the stack Stack Segment Code Segment SP = 0x0004 SS = 0x076C Before execution, SS points to base of stack (smallest address) SP points to top of stack (largest address) Data Segment Note SP holds an effective address, while SS holds a physical address.

The Stack In the previous example, the stack segment was 4 bytes large. When the stack is empty, SP points to SS with an offset of [stack size]. Pushing data onto the stack decrements SP, since the stack grows towards smaller addresses. Code and Data segments are of arbitrary size.

The Stack Push dx Push cx ; dx = 23FFh ; cx = 1234h Code Segment Stack Segment 23 FF 12 34 SS = 0x076C SP = 0x0000 Data Segment

The Stack Since we have 16 data pins, we must push 16-bits at a time. This initializes two bytes of the stack and SP is decremented twice for each push. Little endian architecture Low order bits stored in smaller memory addresses. What happens if we keep pushing dx? SP decremented from 0x0000 to 0xFFFE (stack overflow) Dx pushed to SS:FFFE

The Stack Push dx ; dx = 23FFh Code Segment SP = 0xFFFE Dx pushed somewhere in code or another process mem space (depending on size of code segment) Stack Segment 23 FF 12 34 SS = 0x076C Data Segment

Arrays Arrays in assembly are 1D, and are defined in the following ways: myarray db 8 dup(?) myarray db 8 dup(1) myarray db 0,1,2,3,4,5,6,7 ;8 element uninitialized array ;8 element array all elements initialized to 1 ;8 element array [0 1 2 3 4 5 6 7] myarray dw 4245h, FFFFh, 0017h,C642h ;8 element array [45h 42h FFh FFh 17h 00h 42h C6h]

Arrays As with strings, the effective address of the array is the memory address of the first element Load effective address instruction (lea) can be used in the same way Lea dx, myarray ;loads effective address of ;array in dx Remember indexing into memory must be done using registers si, di, or bx.

Arrays Using register indirect addressing, we can easily access elements of the array mov al, myarray[4] ;Move array element 4 ; to register al As always, be careful about operand sizes and memory to memory operations, which are forbidden mov myarray[2], myarray[5] ;mem to mem forbidden The offset [num] is the offset in bytes, regardless of whether the array was defined using db/dw/dq

Assembly examples Today we will step through a number of assembly language examples, also available online: Assignment 3 solution getint procedure for assignment 4 Buffered string processing examples Factorial/Recursive procedure

Assignment 4 print_int procedure should perform the following steps: 1. Have the number you want to print in register al 2. Iteratively divide number by 10 using div instruction. Push remainder to stack. For example, with input 144 144/10 = 14 +4R push 4 14/10 = 1 +4R push 4 1/10 = 0 + 1R push 1 Think about how to set up the number of iterations in this loop. 3. Stack now looks like 144, with 1 being pushed last 4. In a new loop, push digits off one at a time, ASCII adjust, print.

Assignment 4 Tips: Try print_int before Newton s algorithm. Newton s algorithm will be easier to debug once you can actually print the results. Use the debugger to trace register and memory values. Make sure you are performing 8-bit division (review div instruction documentation)

Lab No lab this week (midterm)