Practical Session 1 Computer Architecture and Assembly Language.

Slides:



Advertisements
Similar presentations
Jump Condition.
Advertisements

Copyright © 2000, Daniel W. Lewis. All Rights Reserved. CHAPTER 5 MIXING C AND ASSEMBLY.
ACOE2511 Assembly Language Arithmetic and Logic Instructions.
Assembly Language :CSC 225 (Lec#4: Flag Register and Conditional Statements) By Dr. Syed Noman.
The CPU Revision Typical machine code instructions Using op-codes and operands Symbolic addressing. Conditional and unconditional branches.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You may modify.
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 2006.
Assembly Language for Intel-Based Computers
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.
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.
Flow Control Instructions
INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
Practical Session 4. Labels Definition - advanced label: (pseudo) instruction operands ; comment valid characters in labels are: letters, numbers, _,
Computer Architecture and Assembly Language Practical session outline Introduction 80x86 assembly –Data storage –The registers –Flags –Instructions Assignment.
The x86 Architecture Lecture 15 Fri, Mar 4, 2005.
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#5) By Dr. Syed Noman.
Computer Architecture and Assembly Language. Byte structure : a byte has 8 bits MSB (Most Significant Bit) LSB (Least Significant Bit) Data Representation.
Dr. José M. Reyes Álamo 1.  Review: ◦ of Comparisons ◦ of Set on Condition  Statement Labels  Unconditional Jumps  Conditional Jumps.
Assembly Language for Intel-Based Computers, 6 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You may modify.
1 ICS 51 Introductory Computer Organization Fall 2009.
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.
LEA instruction The LEA instruction can be used to get the offset address of a variable Example ORG 100h MOV AL, VAR1 ; check value of VAR1 by moving it.
October 1, 2003Serguei A. Mokhov, 1 SOEN228, Winter 2003 Revision 1.2 Date: October 25, 2003.
Practical Session 2. Flags Register (Status Register) A flag is a single bit of information whose meaning is independent from any other bit Each flag.
COMP 2003: Assembly Language and Digital Logic Chapter 2: Flags and Instructions Notes by Neil Dickson.
Assembly 06. Outline cmp (review) Jump commands test mnemonic bt mnemonic Addressing 1.
Computer Architecture and Assembly Language
Jumps, Loops and Branching. Unconditional Jumps Transfer the control flow of the program to a specified instruction, other than the next instruction in.
Assembly Language Wei Gao. Assembler language Instructions.
Practical Session 2 Computer Architecture and Assembly Language.
Computer Architecture and Assembly Language
Practical Session 3.
CSC 221 Computer Organization and Assembly Language
Computer Architecture and Assembly Language
Assembly language.
Data Transfers, Addressing, and Arithmetic
Computer Architecture and Assembly Language
Homework Reading Labs PAL, pp
Practical Session 2.
Today we are going to discuss about,
Instruksi Set Prosesor 8088
Microprocessor and Assembly Language
EE3541 Introduction to Microprocessors
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Assembly IA-32.
INSTRUCTION SET.
More on logical instruction and
Assembly Language Programming Part 2
CS 301 Fall 2002 Assembly Instructions
BIC 10503: COMPUTER ARCHITECTURE
Fundamentals of Computer Organisation & Architecture
Practical Session 4.
Shift & Rotate Instructions)
Program Logic and Control
Program Logic and Control
Homework Reading Machine Projects Labs PAL, pp
Computer Architecture and System Programming Laboratory
X86 Assembly Review.
Computer Architecture and System Programming Laboratory
Chapter 7 –Program Logic and Control
Chapter 8: Instruction Set 8086 CPU Architecture
Chapter 7 –Program Logic and Control
Computer Architecture and Assembly Language
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Presentation transcript:

Practical Session 1 Computer Architecture and Assembly Language

Byte – sequence of 8 bits: MSB (Most Significant Bit) LSB (Least Significant Bit) Bit – basic information unit: (1/0) Word – a sequence of bits addressed as a single entity by the computer byte 16 bit word Main Memory is an array of bytes, addressed by 0 to =0xFFFFFFFF 2 32 bytes = 4∙2 10∙3 bytes = 4 G bytes … … K address space physical memory Data Representation Basics

Register file - CPU unit which contains (32 bit) registers. EAX, EBX, ECX, EDX general purpose registers EAX, EBX, ECX, EDX (Accumulator, Base, Counter, Data) ESP, EBP, ESI, EDI index registers ESP, EBP, ESI, EDI (Stack pointer - contains the address of last used dword in the stack, Base pointer, Source index, Destination Index) EFLAGS flag register / status register EFLAGS EIP / EPC Instruction Pointer / Program Counter EIP / EPC - at run time - - contains address (offset) of the next instruction that is going to be executed (at run time) - changed by unconditional jump, conditional jump, procedure call, and return instructions Registers Registers Note that the list of registers above is partial. The full list can be found here.here Register file High byteLow byteExtended 16-bit register

Assembly Language Program consists of a series of processor instructions, meta-statements, comments, and data translated by assembler into machine language instructions (binary code) that can be loaded into memory and executed NASM - Netwide Assembler - is assembler and for x86 architecture Example: assembly code assembly code: MOV AL, 61h ; load AL with 97 decimal (61 hex) binary code binary code: a binary code (opcode) of instruction 'MOV' 0 specifies if data is byte (‘0’) or full size 16/32 bits (‘1’) 000 a binary identifier for a register 'AL' a binary representation of 97 decimal (97 d = (int)(97/16)*10 + (97%16 converted to hex digit) = 61 h )

label: (pseudo) instruction operands ; comment optional fields either required or forbidden by an instruction Notes: - backslash (\) uses as the line continuation character: if a line ends with backslash, the next line is considered to be a part of the backslash-ended line - no restrictions on white space within a line - a colon after a label is optional Examples: mov ax, 2 ; moves constant 2 to the register ax buffer: resb 64 ; reserves 64 bytes Basic Assembly Instruction Structure Basic Assembly Instruction Structure

A typical instruction has 2 operands - target operand (left) - source operand (right) 3 kinds of operands exists - immediate : value - register : AX,EBP,DL etc. - memory location : variable or pointer Instruction Arguments mov [var1],[var2] ! Note that x86 processor does not allow both operands be memory locations. Examples: mov ax, 2 mov [buffer], ax source operandtarget operand immediateregister source operandtarget operand registermemory location

mov reg8/mem8, reg8/imm8 mov reg8/mem8 (16,32), reg8/imm8 (16,32) ( copies content of register / immediate (source) to register / memory location (destination) ) mov reg8, reg8/mem8 mov reg8 (16,32), reg8/mem8 (16,32) ( copies content of register / memory location (source) to register (destination) ) MOV - Move Instruction – copies source to destination Note that NASM doesn ’ t remember the types of variables you declare. It will deliberately remember nothing about the symbol var except where it begins, and so you must explicitly code mov word [var], 2. operands have to be of the same size Examples: word mov eax, 0x2334AAFF mov [buffer], ax mov word [var], 2 imm32reg32 reg16 mem16 imm16mem16

ADD ADD - add integers Example: add AX, BX ;(AX gets a value of AX+BX) ADC ADC - add integers with carry (value of Carry Flag) Example: adc AX, BX ;(AX gets a value of AX+BX+CF) Basic Arithmetical Instruction reg8/mem8, reg8/imm8 reg8/mem8 (16,32), reg8/imm8 (16,32) ( source - register / immediate, destination- register / memory location) reg8, reg8/mem8 reg8 (16,32), reg8/mem8 (16,32) ( source - register / immediate, destination - register / memory location) SUB - subtract integers Example: sub AX, BX ;(AX gets a value of AX-BX) SBB - subtract with borrow (value of Carry Flag) Example: sbb AX, BX ;(AX gets a value of AX-BX-CF )

Basic Arithmetical Instruction reg8/mem8 reg8/mem8 (16,32) ( source / destination - register / memory location) INC INC - increment integer Example: inc AX ;(AX gets a value of AX+1) DEC DEC - increment integer Example: dec byte [buffer] ;([buffer] gets a value of [buffer] -1)

Basic Logical Instructions reg8/mem8 reg8/mem8 (16,32) ( source / destination - register / memory location) NOT NOT – one’s complement negation – inverts all the bits Example: mov al, b not al ;(AL gets a value of b ) ;( b b = b ) NEG NEG – two’s complement negation – inverts all the bits, and adds 1 1 Example: mov al, b neg al ;(AL gets a value of not( b )+1= b +1= b ) ;( b b = b = 0)

Basic Logical Instructions OR OR – bitwise or – bit at index i of the destination gets ‘1’ if bit at index i of source or destination are ‘1’; otherwise ‘0’ Example: mov al, b mov bl, b or AL, BL ;(AL gets a value b ) reg8/mem8, reg8/imm8 reg8/mem8 (16,32), reg8/imm8 (16,32) ( source - register / immediate, destination- register / memory location) reg8, reg8/mem8 reg8 (16,32), reg8/mem8 (16,32) ( source - register / immediate, destination - register / memory location) AND AND – bitwise and – bit at index i of the destination gets ‘1’ if bits at index i of both source and destination are ‘1’; otherwise ‘0’ Example: or AL, BL ;(with same values of AL and BL as in previous example, AL gets a value )

cmp reg8/mem8, reg8/imm8 cmp reg8/mem8 (16,32), reg8/imm8 (16,32) ( source - register / immediate, destination- register / memory location) cmp reg8, reg8/mem8 cmp reg8 (16,32), reg8/mem8 (16,32) ( source - register / immediate, destination - register / memory location) CMP – Compare Instruction – compares integers Examples: mov al, b mov bl, b cmp al, bl ;(ZF (zero flag) gets a value 0) CMP performs a ‘mental’ subtraction - affects the flags as if the subtraction had taken place, but does not store the result of the subtraction. mov al, b mov bl, b cmp al, bl ;(ZF (zero flag) gets a value 1)

each instruction / data has its offset (address) if we want to refer to the specific instruction / data in the code, we should mark it with a label (non-local) labels have to be unique an instruction that follows a label can be at the same / next line colon is optional Label – specifies instruction’s offset (address) Examples: my_instruction my_instruction: add ax, ax ;(my_instruction is an address of ‘add ax, ax’ instruction) buffer buffer buffer: db 0 add byte [buffer], 2 ;([buffer] gets a value of [buffer]+2)

JMP tells the processor that the next instruction to be executed is located at the label that is given as part of jmp instruction. JMP – unconditional jump jmp label Example: jmp mov eax, 1 inc_again: inc eax jmp inc_again mov ebx, eax this is infinite loop ! this instruction is never reached from this code!

execution is transferred to the target instruction only if the specified condition is satisfied usually, the condition being tested is the result of the last arithmetic or logic operation J – conditional jump j label je jmp Example: read_char: … ; get a character into AL cmp al, ‘a’ ; compare the character to ‘a’ je a_received ; if value of al register equals to ‘a’, jump to a_received jmp read_char ; go back to read another a_received: …

InstructionDescriptionFlags JO Jump if overflowOF = 1 JNO Jump if not overflowOF = 0 JS Jump if signSF = 1 JNS Jump if not signSF = 0 JE JZ Jump if equal Jump if zero ZF = 1 JNE JNZ Jump if not equal Jump if not zero ZF = 0 JB JNAE JC Jump if below Jump if not above or equal Jump if carry CF = 1 JNB JAE JNC Jump if not below Jump if above or equal Jump if not carry CF = 0 JBE JNA Jump if below or equal Jump if not above CF = 1 or ZF = 1 JA JNBE Jump if above Jump if not below or equal CF = 0 and ZF = 0 JL JNGE Jump if less Jump if not greater or equal SF <> OF JGE JNL Jump if greater or equal Jump if not less SF = OF JLE JNG Jump if less or equal Jump if not greater ZF = 1 or SF <> OF JG JNLE Jump if greater Jump if not less or equal ZF = 0 and SF = OF JP JPE Jump if parity Jump if parity even PF = 1 JNP JPO Jump if not parity Jump if parity odd PF = 0 JCXZ JECXZ Jump if CX register is 0 Jump if ECX register is 0 CX = 0 ECX = 0 Jcc: Conditional Branch

D – declare initialized data d initial value Examples: var:db 0x55 ; define a variable ‘var’ of size byte, initialized by 0x55 var: db 0x55,0x56,0x57 ; three bytes in succession var: db 'a‘ ; character constant 0x61 (ascii code of ‘a’) var: db 'hello',13,10,'$‘ ; string constant var: dw 0x1234 ; 0x34 0x12 0x00 – complete to word var: dw 'a' ; 0x41 0x00 – complete to word var: dw 'ab‘ ; 0x41 0x42 0x00 – complete to word var: dw 'abc' ; 0x41 0x42 0x43 0x00 – complete to word var: dd 0x ; 0x78 0x56 0x34 0x12 value filedPseudo-instruction 1 bytebyteDB 2 byteswordDW 4 bytesdouble wordDD 8 bytesquadwordDQ 10 bytestenbyteDT 16 bytesdouble quadwordDDQ 16 bytesoctowordDO

Assignment 0 LetterLeet symbol A4 B8 C( E3 G6 H# I! L1 O0 S5 T7 Z2 You get a simple program that receives a string from a user. Then, this program calls to a function (that you should implement in assembly) that receives a string as an argument and does the following: 1.Convert the uppercase letters to their equivalent Leet symbol, according to the table below. 2.All other uppercase letters should be converted to lowercase letters 3.Return the number of letters converted to Leets in the input string Example: >Enter a string: HELLO WorlD! >Result string: #3110 world! >Number of letters converted to Leet: 5 The function returns the number of characters which aren’t uppercase or lowercase letter (the output should be just the number). The characters conversion should be in-place.

#include # define MAX_LEN 100 // Maximal line size extern int strToLeet (char*); int main(void) { char str_buf[MAX_LEN]; int str_len = 0; printf("Enter a string: "); fgets(str_buf, MAX_LEN, stdin); // Read user's command line string strToLeet str_len = strToLeet (str_buf); // Your assembly code function printf("\nResult string:%s\nNumber of letters converted to Leet: %d\n",str_buf,str_len); } main.c

section.data ; data section, read-write an: DD 0 ; this is a temporary var section.text ; our code is always in the.text section strToLeet global strToLeet ; makes the function appear in global scope extern printf ; tell linker that printf is defined elsewhere ; (not used in the program) strToLeet strToLeet: ; functions are defined as labels push ebp ; save Base Pointer (bp) original value mov ebp, esp ; use base pointer to access stack contents pushad ; push all variables onto stack mov ecx, dword [ebp+8]; get function argument ;;;;;;;;;;;;;;;; FUNCTION EFFECTIVE CODE STARTS HERE ;;;;;;;;;;;;;;;; movdword [an], 0; initialize answer label_here: ; Your code goes somewhere around here... incecx ; increment pointer cmpbyte [ecx], 0 ; check if byte pointed to is zero jnzlabel_here ; keep looping until it is null terminated ;;;;;;;;;;;;;;;; FUNCTION EFFECTIVE CODE ENDS HERE ;;;;;;;;;;;;;;;; popad ; restore all previously used registers mov eax,[an] ; return an (returned values are in eax) mov esp, ebp pop ebp retmyasm.s

To assemble a file, you issue a command of the form > nasm -f [-o ] [ -l listing] Example: > nasm -f elf myasm.s -o myelf.o It would create myelf.o file that has elf format (executable and linkable format). We use main.c file (that is written in C language) to start our program, and sometimes also for input / output from a user. So to compile main.c with our assembly file we should execute the following command: gcc –m32 main.c myelf.o -o myexe.out The -m32 option is being used to comply with 32- bit environment It would create executable file myexe.out. In order to run it you should write its name on the command line: > myexe.out Running NASM

How to run Linux from Window Go to Run the following executable Use “lvs.cs.bgu.ac.il” or “lace.cs.bgu.ac.il” host name and click ‘Open’ Use your Linux username and password to login lace server Go to  Choose any free Linux computer  Connect to the chosen computer by using “ssh –X cs302six1-4” (maybe you would be asked for your password again) cd (change directory) to your working directory

Ascii table