understanding how to convert pseudo code into x86 assembly -


can me understand how able convert pseudo code x86 assembly? don't want answer, understanding , implementing in assembly language.

    = 5     b = 3     c = 1     if == 3:       c = + b     if c > b:       z = 2 

first need plan memory model or register allocation -- have decide, if going use registers variables, or if want place variables in memory, either stack or heap.

one option:

a located in al, ax, eax or rax depending on variable length b located in bl, bx, ebx or rbx c located in cl, cx, ecx or rcx z located in dl, dx, edx or rdx 

to initialize variable in register, 1 uses immediate move:

mov al, 5 

to initialize variable in stack, 1 needs designate area in stack, or use red zone, small area above stack, guaranteed not modified (-4 largest offset store int32_t , -128 smallest. see this link red-zone)

mov eax, 5 mov [esp - 4], eax   ; store 5 above current stack pointer 

edit pointed ped7g, red zone x64 concept, , idiomatic way allocate frame manually, , use positive offsets:

sub esp, 12          ; allocate 12 bytes of stack mov [esp+0], eax     ; there's space 3 variables mov [esp+4], ebx mov [esp+8], ecx ... more code ... add esp, 12          ; deallocate after variables not needed ret                  ; ... before returning 

and if target x86 (i.e. 16-bit isa), 1 has build stack frame, using bp fixed reference in stack, allowing sp move around , subroutines called.

arithmetic can performed between registers, or between register , memory

add [esp - 4], ebx   ; adds value in ebx variable in stack add ebx, eax         ; adds ebx := ebx + eax 

arithmetic typically performed dst_register := dst_register op src, destroys original value of destination. e.g. found in c = + b; pseudocode, has broken 2 steps --

mov ecx, eax   ; c = add ecx, ebx   ; c = c + b    i.e c = + b; 

or 1 can go deep in x86 programming , find lea ecx, [eax + ebx] paradigm doing additions (and more) in single instruction.

then last piece conditional execution, typically sequenced branches:

if (something) do_something    // transformed  if (!something) goto next_label; do_something next_label: 

in x86 there 16 different kinds of something, e.g. jumping when previous instruction set flags special, such as

sub eax, eax   ;  produces 0 , sets 0 flag jnz skip       ;  jump never taken  cmp eax, 3     ;  subtract 'a  - 3', discarding result, modifying flags accordingly jnz skip       ;  don't next thing, if == 3 (because 3-3 = 0 ==> zeroflag = 1) 

the last missing piece less comparison, has 2 variants, 1 signed comparison , 1 unsigned.

the mnemonics signed inequalities jl, jg, jle, jge meaning jump less, jump greater, jump less or equal, jump greater or equal.

the mnemonics unsigned inequalities jb, ja, jbe, jae standing jump below, jump above, jump below or equal , jump above or equal

in order of flags set correctly, 1 has use either cmp or sub instruction before conditional jump (or add such instructions, not modify flags).


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -