130 likes | 155 Views
This discussion section covers topics such as data endianess, x86 assembly basics, registers, memory addressing, and various x86 assembly instructions.
E N D
CS33: Introduction to Computer OrganizationWeek 1 – Discussion Section Atefeh Sohrabizadeh atefehsz@cs.ucla.edu 10/4/19
Discussion Class Materials • You can find all the files we use in discussion class from: • http://vast.cs.ucla.edu/~atefehSZ/teaching/cs33.html
Agenda • Lab1: DataLab Sample Example • Big Endian vs. Little Endian • x86 Assembly: Basics
Endian • Big endian • Least significant byte has the highest address • E.g. : x = 0x1234567a, &x = 0x100 • Little endian • Least significant byte has the lowest address
Registers • Memory units that we can access them at relatively the speed of execution of instructions • A program brings the data it needs to registers and work on them • Each register can store a number of bits • The registers on a 64-bit addressable machine are 64-bits registers
x86-64 Registers • Has 16 general-purpose registers • Example • %rax: full 64-bits stored in the register rax • %eax: lower 32-bits stored in the register rax • %ax: lower 16-bits stored in the register rax • %al: lower 8-bits stored in the register rax • %ah: upper 8-bits of lower 16-bits stored in the register rax
x86 Assembly • General format • [operation] [source] [destination] • The suffix of an operation determines how much data to work on • b: work on a byte • w: work on a word (16 bits) • A word on a 64-bit machine is 64 bits, but when x86 was developing a word was 16 bits • l: work on a long/double word (32 bits) • q: work on a quad word (64 bits)
mov_ family • movb: move a byte • movw: move a word (16 bits) • movl: move a long word (32 bits) • movq: move a quad word (64 bits) • Examples: • movq %rax, %rbx# %rbx = the value stored in rax (%rax) • movq %rax, (%rbx) # value of memory address stored in rbx = %rax • () means: treat bit vector in the register as memory address • movl $0x1abc, %rax# %rax = 0x1abc • $ indicates a constant number value (immediate) • movl 4(%rbx), %rax# %rax = value of (memory address stored in rbx + 4(bytes))
Memory Addressing • General form: • D(Rb, Ri, S) : Mem[Reg[Rb] + S*Reg[Ri] + D] • Example: • (Rb): Mem[Reg[Rb]] • (Rb, Ri): Mem[Reg[Rb] + Reg[Ri]] • D(Rb, Ri): Mem[Reg[Rb] + Reg[Ri] + D] • (Rb, Ri, S): Mem[Reg[Rb] + S*Reg[Ri] ] Scale Base register Displacement Index register
lea_ instruction • “Load Effective Address” • A faster way to calculate the address • Or just do arithmetic • leaq (%rax), %rbx# %rbx = the value in rax • Not the memory address stored in rax • Comparison to movq • movq 2(%rax, %rbx, 8), %rcx# %rcx = Mem[%rax + 8*%rbx + 2] • leaq 2(%rax, %rbx, 8), %rcx# %rcx = %rax + 8*%rbx + 2
Other Instructions • addqsrc, dst# dst = dst + src • subqsrc, dst# dst = dst – src • salqsrc, dst# dst = dst << src • sarqsrc, dst# dst = dst >> src • negqsrc, dst# dst = -dst • Check section 3.5 of the book for more instructions