assembly intro, MOV, ADD jbwyatt.com

.. Assembly Language: What is it? Why do we use it?

Assembly language is hard - but it's better than writing machine code !!
Remember that ultimately, EVERYTHING becomes ones and zeroes in the machine...

   Assembly language is a symbolic representation of the machine code
   used to write programs for an architecture. Primitive!
       
   Once a program has been compiled, it is difficult - or impossible -
   to decompile the code back to its HLL.
       
   Debuggers will frequently only show the program code in assembly.
   
   Assembly language is often the only tool available for implementing
   some low-level tasks, such as bootloaders, drivers and low-level kernel components.
   
   Assembly code has less overhead than code written in HLLs, so it may run faster.
   HOWEVER: Developing a program in assembly is a very time consuming process!.

   Assembly instructions have a 1-to-1 relationship with the machine instructions.
  
An assembler Turns assembly code into machine code.
MOV AL, 5 (Assembly Language) 10110000 00000101 (Machine Language)
Assembly Machine -------- ------- MOV AX => assembler => 10110000 5 => assembler => 00000101

Do you need to understand computer architecture and software execution better? YES!

  To work with assembly language programs we must:
     learn how the computer represents data and instructions
     learn how the computer manipulates data and instructions
     learn how the represented information is organized
     learn about assembly language
        syntax
        representation
     learn about the internals of the computer
        registers / cpu
        ram
        organization of data and instructions into a program
     learn in detail how a program is executed

.. MOV instruction

    MOV transfers contents of the source to the destination MOV destination, source <<-------- MOV al, 3 ; move an IMMEDIATE value of 3 into al machine code of instruction is: 'B0 03' ------- 'B0' => mov AL (with an immediate value) '03' => actual 3 MOV AL, var1 ; move contents of RAM at count to 8 bit AL register machine code of instruction is: 'A000010400' ---------- 'A0' => mov AL (with a memory address) '00010400' => address of var1 ---------------------------------------- Both operands must be of the SAME size. ---------------------------------------- Examples: wsum DW 5678h, 1234h; bsum DB 78h,56h,34h,12h MOV sum, 1255 ; decimal 1255 in ram at SUM MOV AL, 34h ; 34 hex (52 dec) in 8 bit register AL MOV BL, AL ; contents of AL to BL MOV DX, 3*4 ; 12 dec to DX MOV BX, sum ; contents of memory at SUM to EAX reg

    emu: MOV instruction (scroll down)


    ERRORS... ========= mov al, 256 ; error: too large mov bx, AbyteVar ; error: size mismatch mov cx, bl ; error: operand not same size mov wordVar1, wordVar2 ; error: mem-to-mem

.. ADD instruction

         ADD adds the contents of the source to the destination
    
         add destination, source
                  <<--------
    
          add al, 3
          add al, bl
          add al, x
    

.. Example Program Code

    EXAMPLE ASSEMBLY CODE (from emu)
    =====================
    name "add-sub"
    
    org 100h
    
    MOV al, 5       ; bin=00000101b
    MOV bl, 10      ; hex=0ah or bin=00001010b
    
    ; 5 + 10 = 15 (decimal) or hex=0fh or bin=00001111b
    add bl, al
    
    ; 15 - 1 = 14 (decimal) or hex=0eh or bin=00001110b
    sub bl, 1
    
    ; print result in binary:
    MOV cx, 8
    print: MOV ah, 2   ; print function.
           MOV dl, '0'
           test bl, 10000000b  ; test first bit.
           jz zero
           MOV dl, '1'
    zero:  int 21h
           shl bl, 1
    loop print
    
    ; print binary suffix:
    MOV dl, 'b'
    int 21h
    
    ; wait for any key press:
    MOV ah, 0
    int 16h
    ret
    
    Same program with machine code
    ======================================================================
    [LINE]     LOC: MACHINE CODE         SOURCE
    ======================================================================
    [   1]        :             name "add-sub"
    [   2]        :
    [   3]        :             org 100h
    [   4]        :
    [   5]    0100: B0 05       MOV al, 5       ; bin=00000101b
    [   6]    0102: B3 0A       MOV bl, 10      ; hex=0ah or bin=00001010b
    [   7]        :
    [   8]        :             ; 5 + 10 = 15 (decimal) or hex=0fh or bin=00001111b
    [   9]    0104: 02 D8       add bl, al
    [  10]        :
    [  11]        :             ; 15 - 1 = 14 (decimal) or hex=0eh or bin=00001110b
    [  12]    0106: 80 EB 01    sub bl, 1
    [  13]        :
    [  14]        :             ; print result in binary:
    [  15]    0109: B9 08 00    MOV cx, 8
    [  16]    010C: B4 02       print: MOV ah, 2   ; print function.
    [  17]    010E: B2 30       MOV dl, '0'
    [  18]    0110: F6 C3 80    test bl, 10000000b  ; test first bit.
    [  19]    0113: 74 02       jz zero
    [  20]    0115: B2 31       MOV dl, '1'
    [  21]    0117: CD 21       zero:  int 21h
    [  22]    0119: D0 E3       shl bl, 1
    [  23]    011B: E2 EF       loop print
    [  24]        :
    [  25]        :             ; print binary suffix:
    [  26]    011D: B2 62       MOV dl, 'b'
    [  27]    011F: CD 21       int 21h
    [  28]        :
    [  29]        :             ; wait for any key press:
    [  30]    0121: B4 00       MOV ah, 0
    [  31]    0123: CD 16       int 16h
    [  32]        :
    [  33]    0125: C3          ret
    [  34]        :
    
    

    ; Example assembly language program -- adds 158 to number in memory ; Author: R. Detmer ; Date: 10/2004 .386 .MODEL FLAT ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD .STACK 4096 ; reserve 4096-byte stack .DATA ; reserve storage for data number DWORD -105 sum DWORD ? .CODE ; start of main program code _start: MOV eax, number ; first number to EAX add eax, 158 ; add 158 MOV sum, eax ; sum to memory INVOKE ExitProcess, 0 ; exit with return code 0 PUBLIC _start ; make entry point public END ; end of source code

    EXAMPLE ASSEMBLY / C++ CODE ============================== int Func2(int first, int second) { __asm { MOV eax,0 MOV ecx,second L1: ;loop adds first to itself, second amout of times add eax,first ;same as multiplying first by second loop L1 MOV first,eax ;moves value from eax to variable 'first' to return } return first; }


.. The Process of Assembly

      
    
      
  • Assembler reads what?
  • How many times through?
  • How does the symbol table work with the assembler?
  • What is the final output of the assembler?
  • So, we're now ready to execute the code?
  • NO! - what creates the exe?
  • libraries?

  • emu: assembling the assembly code