1 Homework / Exam Reading –PAL, pp 216-227 Homework –mp2 due before class number 12 Exam #1 –Class 13 (three sessions from today) –Open book / Open notes.

Slides:



Advertisements
Similar presentations
COMP 2003: Assembly Language and Digital Logic
Advertisements

NEG Instruction Change operand content into two’s complement (negative value) and stored back into its operand mov bl, b neg bl; bl = mov.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 2 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
ACOE2511 Assembly Language Arithmetic and Logic Instructions.
Computer Organization & Assembly Language
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, MUL Instruction The MUL (unsigned multiply) instruction.
Introduction to Assembly Here we have a brief introduction to IBM PC Assembly Language –CISC instruction set –Special purpose register set –8 and 16 bit.
Gursharan Singh Tatla 21-Nov-20101www.eazynotes.com.
Inline Assembly Section 1: Recitation 7. In the early days of computing, most programs were written in assembly code. –Unmanageable because No type checking,
© 2006 Pearson Education, Upper Saddle River, NJ All Rights Reserved.Brey: The Intel Microprocessors, 7e Chapter 5 Arithmetic and Logic Instructions.
Shift and Rotate Instructions
1 Homework Reading –Professional Assembly Language, pp 17-32, Continue work on mp1 –Questions? Lab with your assigned section this week.
1 Homework Reading –PAL, pp Machine Projects –MP2 due at start of Class 12 Labs –Continue labs with your assigned section.
Assembly Language – Lab 5
Ch. 7 Logic, Shift and Rotate instr.
Khaled A. Al-Utaibi  Introduction  Arithmetic Instructions  Basic Logical Instructions  Shift Instructions  Rotate Instructions.
Multiplication and Division Instructions & the 0Ah function.
The x86 Architecture Lecture 15 Fri, Mar 4, 2005.
Microprocessors Monday, Apr. 13 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.
Assembly תרגול 5 תכנות באסמבלי. Assembly vs. Higher level languages There are NO variables’ type definitions.  All kinds of data are stored in the same.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University Machine Level Programming.
Arithmetic Flags and Instructions
Assembly Language for Intel-Based Computers Chapter 7: Integer Arithmetic (c) Pearson Education, All rights reserved. You may modify and copy.
1 Logic, Shift, and Rotate Instructions Read Sections 6.2, 7.2 and 7.3 of textbook.
Assembly Language. Symbol Table Variables.DATA var DW 0 sum DD 0 array TIMES 10 DW 0 message DB ’ Welcome ’,0 char1 DB ? Symbol Table Name Offset var.
Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2013/2014.
Functions/Methods in Assembly
Assembly 05. Outline Bit mapping Boolean logic (review) Bitwise logic Bit masking Bit shifting Lookup table 1.
1 Assembly Language Part 2 Professor Jennifer Rexford COS 217.
ICS 312 SET 10 Multiplication & Division & input using function 0Ah.
The Assemble, Unassemble commands of the debugger: U Command for converting machine code language source Equivalent machine code instructions Equivalent.
Chapter 7 Bit Manipulation. 7.1 Logical Operations.
Microprocessor & Assembly Language Arithmetic and logical Instructions.
Calling Procedures C calling conventions. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
Computer and Information Sciences College / Computer Science Department CS 206 D Computer Organization and Assembly Language.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 251 Introduction to Computer Organization.
Multiplication and Division instructions Dr.Hadi AL Saadi.
Precept 7: Introduction to IA-32 Assembly Language Programming
Computer Architecture and Assembly Language
Assembly Language Programming IV: shift, struct, recursion
Homework Reading Lab with your assigned section starts next week
Credits and Disclaimers
Data Transfers, Addressing, and Arithmetic
Homework Reading Labs PAL, pp
Microprocessor Systems Design I
Aaron Miller David Cohen Spring 2011
Chapter 3 Bit Operations
Basic Assembly Language
Assembly IA-32.
Assembly Language Programming Part 2
# include < stdio.h > v oid main(void) { long NUM1[5]; long SUM; long N; NUM1[0] = 17; NUM1[1] = 3; NUM1[2] =  51; NUM1[3] = 242; NUM1[4] = 113; SUM =
Homework Reading Continue work on mp1
Chapter 4: Instructions
Assembly Language: IA-32 Instructions
Microprocessor and Assembly Language
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Shift & Rotate Instructions)
Homework Reading Machine Projects Labs PAL, pp
Multiplication and Division Instructions
Shift & Rotate Instructions)
Assembly Language for Intel-Based Computers, 4th Edition
Assembly Language for Intel-Based Computers, 5th Edition
Computer Architecture CST 250
Shift, Multiply, and Divide
Computer Architecture and System Programming Laboratory
X86 Assembly Review.
Chapter 5 Arithmetic and Logic Instructions
Credits and Disclaimers
Computer Architecture and System Programming Laboratory
CS-401 Computer Architecture & Assembly Language Programming
Presentation transcript:

1 Homework / Exam Reading –PAL, pp Homework –mp2 due before class number 12 Exam #1 –Class 13 (three sessions from today) –Open book / Open notes –Practice exam posted on web site now

2 Using C Structs in Assembly Code How do we access a C structure such as: #define NAMELEN 20 struct teststruct { int x, int y; char name[NAMELEN]; }t; t.x = 2; t.y = 5; strncpy(t.name, “wilson”, NAMELEN); trystruct(&t); /* pass to asm via pointer*/

3 Using C Structs in Assembly Code Assembly code would look like: movl4(%esp),%edx# ptr to t movl(%edx),%eax# x itself movl4(%edx),%ebx# y itself movb8(%edx),%cl# 1st string char 0xfffffc &t 0xfffff80xfffff4 %esp Return %eip t.x‘w’ ‘i’ ‘l’ ‘s’ ‘o’ ‘n’ ‘\0’t.y 0x0200e00x0200e40x0200e Stack struct teststruct

4 Using C Structs in Assembly Code However, we would normally have a pointer to string: #define NAMELEN 20 char array [NAMELEN]; struct teststruct { int x, int y; char *name; }t; t.x = 2; t.y = 5; t.name = array; strncpy(array, “wilson”, NAMELEN); trystruct(&t); /* pass to asm via pointer*/

5 Using C Structs in Assembly Code Assembly code would look like: movl4(%esp),%edx# ptr to t movl(%edx),%eax# x itself movl4(%edx),%ebx# y itself movl8(%edx),%edx# ptr to string movb(%edx),%cl # first string char 0xfffffc &t 0xfffff80xfffff4 %esp Return %eip t.x ‘w’ ‘i’ ‘l’ ‘s’ ‘o’ ‘n’ ‘\0’ t.y 0x0200e00x0200e40x0200e Stack struct teststruct t.name 0x

Introduction to Shift Instructions We can shift the bits in a byte, word, or long word by a variable number of positions These are the machine level instructions used to implement the C language operators > –SAL / SHL are the left shift instructions for signed or unsigned data (arithmetic or logical left shift) –SAR is the right shift instruction for signed data (arithmetic right shift) –SHR is the right shift instruction for unsigned data (logical right shift) 6

Introduction to Shift Instructions The SAL / SHL Instruction (Signed / Unsigned) The SAR Instruction (Signed) The SHR Instruction (Unsigned) 7 0 CF 0

Introduction to Shift Instructions The target of the shifting can be a register or memory location (byte, word, or long word) The count for the number of bits to shift can be specified with immediate data (constant) or the %cl register (variable) Examples: sall $4, %eax # logical left shift of %eax by 4 bits sarb %cl, label # arithmetic right shift of memory byte # by a variable value stored in the %cl 8

Introduction to Shift Instructions Multiplication by 2 N can be done via left shift sall $4, %eax # %eax times 2 4 Can combine left shifts and addition Division by 2 N can be done via right shift sarb %cl, label # memory byte / 2 %cl Can combine right shifts and subtraction 9

10 Introduction to Multiply and Divide Unsigned Multiply and Divide –mul –div Signed Multiply and Divide –imul –idiv We won’t do much with these because of the complexity involved - especially for divide

11 Introduction to Multiply and Divide Multiply always operates with %al, %ax, or %eax Result needs more bits than either operand Syntax: mulb %bl %ax  %al * %bl mulw %bx %dx, %ax  %ax * %bx mull %ebx %edx, %eax  %eax * %ebx

12 Introduction to Multiply and Divide Register Pictures (Byte) (Word) %bl %ax * %bx %dx, %ax *

13 Example – For/While Loop and mul C code for n = 5! (done as a for loop) unsigned int i, n; n = 1; for (i = 1; i <= 5; i++) n *= i; C code for n = 5! (done as a while loop) unsigned int i, n; n = i = 1; while (i <= 5) n *= i++;

14 Example – For/While Loop and mul Assembly code for n = 5! (byte * byte = word) movb $1, %bl # i = 1 movb %bl, %al # n = i = 1 loop:cmpb $5, %bl # while (%bl <= 5) jaexit # %bl > 5 now mulb%bl # %ax = %al * %bl incb%bl # incr %bl jmploop # and loop exit: # 5! in %ax Note: No difference between for and while in assy

15 Example – For/While Loop and mul Assembly code for n = 5! (word * word = long) movw $1, %bx # i = 1 movw %bx, %ax # n = i = 1 loop:cmpw $5, %bx # while (%bx <= 5) jaexit # %bx > 5 now mulw%bx # %ax = %ax * %bx # %dx = 0 now incw%bx # incr %bx jmploop # and loop exit: # 5! in %eax

16 Recursive Factorial Main program to call recursive factorial subr.text pushl $5 call factorial addl $4, %esp ret

17 Recursive Factorial factorial: # works up to 16 bit results movl 4(%esp), %eax cmpl $1, %eax jna return decl %eax pushl %eax call factorial addl $4, %esp movw 4(%esp), %bx mulw %bx # 16 lsbs go to %ax return: # ignore msbs in %dx ret.end

18 Recursive Factorial Stack Operations (while calling) Stack Operations (while returning) %esp at main value 5 %eip (main) 1 st Call value 4 %eip (fact) value 3 %eip (fact) value 2 %eip (fact) value 1 %eip (fact) Decr & 2 nd Call Decr & 3 rd Call Decr & 4 th Call Decr & 5 th Call arg ==1 so 1 st Return Multiply & 2 nd Return Multiply & 3 rd Return Multiply & 4 th Return Multiply & 5 th Return %eax = 1%eax = 2%eax = 6%eax = 24%eax = 120