Week 2 Discussion Section Atefeh Sohrabizadeh atefehszcsuclaedu 101119 Agenda mov Quizzes Control Flags and Conditional Operations Stack Procedure Call Passing Argument BombLab ID: 798827
Download The PPT/PDF document "CS33: Introduction to Computer Organizat..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
CS33: Introduction to Computer OrganizationWeek 2 – Discussion Section
Atefeh Sohrabizadeh
atefehsz@cs.ucla.edu
10/11/19
Slide2Agendamov Quizzes
Control Flags and Conditional Operations
Stack
Procedure Call
Passing Argument
BombLab
Notes
Slide3mov Quizzesmovl
%
eax
, %bx
Invalid: can’t move 32 bit to a 16-bit container
movl %ax, %ebxInvalid: can’t move 32 bit from a16-bit containermovb %al, %ebxInvalid: movb wants to move a byte, but dst is a 32-bit containermovb %al, %bl%rbx = 0x89abcdefmovzbl %al, %ebx %rbx = 0x000000efmovsbl %al, %ebx%ebx = 0xffffffefmovsbq %al, %ebxInvalid: moving 64-bit to 32-bit container
rax
0x123456ef
rbx
0x89abcd78
Slide4Additional notesThe size of the register operands must match the size of prefix designated by the instruction
Memory references can work with all sizes:
E.g.:
movb
%al, (%
rbx)What about movb %al, (%ebx)?Not meaningful on a 64-bit machine, since you are giving a 32 bit address
Slide5Control FlagsSingle bit values that change with some instructions
SF (Sign Flag)
Is set if the
most recent
operation set MSB to 1
ZF (Zero Flag)Is set if the most recent operation produce zero (or is zero)Usually used for checking equality or if the result is zeroCF (Carry Flag)Is set if the operation has carry outUsually used for unsigned overflow (or comparison)OF (Overflow Flag)Is set if (a>0 && b>0 && t<0) || (a<0 && b<0 && t>=0)Usually used for signed overflow (or comparison)What bit will “addq %rax, %rbx” set if there is an overflow?Both CF and OF. Then based on programmer’s intention, it will interpret the flags
Slide6Control Flags (Cont.)Some instructions only set the flags:
cmp
a, b : compute b-a and set the flags accordingly
testq
a, b : compute
a&b and set the flags accordinglyNote that it doesn’t change the value of a or bControl flags usage:Set value of registers using setX family:E.g.: sete %al → sets %al to 1 if ZF=1Use for conditional operations:Conditional moveE.g.: cmove src, dst → dst=src if ZF=1Conditional jumpsE.g.: je L1 → jump to label L1 if ZF=1
Slide7Notes on jump operationsjmp
L1
Jump to the instruction labeled as L1
jmp
0x400020
Jump to the instruction at address 0x400020jmp *%raxJump to the instruction that its address is in raxjmp *(%rax)Jump to the instruction that its address is in memory location %raxThe address of the memory location is the value in raxYou can define the address in D(Rb, Ri, S) format
Slide8StackA storage for local variables
It grows downwards
%
rsp
contains lowest stack address
Use push and pop to modify itpushq srcDecrement %rsp by 8Write operand src at address given by %rsppopq dstStore value addressed by
%
rsp
in
dst
Increment
%
rsp
by 8
Increasing
Addresses
Stack “Top”
Stack “Bottom”
Stack Pointer
:
%
rsp
Slide9Procedure Call%rip stores the instruction pointer
%
rsp
stores the top of current stack frame
%
rbp stores the base of current stack frameCalling the procedure: (callq label)Push return address to stackReturn address is the instruction pointer to the next instruction after function callJump to labelReturning from the procedure: (retq)Pop return address from stackJump to the return address
Slide10Passing ArgumentsThe main procedure passes arguments to the called procedure:
For the first 6
args
: %
rdi
, %rsi, %rdx, %rcx, %r8, %r9 respectivelyThe rest of the args are pushed to stackArg 7 is at top of stackThe returned value is at:%raxAfter passing arguments, the procedure call happensThe called procedure can modify these registers
Slide11Saving ValuesWhen P calls Q:
Q is responsible for saving registers %
rbx
, %
rbp
, and %r12-%r15Q must ensure these registers have the same value when they return to PEither by not modifying them Or pushing their value to stack before modification and pop them before returningP is responsible for saving all other registers except %rspAs Q may change the rest of the registers, P should push the value of the registers it is using to stack before making the call
Slide12BombLab NotesDon’t start in TUI mode as it may show garbled output
Start GDB with “$
gdb
bomb”
Run “layout
asm” in gdb to enter TUI mode and see assembly codeDon’t use “run” command in TUI mode as it may show garbled outputCheckout GDB section in the course websiteLocated at: https://polyarch.github.io/cs33/06-resources/Start early!
Slide13Useful Commands When Debugging in AssemblyUse
si
instead of s
stepi
(or
si) execute one machine instruction (follows a call)step (or s) execute one C-program statement (steps into functions)Use ni instead of nInfo register (or “i r”)Shows the value of registersi r $rax: show only value of raxOr just use: print $raxuntil *0x400e58Continue execution until instruction at address 0x400e58x/nfu addressPrints value at the specified memory addressn: how many units to printf: display format (x, d, u, o, t, a, c, f, s)u: unit (b, h, w, g)x/4xw 0x84200: prints the four words starting at address 0x84200 in hex formatCheck week 0 slides and page 280 of the book for more