<<

3/18/2015

Outline • : CISC – Registers, Addressing – X86 ALP & Components of CPU Assembly Language Programming – NASM Simulator, GCC/C inline Assembly • Components of CPU A. Sahu – Register, Multiplexor, Decoder, IM/DM CSE, IIT Guwahati – , substractor, Varity of Adder Please be updated with – Multiplier : Serial, Parallel http://jatinga.iitg.ernet.in/~asahu/cs222/ – ALU and Floating Point

Flow of Our Course Understanding of Overall

Understanding of an existing architecture and Analysisy Understanding of CISC to RISC X86 Assembly Program

Assembly language program : MIPS and X86

Components of processor : Reg, Mux. ALU, Mem, Adder

Processor Design : Data Path and Control Path : Analysis and Improvement

X86 Compatibility • 8085 (8 ) == > 8086 (16 bit) == > i386 (32 31 15 7 0 bit) or ia32== > x86‐64 (64 bit) or ia64 • AX ‐ accumulator reg EAX AH AL • BX ‐ base address reg EBX BH BL • AX (AH+AL) : 16 bit ECX CH CL • CX ‐ count reg EDX DH DL • EAX 32 bit • DX ‐ data reg ESI SI (Source Idx ) EDI DI (Dest. Idx) • RAX 64 bit • SI ‐ source index reg EBP BP (Base Ptr ) • DI ‐ dest index reg ESP SP (Stack Ptr) • BP ‐ base pointer. EZ Z (Flag Reg) • SP ‐ stack pointer. ECS CS (Code Seg Reg) EDS DS (Data Seg Reg ) EES ES (Extra Seg Reg ) ESS SS (Stack Seg Reg)

EIP IP (Intr Ptr)

1 3/18/2015

Register Index and Ptr Register • General registers • ES:EDI EDI DI : Destination – EAX, EBX, ECX, EDX – Used for string, memory array copying and setting and – Acc, Base Ptr for Memory, Ctr and Interrupt, Data for far pointer addressing with ES • DS:ESI, EDI SI : Source index register • Segment registers – Used for string and memory array copying – CS, DS, ES FS GS, SS • SS: EBP EBP BP : SSktack Base pointer register – Code, Data, Extra (far,near,VideoMem), Stack – Holds the base address of the stack • Index and pointers • SS:ESP ESP SP : Stack pointer register – ESI EDI EBP EIP ESP – Holds the top address of the stack • Indicator • CS:EIP EIP IP : Index Pointer – Holds the offset of the next instruction, It can only be – EFLAGS read

Memory Model: Segment Definition • .model small – hellodat SEGMENT BYTE 'DATA' ;Define the data segment Most widely used memory model. dos_pr EQU 9 ;define a constant via EQU – The code must fit in 64k. strng DB 'Hello World',13,10,'$‘; Define char string – The data must fit in 64k. hellodat ENDS • .model medium – The code can exceed 64k. hellodat SEGMENT ;define a segment – The data must fit in 64k. dos_print EQU 9 ;define a constant strng DB 'Hello World',13,10,'$' ;Define char string • .model compact hellodat ENDS – The code must fit in 64k. – The data can exceed 64k. .data • .medium and .compact are opposites. dos_print EQU 9 ;define a constant strng DB 'Hello World',13,10,'$' ;Define char string

Data Allocation Directives • db : define byte dw: def. word (2 bytes) • Assemby code: Loop • dd: def double word (4) dq : def quad word (8) – Loop simply decreases CX and checks if CX != 0, if • equ : equate assign numeric expr to a name so, a Jump to the specified memory location .data MOV CX,100 _LABEL: INC AX db A 100 dup (?) ; define 100 bytes, with no initial LOOP _LABEL values for bytes – LOOPNZ : LOOPs when the zero flag is not set db “Hello” ; define 5 bytes, ASCII equivalent of “Hello”. MOV CX,10 dd PtrArray 4 dup (?) ;array[0..3] of dword _CMPLOOP:DEC AX maxint equ 32767 ; define maxint=32767 CMP AX,3 LOOPNE CMPLOOP count equ 10 * 20 ; calculate a value (200)

2 3/18/2015

• Arithmetic • Assemby code: Nested Loop: One CX register – ADD, SUB, MUL, DIV – ADD AX, 5 AX = 0003 Î AX = 0008 mov cx, 8 • Logic Loop1: push cx – AND, OR, XOR, NOT mov cx, 4 – AND CH, DL CH = 11111111 DL = 00000010 Î CH= Loop2: stmts 00000010 • loop Loop2 – SHL/SHR pop cx – SHL AL, 1 AL= 101101010 Î 01101010 ;(SHL by 1) stmts • Comparisons and jumps loop Loop1 – JMP, CMP, Jxx, CALL, RET

• Register MOV AX, BX ; AX Å BX • Immediate W = X + Y * Z MOV AX, 3CH ; AXÅ 3CH mov ax, y ;Must compute Y*Z first since • Diirect imul z ; multiplication has a higher MOV [2000], AX ; 0(DSx10h+2000)Å AX add ax, x ; precedence than . • Reg indirect mov w, ax MOV [BX], AX ; 0(DSx10h+BX)ÅAX

• Base+Indx MOV [BX+SI], AX ;0(DSx10h+BX+SI)ÅAX • Memory address written as • Reg Relative – SEGMENT:OFFSET – Dereference offset with square brackets CS:[C494] MOV [BX+4], AX ;0(DSx10h+BX+4)ÅAX • DS is implicit: [][1337] is same as DS:[][1337] • Base RlRelat ive + IdIndex MOV ARRAY[BX+SI], AX ;0(DSx10h+ARRAY+BX+SI)ÅAX • Scaled index MOV [BX+2 x SI], AX ; 0(DSx10h+BX x 2+SI) ÅAX

3 3/18/2015

• Input a single char from KBD and echo • Terminates a – Registers used: AH = 1, AL = the character inputted – Registers used: AH = 4CH, AL = binary return code. from keyboard. – Ex: MOV AH,4CH – Ex: MOV AH,1 INT 21H INT 21H • Outputs a string of data, terminated by a $ – Registers used: AH = 9, DX = the offset address of the data to be displayed. – Ex: MOV AH,09 MOV DX,OFFSET MESS1 INT 21H

.model small • putchar( ‘a‘ ) ; .stack 100h ; reserve 256 bytes of stack space .data mov dl, ‘a‘ ;dl = ‘a‘ .code main proc mov ah, 2h ;character output subprogram call print40Dots int 21h ; call ms‐dos output character mov ax,4c00h ; Halt for DOS routine (Exit Program) int 21h main endp • c = getchar() ; end main print40Dots proc near ; print 40H dots mov al, ' . ' mov ah, 1h ; keyboard input subprogram mov cx, 40 mov ah, 2h int 21h ; char input, char is stored in al PSLoop: int 21H mov c, al ; copy character from al to c loop PSLoop ret PrintSpaces endp

• MACRONAME MACRO {ARG} • Examples ADDITION MACRO X, Y, Z .model small SUM_OF_N proc near .data PUSH AX cmp bx, 00 N EQU X jz BX_O MOV AX,X PUSH AX .code push bx ADD AX,Y MOV AX,A1 main proc dec bx MOV Z,AX ADD AX,A2 mov bx, N call SUM_OF_N POP AX call SUM_OF_N pop bx MOV A3,AX mov ax,4c00H ENDM POP AX BX_O: add ax, bx int 21h ret • Call macro and expand main endp endp end main – ADDITION A1, A2, A3

4 3/18/2015

NASM Hello World NASM SECTION .data ; data section msg: db "Hello World",10 ; the string to print, 10=cr • The Netwide ASseMbler len: equ $‐msg ; "$" means "here" ; len is a value, not an address • Free BSD : Free Berkley Software Distribution SECTION .text ; code section • Linux/Unix Version global main ; make label available to linker • 16/32/64 bit X86 main: ; standard gcc entry point mov edx, len ; arg3, length of string to print • Inter operatable with Liux GCC compiler mov ecx, msg ; arg2, pointer to string mov ebx, 1 ; arg1, where to write, screen mov eax, 4 ; write sysout command to int 80 hex int 0x80 ; interrupt 80 hex, call kernel

mov ebx, 0 ; exit code, 0=normal mov eax, 1 ; exit command to kernel int 0x80 ; interrupt 80 hex, call kernel

NASM‐Hello World • Compilation and linking GCC inline assembly syntax Assemble:$nasm ‐f elf ‐l hello.lst hello.asm #include link: $gcc ‐o hello hello.o int main() { run: $./hello /* Add 10 and 20 and store result into register %eax Output: Hello World */ __asm__ ( "movl $10, %eax;" "movl $20, %ebx;" NASM Tutorial and hello World Example "addl %ebx, %eax;" Available in Course website ); http://jatinga.iitg.ernet.in/~asahu/cs222/ }

GCC inline assembly syntax Extended Assembly asm("assembly code"); we can also specify the operands. It allows us to O r specify the input registers, output registers __asm__ ("assembly code"); and a list of clobbered registers. Example asm ( "assembly code" asm("movl %ebx, %eax"); : output operands /* optional */ /* moves the ontents of ebx register to eax */ : input operands /* optional */ __asm__("movb %ch, (%ebx)"); : list of clobbered registers /* optional /* moves the byte from ch to the memory pointed by ebx */ */ );

5 3/18/2015

Extended Assembly Extended Assembly int arg1, arg2, add ; #include int main() { __asm__ ( "addl %%ebx, %%eax;" int arg1, arg2, add, : "=a" (add) printf( "Enter two integer numbers : " ); : "a" (arg1), "b" (arg2) scanf( "%d%d", &arg1, &arg2 ); /* Perform Addition*/ ); __asm__ ( "addl %%ebx, %%eax;" : "=a" (add) Here "add" is the output operand referred to by : "a" (arg1) , "b" (arg2) register eax. And arg1 and arg2 are input ); operands referred to by registers eax and ebx respectively. printf( "%d + %d = %d\n", arg1, arg2, add ); return 0 ; eax=arg1; ebx=arg2; eax=eax+ebx; add=eax; }

Flow of Our Course Other Examples and Tutorial Understanding of Overall Computer

GCC inline assembly tutorial and some Understanding of an existing processor architecture examples are Available in Course website and Analysisy http://jatinga.iitg.ernet.in/~asahu/cs222/ Understanding of CISC to RISC Assembly language program : MIPS and X86

Components of processor : Reg, Mux. ALU, Mem, Adder

Processor Design : Data Path and Control Path Processor Design : Analysis and Improvement

Division into Data path and Control

Components of CPU DATA PATH

control status signals signals CONTROLLER

6 3/18/2015

Building block types Building block types

Two types of functional units: Two types of functional units: • Elements that operate on data values • Elements that operate on data values (combinational) (combinational) – Output is function of current input – Output is function of current input – No memory – No memory • Elements that contain state (sequential) • Elements that contain state (sequential) – Output is function of current and previous inputs – Output is function of current and previous inputs – State = memory – State = memory

Combinational circuit examples Sequential circuit examples • Gates: and, or, nand, nor, xor, inverter • Flip‐ • Counters • Decoder • RiRegisters • Adder, , comparator • Register files • ALU • Memories • Array multipliers

Clocked vs. unclocked circuit Unclocked state elements

• Clocked state element C R – State changes only with clock edge Q Q • Unclocked state element _ _ – Sta te changes can occur with changes in other Q Q S inputs D falling edge

D

cycle time C rising edge Q

7 3/18/2015

Clocked State Elements Clock and timings

D D D Q D Q Q D D Set-up time Hold time latch latch _ _ C C Q Q

C C State State Combinational Element Element Logic D 1 2

C

Q Clock Cycle

Components for MIPS subset

• Register • Adder • ALU • Multiplexer • • Program memory • Data memory • Bit manipulation components

8