Computer Architecture and Assembly Language
Total Page:16
File Type:pdf, Size:1020Kb
Computer Architecture and System Programming Laboratory TA Session 1 Data Representation Basics • bit – basic information unit: (1/0) 3 2 1 0 • nibble – sequence of 4 bits: 7 6 5 4 3 2 1 0 • byte – sequence of 8 bits: • word – sequence of 2 (or 4) bytes 16 bit word 32 bit word byte byte byte byte byte byte main memory 64 • address space: 2 -1 0 to 264-1= 0xFFFFFFFFFFFFFFFF … 264 bytes = 24∙260 bytes 2K-1 = 24∙230 Gb = 16 exabytes address … space 1 physical memory 0 Registers file - CPU unit which contains registers general purpose registers r8 – r15 r8d r8w r8b Extended High Low flag register RFLAGS Instruction Pointer register RIP - contains address of the next instruction that is going to be executed (at run time) - changed by unconditional jump, conditional jump, procedure call, and return instructions Note that the list of registers above is partial. The full list can be found here. Assembly Language Program • consists of a series of processor instructions, assembler directives, comments, and data • translated by assembler into machine language instructions (binary code) that can be loaded into memory and executed • NASM - Netwide Assembler - is an assembler and for x86 architecture Example: assembly code: MOV AL, 0x61 ; load AL with 97 decimal (61 hex) machine code (in binary): 10110000 01100001 1011 a binary 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' 01100001 a binary representation of 97 decimal (97d = (int)(97/16)*10 + (97%16 converted to hex digit) = 61h) Basic structure of an assembly instruction label: (pseudo) opcode operands ; comment optional fields either required or forbidden by instruction • each instruction has its address in memory • we mark an instruction with a label to refer it in the code RAM • (non-local) labels have to be unique • an instruction that follows a label can be at the same / next line • colon is optional Examples: … mov ax, 2 ; moves constant 2 to the register ax bytes buffer: resb 64 ; reserves 64 bytes 64 buffer 2048 mov buffer, 2 mov 2048, 2 mov [buffer], 2 mov [2048], 2 mov byte [buffer], 2 mov byte [2048], 2 ☺ move to one starting from buffer numeric byte address in memory value 2 - backslash (\) : 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 Instruction Arguments 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 Examples: mov ax, 2 mov [buffer], ax target operand source operand target operand source operand register immediate memory location register ! Note that we cannot have both source and target operands specified as memory locations. mov [var1],[var2] MOV - Move Instruction – copies source to target mov reg8/mem8(16,32,64),reg8/imm8(16,32,64) (copies content of register / immediate (source) to register / memory location (target)) mov reg8(16,32,64),reg8/mem8(16,32,64) (copies content of register / memory location (source) to register (target)) operands have to be of the same size Examples: mov eax, 0x2334AAFF mov [buffer], ax mov word [var], 2 reg32 imm32 mem16 reg16 mem16 imm16 Note that NASM doesn’t remember the size 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 Instruction carry means the digit with <instruction> reg8/mem8(16,32,64),reg8/imm8(16,32,64) promote to higher powers, (source - register / immediate, - register / memory location) whereas borrow means the digit we demote to lower powers <instruction> reg8(16,32,64),reg8/mem8(16,32,64) (source - register / immediate, - register / memory location) ADD - add integers without carry SUB - subtract integers without carry Example: Example: add AX, BX ;(AX gets a value of AX+BX) sub AX, BX ;(AX gets a value of AX-BX) ADC - add integers with carry SBB - subtract with borrow (value of Carry Flag) (value of Carry Flag) Example: Example: adc AX, BX ;(AX gets a value of AX+BX+CF) sbb AX, BX ;(AX gets a value of AX-BX-CF) Basic Arithmetical Instruction <instruction> reg8/mem8(16,32,64) (source / - register / memory location) INC - increment integer Example: inc AX ;(AX gets a value of AX+1) DEC - decrement integer Example: dec byte [buffer] ;(first byte of [buffer] gets a value of first byte of [buffer] -1) Basic Logical Instructions <instruction> reg8/mem8(16,32,64) (source / - register / memory location) NOT – one’s complement negation – inverts all the bits Example: mov al, 11111110b not al ;(AL gets a value of 00000001b) ;(11111110b + 00000001b = 11111111b) NEG – two’s complement negation – inverts all the bits, and adds 1 Example: mov al, 11111110b neg al ;(AL gets a value of not(11111110b)+1=00000001b+1=00000010b) ;(11111110b + 00000010b = 100000000b = 0) Basic Logical Instructions <instruction> reg8/mem8(16,32,64),reg8/imm8(16,32,64) (source - register / immediate, - register / memory location) <instruction> reg8(16,32,64),reg8/mem8(16,32,64) (source - register / immediate, - register / memory location) OR – bitwise or – bit at index i of the gets ‘1’ if bit at index i of source or are ‘1’; otherwise ‘0’ Example: mov al, 11111100 b mov bl, 00000010b or AL, BL ;(AL gets a value 11111110b) AND– bitwise and – bit at index i of the gets ‘1’ if bits at index i of both source and are ‘1’; otherwise ‘0’ Example: or AL, BL ;(with same values of AL and BL as in previous example, AL gets a value 0) CMP – Compare Instruction – compares integers CMP performs a ‘mental’ subtraction - affects the flags as if the subtraction had taken place, but does not store the result of the subtraction. cmp reg8/mem8(16,32,64),reg8/imm8(16,32,64) (source - register / immediate, - register / memory location) cmp reg8(16,32,64),reg8/mem8(16,32,64) (source - register / immediate, - register / memory location) Examples: mov al, 11111100 mov al, 11111100b b mov bl, 11111100 mov bl, 00000010b b cmp al, bl ;(ZF (zero flag) gets a value 0) cmp al, bl ;(ZF (zero flag) gets a value 1) JMP – unconditional jump jmp label JMP tells the processor that the next instruction to be executed is located at the label that is given as part of jmp instruction. Example: this is infinite loop ! mov eax, 1 inc_again: this instruction is never inc eax reached from this code jmp inc_again mov ebx, eax rip register gets inc_again label (address) J<Condition> – conditional jump j<cond> label • 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 mov eax, 1 Example: inc_again: inc eax mov eax, 1 cmp eax, 10 inc_again: jne inc_again ; if eax ! = 10, go back to loop inc eax cmp eax, 10 je end_of_loop ; if eax = = 10, jump to end_of_loop jmp inc_again ; go back to loop end_of_loop: Instruction Description Flags JO Jump if overflow OF = 1 JNO Jump if not overflow OF = 0 JS Jump if sign SF = 1 JNS Jump if not sign SF = 0 JE Jump if equal ZF = 1 JZ Jump if zero JNE Jump if not equal ZF = 0 JNZ Jump if not zero JB Jump if below CF = 1 JNAE Jump if not above or equal JC Jump if carry JNB Jump if not below CF = 0 JAE Jump if above or equal JNC Jump if not carry JBE Jump if below or equal CF = 1 or ZF = 1 JNA Jump if not above JA Jump if above CF = 0 and ZF = 0 JNBE Jump if not below or equal JL Jump if less SF <> OF JNGE Jump if not greater or equal JGE Jump if greater or equal SF = OF JNL Jump if not less JLE Jump if less or equal ZF = 1 or SF <> OF JNG Jump if not greater JG Jump if greater ZF = 0 and SF = OF JNLE Jump if not less or equal JP Jump if parity PF = 1 JPE Jump if parity even JNP Jump if not parity PF = 0 JPO Jump if parity odd JCXZ Jump if CX register is 0 CX = 0 JECXZ Jump if ECX register is 0 ECX = 0 Note that the list above is partial. The full list can be found here. d<size> – declare initialized data d<size> initial value Pseudo-instruction <size> filed <size> value DB byte 1 byte DW word 2 bytes DD double word 4 bytes DQ quadword 8 bytes DT tenbyte 10 bytes DDQ double quadword 16 bytes DO octoword 16 bytes 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’,10 ; string constant var: dw 0x1234 ; 0x34 0x12 var: dw ‘A' ; 0x41 0x00 – complete to word var: dw ‘ABC' ; 0x41 0x42 0x43 0x00 – complete to word var: dd 0x12345678 ; 0x78 0x56 0x34 0x12 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: • Convert lower case to upper case. • Convert ‘(’ into ‘<’. • Convert ‘)’ into ‘>’. • Count the number of the non-letter characters, that is ANY character that is not 'a'->'z' or 'A'->'Z‘, including ‘\n’ character The function shall return the number of the letter characters in the string.