Computer Architecture and Assembly Language

Slides:



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

Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 2 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Department of Computer Science and Software Engineering
Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1.
Computer Organization And Assembly Language
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 2006.
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
ICS312 Set 3 Pentium Registers. Intel 8086 Family of Microprocessors All of the Intel chips from the 8086 to the latest pentium, have similar architectures.
Practical Session 1 Computer Architecture and Assembly Language.
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.
Computer Architecture and Assembly Language. Byte structure : a byte has 8 bits MSB (Most Significant Bit) LSB (Least Significant Bit) Data Representation.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 4 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
1 ICS 51 Introductory Computer Organization Fall 2009.
Microprocessors The ia32 User Instruction Set Jan 31st, 2002.
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.
Chapter 2 Parts of a Computer System. 2.1 PC Hardware: Memory.
Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2013/2014.
X86 Assembly Language We will be using the nasm assembler (other assemblers: MASM, as, gas)
Computer Organization & Assembly Language University of Sargodha, Lahore Campus Prepared by Ali Saeed.
Microprocessors CSE- 341 Dr. Jia Uddin Assistant Professor, CSE, BRAC University Dr. Jia Uddin, CSE, BRAC University.
Intel MP Organization. Registers - storage locations found inside the processor for temporary storage of data 1- Data Registers (16-bit) AX, BX, CX, DX.
Precept 7: Introduction to IA-32 Assembly Language Programming
Computer Architecture and Assembly Language
Practical Session 3.
Stack Operations Dr. Hadi AL Saadi.
Assembly language programming
Computer Architecture and Assembly Language
Assembly language.
Instruction set Architecture
Introduction to assembly programmıng language
Format of Assembly language
Data Transfers, Addressing, and Arithmetic
Computer Architecture and Assembly Language
Computer Architecture and Assembly Language
Aaron Miller David Cohen Spring 2011
Microprocessor and Assembly Language
Chapter 4 Data Movement Instructions
Microprocessor and Assembly Language
Basic Microprocessor Architecture
Assembly IA-32.
Assembly Language Programming Part 2
Defining Types of data expression Dn [name] expression Dn [name]
Symbolic Instruction and Addressing
Introduction to Assembly Language
BIC 10503: COMPUTER ARCHITECTURE
Data Addressing Modes • MOV AX,BX; This instruction transfers the word contents of the source-register(BX) into the destination register(AX). • The source.
8086 Registers Module M14.2 Sections 9.2, 10.1.
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Symbolic Instruction and Addressing
Practical Session 4.
Symbolic Instruction and Addressing
Computer Architecture CST 250
Computer Architecture and System Programming Laboratory
X86 Assembly Review.
Computer Architecture and System Programming Laboratory
Chapter 6 –Symbolic Instruction and Addressing
CSC 497/583 Advanced Topics in Computer Security
Chapter 8: Instruction Set 8086 CPU Architecture
Computer Architecture and Assembly Language
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Presentation transcript:

Computer Architecture and Assembly Language

Byte structure: 7 6 5 4 3 2 1 a byte has 8 bits MSB (most significant bit) LSB (least significant bit)

Registers: CPU contains a unit called “Register file”. This unit contains the registers of the following types: 1. 8-bit general registers: AL, BL, CL, DL, AH, BH, CH, DH 2. 16- bit general registers: AX, BX, CX, DX, SP, BP, SI, Dl 3. 32-bit general registers: EAX, EBX, ECX, EDX, ESP, EBP, ESI, EDI (Accumulator, Base, Counter, Data, Stack pointer, Base pointer, Source index, Destination Index) 4. Segment registers: ES, CS ,SS, DS, FS, GS 5. instruction pointer: EIP Note: the registers above are a partial list. There are more registers.

XH XL EIP - instruction pointer: contains offset (address) of the next instruction that is going to be executed. Exists only during run time. The software change it by performing unconditional jump, conditional jump, procedure call, return. AX,BX,CX,DX - 16-bit general registers: contains two 8-bit registers: Example: AH,AL (for AX) EAX - 32-bit general purpose register: lower 16 bits are AX. segment registers: we use a flat memory model – 32bit 4GB address space, without segments. So for this course you can ignore segment registers. ESP - stack pointer: contains the next free address on a stack. high byte low byte XH XL

Basic assembly instructions: Each NASM standard source line contains a combination of the 4 fields: label: (pseudo) instruction operands ; comment Either required or forbidden by an instruction optional fields Notes: 1. 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. 2. no restrictions on white space within a line. 3. a colon after a label is optional. Examples: 1. mov ax, 2 ; moves constant 2 to the register ax 2. buffer: resb 64 ; reserves 64 bytes

Instruction arguments A typical instruction has 2 operands. The left operand is the target operand, while the right operand is the source operand 3 kinds of operands exists: Immediate, i.e. a value Register, such as AX,EBP,DL Memory location; a variable or a pointer. One should notice that the x86 processor does not allow both operands be memory locations. mov [var1],[var2]

Move instructions: MOV – move data mov r/m8,reg8 (copies content of 8-bit register (source) to 8-bit register or 8-bit memory unit (destination) ) mov reg32,imm32 (copies content of 32-bit immediate (constant) to 32-bit register) - In all forms of the MOV instruction, the two operands are the same size Examples: mov EAX, 0x2334AAFF mov [buffer], ax Note: 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.

Basic arithmetical instructions: ADD: add integers add r/m16,imm16 (adds its two operands together, and leaves the result in its destination (first) operand) Examples: add AX, BX ADC: add with carry adc r/m16,imm8 (adds its two operands together, plus the value of the carry flag, and leaves the result in its destination (first) operand) Examples: add AX, BX (AX gets a value of AX+BX+CF)

Basic arithmetical instructions (Cont.): SUB: subtract integers sub reg16,r/m16 (subtracts its second operand from its first, and leaves the result in its destination (first) operand) Examples: sub AX, BX SBB: subtract with borrow sbb r/m16,imm8 (subtracts its second operand, plus the value of the carry flag, from its first, and leaves the result in its destination (first) operand) Examples: sbb AX, BX (AX gets a value of AX-BX-CF)

Basic arithmetical instructions (Cont.): INC: increment integer inc r/m16 (adds 1 to its operand) * does not affect the carry flag; affects all the other flags according to the result Examples: inc AX DEC: decrement integer dec reg16 (subtracts 1 from its operand) * does not affect the carry flag; affects all the other flags according to the result Examples: dec byte [buffer]

Basic logical instructions: NEG, NOT: two's and one's complement neg r/m16 (replaces the contents of its operand by the two's complement negation - invert all the bits, and then add one) not r/m16 (performs one's complement negation- inverts all the bits) Examples: neg AL (if AL = (11111110), it becomes (00000010)) not AL (if AL = (11111110), it becomes (00000001))

Basic logical instructions (Cont.): OR: bitwise or or r/m32,imm32 (each bit of the result is 1 if and only if at least one of the corresponding bits of the two inputs was 1; stores the result in the destination (first) operand) Example: or AL, BL (if AL = (11111100), BL= (00000010) => AL would be (11111110)) AND: bitwise and and r/m32,imm32 (each bit of the result is 1 if and only if the corresponding bits of the two inputs were both 1; stores the result in the destination (first) operand) Example: and AL, BL (if AL = (11111100), BL= (11000010) => AL would be (11000000))

Compare instruction: CMP: compare integers cmp r/m32,imm8 (performs a ‘mental’ subtraction of its second operand from its first operand, and affects the flags as if the subtraction had taken place, but does not store the result of the subtraction anywhere) Example: cmp AL, BL (if AL = (11111100), BL= (00000010) => ZF would be 0) (if AL = (11111100), BL= (11111100) => ZF would be 1)

Labels definition (basic): Each instruction of the code has its offset (address from the beginning of the address space). If we want to refer to the specific instruction in the code, we should mark it with a label: my_instruction: add ax, ax … - label can be with or without colon - an instruction that follows it can be at the same or the next line - a code can’t contain two different non-local (as above) labels with the same name

Unconditional Jump: JMP: jump to instruction Usually it takes the form: jmp label *see section B.4.130 JMP in the nasm manual for full specification Tells the processor that the next instruction to be executed is located at the label that is given as part of the instruction. Example: mov eax,1 inc_again: ; In this case it is infinite loop! inc eax jmp inc_again mov ebx,eax ; Never reached from this code …

Conditional Jumps: JE,JG, JL, JGE, JLE, JNE: jump to instruction if condition is satisfied Usually it takes the form: j<cond> label *see section B.4.128 JMP in the nasm manual for full specification 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. Example: read_char: mov dl,0 . . . (code for reading a character into AL) cmp al, ‘a’ ; compare the character to ‘a’ je a_received ; if equal, jump to a_received inc cl ; otherwise, increment CL and jmp read_char ;go back to read another a_received: …

DB, DW, DD : declaring initialized data DB, DW, DD, DQ (DT, DDQ, and DO) are used to declare initialized data in the output file. They can be invoked in a wide range of ways: db 0x55 ; just the byte 0x55 db 0x55,0x56,0x57 ; three bytes in succession db 'a',0x55 ; character constants are OK db 'hello',13,10,'$‘ ; so are string constants dw 0x1234 ; 0x34 0x12 dw 'a' ; 0x41 0x00 (it's just a number) dw 'ab‘ ; 0x41 0x42 (character constant) dw 'abc' ; 0x41 0x42 0x43 0x00 (string) dd 0x12345678 ; 0x78 0x56 0x34 0x12 (dword)

Assignment 0 You get a simple program that receives a string from the user. Than, it calls to a function (that you’ll implement in assembly) that receives one string as an argument and should do the following: 1. Convert lower case to upper case. 2. Convert ‘(’ into ‘<’. 3. Convert ‘)’ into ‘>’. 4. Count the number of the non-letter characters. (Note: characters that are not letters,’(‘ or ‘)’ will remain as they are) e.g. “42: heLL() WorLd!" → “42: HELL<> WORLD!“ The function shall return the number of the letter characters in the string. The characters conversion should be in-place.

section .data ; data section, read-write an: DD 0 ; this is a temporary var section .text ; our code is always in the .text section global do_str ; makes the function appear in global scope extern printf ; tell linker that printf is defined elsewhere ; (not used in the program) do_str: ; 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 ;;;;;;;;;;;;;;;; mov dword [an], 0 ; initialize answer label_here: ; Your code goes somewhere around here... inc ecx ; increment pointer cmp byte [ecx], 0 ; check if byte pointed to is zero jnz label_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 dword ebp ret

Running NASM To assemble a file, you issue a command of the form > nasm -f <format> <filename> [-o <output>] [ -l listing] Example: > nasm -f elf mytry.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