Assignment 04
Assignment 04
Subject: OOP
Assignment: 4
Saad Ahmad
Submitted By: Roll No: 10120
BS.CS 3rd B
X86-64
Intel first created the beginnings of its “x86” assembly language
way back in about the early 1980’s with its 8088 8-bit CPU. This was
followed quickly by a 16-bit version called the 8086, from which we get
the “86” in “x86”. The late 1980’s saw the first 32-bit CPU, the 80386, and
many 32-bit redesigns were done; eventually a need to move to 64 bits
arose, and so they created the “x86-64” extension. (Actually, AMD
created it before Intel!)
x86-64, also known as x64 or AMD64, is a 64-bit extension of the
x86 instruction set architecture (ISA) that was originally developed by
AMD. It was later adopted by Intel and is now widely used in modern
CPUs.
REGISTERS
In addition to the legacy alpha-named registers, x86-64 added
some numbered registers, and so now there are 16 64-bit registers: %rax,
%rbx, %rcx, %rdx, %rdi, %rsi, %rbp, %rsp, and %r8-15. While they all work
generally, many have very specific purposes. For example, %rsp is the
stack pointer and should never be used for anything else; %rbp is
generally used as a call frame pointer and not for anything else; etc. The
alpha-named registers all have a %eXX 32-bit equivalent, but the
numbered registers do not.
FUNCTION CALLING:
In 32-bit x86, all function arguments were passed on the stack; this
is drastically changed in x86-64. In x86-64, the first six function
arguments are passed in the registers %rdi, %rsi, %rdx, %rcx, %r8, and
%r9, in order. If a function has more than six arguments, the rest are
passed on the stack. The function return value is passed in %rax
(and %rdx if more bits needed). The registers %rbx, %rbp, and %r12-15
are considered callee-saved registers. This means they are not freely
available to use in a function; if a function wants to use them, it must first
save them (by pushing them on the stack), and then restore them at the
end. All other registers are considered freely available (the caller must
save them before the call if it needs their values after the call).
THE STACK:
“The Stack” refers to the hardware-supported function call stack,
where each function call creates an activation record (also known as
a stack frame or call frame) on the stack, which contains information
necessary for the function call to operate. This information generally
includes: argument values (if not passed in registers), the return
address, callee-saved register values, and local variables. Not every
function call will have all of these, but every function call will at least
have a return address, which is the place in the calling function to return
to when the function call ends.
The CPU supports the stack with the %rsp register, known as
the stack pointer. This register contains the memory address of the top of
the stack, and the stack grows “downward” towards lower memory
addresses. So subtracting 64 from the stack pointer is equivalent to
opening up 64 bytes of usable memory space on the stack! Indeed, this is
exactly how space for local variables is created!
In x86-64, usually the first two instructions in a function save the
current %rbp on the stack (“pushq %rbp”) and then copy the stack
pointer to %rbp (“movq %rsp, %rbp”). This makes %rbp a__ frame
pointer__ for the current function call. Then all references to local
variables and to arguments in memory are made using indirect
addressing with %rbp. Since the %rbp is initialized before space is
created on the stack (by subtraction), then local variable space is all
with a negative offset from %rbp. So the memory address of a local
variable looks something like “-16(%rbp)”, which is assembly syntax for
subtracting 16 from the address value in %rbp, and using the result in
indirect addressing.
64-BIT ADDRESSING
The x86-64 architecture supports 64-bit virtual addresses, allowing
it to access up to 16 exabytes of memory. This is much larger than the 4
gigabytes of memory that can be addressed by IA-32.
BACKWARDS COMPATIBILITY
The x86-64 architecture is backwards compatible with IA-32, which
means that it can run most existing x86 software without modification.
This is achieved through a mode called legacy mode, which allows the
CPU to switch between the x86-64 and IA-32 instruction sets.