September 15, 2011

C Assembly Programming - Binary Operations and Concept of Registers

Assembly Programming series by MyCFiles.com
This post will explain different binary operations and the concept of registers.
Read Part 1 First - Introduction to C Assembly Programming


THEORY
When it comes to assembly or lower level programming there is no way to ignore the concept of bits and bytes and therefore the operations on them. Believe it or not if you neglect binary operations you miss 60 % and therefore whole language..




WHY BINARY ONLY???
Actually the whole essence of bits and bytes lies in the way electronic instruments work. Since it is 100 times easy to work with only 2 nos (binary sys) then 10 (dec sys). Hence binary was adopted in electronics. You better grab a book on Boolean algebra to dig it out how computer internals work (call it ALU whatever).


THE BINARY OPERATIONS
BITWISE AND

first of all consider 2 bin nos

1 0 1 0 1 0 0 0 => 168
1 1 0 0 1 1 0 0 => 204
as is the tradition we represent AND by .(dot) in B-algebra following the rules
1 . 1 = 1
0 . 1 = 0
1 0 1 0 1 0 0 0 => 168
AND 1 1 0 0 1 1 0 0 => 204
-------------
1 0 0 0 1 0 0 0 => 200

BITWISE OR
it is simply the opposite of AND represented by + sign in B algebra following the rules
1 + 1 = 1
0 + 1 = 1
0 + 0 = 0
1 0 1 0 1 0 0 0 => 168
OR 1 1 0 0 1 1 0 0 => 204
-------------
1 1 1 0 1 1 0 0 => 236
understandable enough...

LOGICAL NEGATION - NOT
it is the operation is easy all you have to do it change 0 to one or vice-versa..
Its represented by 'over no or bar over the no.
The rules are
1' = 0
0' = 1
1 0 1 0 1 0 0 0 -> NOT -> 0 1 0 1 0 1 1 1

BITWISE XOR
it has no representation in B-algebra since it is combination of AND OR and NOT... but i will use ^ for it. You may have heard of it as MAGIC OPERATOR whe reason it that it speeds up things incredibaly faster and help generate more efficient code.
The rules are
give 1 ans only and only if 1 s in ques are odd else 0
1 0 1 0 1 0 0 0 => 168
XOR 1 1 0 0 1 1 0 0 => 204
0 1 1 0 0 1 0 0 => 100

XOR when decoded translates to

(a + b) . (a . b)'
better in C
(a || b) && !(a && b)

good programmers use it as efficient way to turn some var to 0 notice
a ^ a = 0

even
a ^= b
b ^= a
a ^= b

will swap a and b(much faster then using any other var).

CPU REGISTERS
Registers are the core of CPU and therefore heart of assembly. infarct learning assembly is to apply instructions to these regs rest is all same as c/c++.

For DOS Registers are 16 bit variables in your intel chip which guides cpu in the sense what to do next.
They are of type.
1. General regs
2. Segment regs
3. Pointer(offset) regs

GENERAL REGISTERS
there are 4 under this category.
AX - accumulator -> AH,AL
BX - base -> BH,BL
CX - counter -> CH,CL
DX - data -> DH,DL

xH,xL stands for there msb(15 to 8) and lsb(8 to 0) respectively.

SEGMENT REGISTER
they are 4
CS - code segment
DS - data segment
ES - extra segment
SS - stack segment
no access to lower or high bit.

POINTER(OFFSET)REGISTERS
they are of 5 type
IP - instruction pointer
SI - source index
DI - destination index
SP - stack pointer
BP - base pointer
no high low access to them too.

THE CONCEPT OF SEGMENT AND OFFSET
If you consider your whole computer memory as 2D-array then segment represents column and offset a row. Any physical entity(data or code) is accessible only if it is in its respective segment(CS or DS) means code within CS is accessible data within DS is accessible..
.
UTILITY
AX - normally as a var
BX - as a var, segment addressing.
CX - as a counter in loops
DX - same as BX

CS - segment having current code
DS - segment having global vars
ES - normally for interrupt calls
SS - segment containing stack of current program

IP - pointer to instruction of current code
SI - in array addressing
DI - same as SI
SP - pointer to stack of code.

Don't Forget to Ask Off Queries :)

Related Posts :



0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Related Posts with Thumbnails