Assembly Language Programming IV: shift, struct, recursion

Slides:



Advertisements
Similar presentations
Fabián E. Bustamante, Spring 2007 Machine-Level Programming II: Control Flow Today Condition codes Control flow structures Next time Procedures.
Advertisements

COMP 2003: Assembly Language and Digital Logic
NEG Instruction Change operand content into two’s complement (negative value) and stored back into its operand mov bl, b neg bl; bl = mov.
ACOE2511 Assembly Language Arithmetic and Logic Instructions.
Computer Organization & Assembly Language
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.
9-1 ECE 424 Design of Microprocessor-Based Systems Haibo Wang ECE Department Southern Illinois University Carbondale, IL x86 Instructions Part.
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.
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.
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.
Functions/Methods in Assembly
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.
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
Reading Condition Codes (Cont.)
Computer Architecture and Assembly Language
Homework Reading Lab with your assigned section starts next week
Credits and Disclaimers
Data Transfers, Addressing, and Arithmetic
Homework Reading Labs PAL, pp
Practical Session 2.
Microprocessor Systems Design I
Aaron Miller David Cohen Spring 2011
Chapter 3 Bit Operations
EE3541 Introduction to Microprocessors
Computer Architecture
Basic Assembly Language
Assembly IA-32.
Assembly Language Programming Part 2
Recitation 2 – 2/4/01 Outline Machine Model
Homework Reading Continue work on mp1
Chapter 3 Machine-Level Representation of Programs
Chapter 4: Instructions
Assembly Language: IA-32 Instructions
Microprocessor and Assembly Language
Machine-Level Programming 2 Control Flow
Machine-Level Programming III: Procedures Sept 18, 2001
Machine-Level Representation of Programs III
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
Chapter 3 Machine-Level Representation of Programs
X86 Assembly Review.
Chapter 5 Arithmetic and Logic Instructions
CSC 497/583 Advanced Topics in Computer Security
Credits and Disclaimers
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
CS-401 Computer Architecture & Assembly Language Programming
Presentation transcript:

Assembly Language Programming IV: shift, struct, recursion

Homework Reading PAL, pp 216-227

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*/

Using C Structs in Assembly Code Assembly code would look like: Movl 4(%esp),%edx # ptr to t: edx=0x0200e0 Movl (%edx),%eax # x itself: eax = 2 Movl 4(%edx),%ebx # y itself: ebx = 5 Movb 8(%edx),%cl # 1st string char: cl = ‘w’ struct teststruct t.x t.y ‘w’ ‘i’ ‘l’ ‘s’ ‘o’ ‘n’ ‘\0’ %esp Stack 0x0200e0 0x0200e4 0x0200e8 . . . . . . Return %eip 0x0200e0 0xfffff4 0xfffff8 0xfffffc

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*/

Using C Structs in Assembly Code Assembly code would look like: movl 4(%esp),%edx # ptr to t: edx = 0x0200e0 movl (%edx),%eax # x itself: eax = 2 movl 4(%edx),%ebx # y itself: ebx = 5 movl 8(%edx),%edx # ptr to string: edx = 0x20158 movb (%edx),%cl # first string char: cl = ‘w’ struct teststruct t.x t.y t.name %esp Stack 0x0200e0 0x0200e4 0x0200e8 . . . . . . &t Return %eip ‘w’ ‘i’ ‘l’ ‘s’ ‘o’ ‘n’ ‘\0’ 0xfffff4 0xfffff8 0xfffffc 0x020158 . . .

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 << and >> SAL / SHL are the left shift instructions for signed or unsigned data. SAL and SHL performs the same operations SAR is the right shift instruction for signed data (arithmetic right shift) SHR is the right shift instruction for unsigned data (logical right shift)

Introduction to Shift Instructions The SAL / SHL Instruction (Signed / Unsigned) The SAR Instruction (Signed or Arithmetic shift) The SHR Instruction (Unsigned or logical shift) CF CF CF

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

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

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

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

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

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) n = i = 1; while (i <= 5) n *= i++;

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) ja exit # %bl > 5 now mulb %bl # %ax = %al * %bl incb %bl # incr %bl jmp loop # and loop exit: # 5! in %ax Note: No difference between for and while in assy

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) ja exit # %bx > 5 now mulw %bx # %ax = %ax * %bx # %dx = 0 now incw %bx # incr %bx jmp loop # and loop exit: # 5! in %eax

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

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

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