assembly - ASM - 8086 - Using Registers and variables -


am starting grips asm programming feel missing regarding use of registers store variables. issue have instructions modify other registers internal reasons. in cases have used these registers store application logic. there golden rules how use registers?

for example: following code changes dx register , wipes out current variable.

 mov ax, 04h mov bx, 02h mul bx 

i did not want nor state want dx register wiped out. what's going on here?

welcome assembly language programming. short answer pick values manipulated often. rule compilers use register allocation. score usages numerically heuristic values, put best scores in registers until there no more.

in 8086 assembler, have small number of registers, , many of them have special purposes. you've discovered one: mul , div implicitly use ax , dx. think of mul instruction mul dx:ax, operand, , you'll see what's going on. makes life harder, the assembly level, architecture designer doesn't care. he's trying make hardware implementation small , fast.

studying x86 assembly 32-bit operands make life easier in regard because lots of special usages go away. example, there 2-operand mul instructions take destination register , source add.

rules of thumb:

  • put variables in loop bodies in registers. nested loops, it's innermost, work outward.

  • for computations require special purpose usages: multiply , divided (implicitly use ax , dx operands), variable shift counts (need cx), address calculations (need bx, si, or di), etc., try arrange required number in necessary register during previous computation rather adding mov. called register targeting.

  • look @ assembly generated compiler new ways of thinking. many person-years of expert though go strategies use. can learn them.


Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -