H4x0

ASM Tutorial Notes

Jul 7th, 2013
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;========================================================================;
  2. ;Authored by H4x0                                                        ;
  3. ;Using samples from the 'blackfireize' Youtube tutorial series           ;
  4. ;http://www.youtube.com/watch?v=GFkrYM7mbTc                              ;
  5. ;http://www.h4x0.com/lib/Snippets/Tutorial_Notes.asm                     ;
  6. ;16Bit Assembly (DOS) - emu8086 & FASM                                   ;
  7. ;========================================================================;
  8. #fasm#
  9. name "mycode"
  10. org 100h                        ;DOS FTW!
  11.  
  12. call one                        ;Used for calling a function or "label"
  13.                                 ;Change to lesson function you want to see
  14.  
  15. call return_os                  ;Call our helper function to return control
  16.                                 ;to the operating system
  17.        
  18. ;========================================================================;
  19. ;Variables                                                               ;
  20. ;========================================================================;    
  21. msgOne db 'Hello World$'        ;Define a string called msgOne
  22. msgThr db 'Look At Source$'
  23.  
  24. ;========================================================================;
  25. ;Lesson functions                                                        ;
  26. ;========================================================================;
  27. one:
  28.   mov ah, 9h
  29.   mov dx, msgOne                ;Move msgOne to dx register
  30.   int 21h                       ;Print (Execute interupt)
  31.   ret                           ;Return from function
  32.  
  33. two:
  34.   mov ax, 123d                  ;Move 123 (Integer) to ax register    
  35.   call print_decimal            ;Call the print_decimal helper function
  36.   ret                           ;Return from function
  37.  
  38. three:
  39.   ;Informational Lesson
  40.  
  41.   ;AX                           ;Acumulator Register (Mathmatical Operations)
  42.     ;AH                           ;A (Register) H (High)
  43.     ;AL                           ;A (Register) L (Low)
  44.    
  45.   ;BX                           ;Base Register (Counting, Manipulation, General)
  46.     ;BH                           ;B (Register) H (High)
  47.     ;BL                           ;B (Register) L (Low)
  48.    
  49.   ;CX                           ;Unknown Name (Counting Operations)
  50.     ;CH                           ;C (Register) H (High)
  51.     ;CL                           ;C (Register) L (Low)
  52.    
  53.   ;DX                           ;Data Register (Holding Memory or Data Pointers)
  54.     ;DH                           ;D (Register) H (High)
  55.     ;DL                           ;D (Register) L (Low)
  56.  
  57.   ;Segment Registers (Point to 64kb memory segments)  
  58.   ;CS                           ;Code Segment
  59.   ;DS                           ;Data Segment
  60.   ;ES                           ;Extra Segment
  61.   ;FS                           ;Extra Segment
  62.   ;GS                           ;Extra Segment
  63.   ;SS                           ;Stack Segment
  64.  
  65.   ;ESI                          ;Extended Source Index
  66.     ;SI                           ;Source Index (String Operations)
  67.    
  68.   ;EDI                          ;Extended Destination Index
  69.     ;DI                           ;Destination Index (General Purposse)
  70.    
  71.   ;EBP                          ;Extended Base Pointer
  72.     ;BP                           ;Base Pointer
  73.    
  74.   ;ESP                          ;Extended Stack Pointer
  75.     ;SP                           ;Stack Pointer
  76.    
  77.   ;EFLAGS                       ;Never Change (at least directly)
  78.     ;FLAGS                        ;Never Change (at least directly)
  79.       ;ZF                           ;Zero Flag
  80.       ;PF                           ;Parody Flag
  81.      
  82.   mov ah, 9h
  83.   mov dx, msgThr                ;Move msgThr to dx register
  84.   int 21h                       ;Print (Execute interupt)
  85.   ret                           ;Return from function
  86.  
  87.  
  88. ;========================================================================;
  89. ;Helper functions                                                        ;
  90. ;========================================================================;
  91. return_os:                      ;Return control to OS
  92.   mov ah, 4ch                  
  93.   int 21h                       ;Execute interupt
  94.  
  95. print_decimal:                  ;AX -> Decimal Number
  96.   pusha                         ;Push all to the stack  
  97.   mov bx, 10d                   ;Move 10 (Integer) to bx register
  98.   mov cx, 0                     ;Move 0 (Integer) to cx register
  99.   mov dx, 0                     ;Move 0 (Integer) to dx register
  100.  
  101.   .tryloop1:                    ;Loop
  102.     inc cx                      ;Register cx++ or cx=cx+1 (Increment)
  103.     mov dx, 0                   ;Move 0 (Integer) to dx register
  104.     div bx                      ;Divide bx register
  105.     push dx                     ;Push dx to stack
  106.     cmp dx, 0                   ;Compare dx register with 0
  107.   jne .tryloop1                 ;Jump back if not equal
  108.    
  109.   mov ah, 02h
  110.  
  111.   .tryloop2:                    ;Loop
  112.     pop dx                      ;Clear dx register
  113.     add dl, 48                  ;Get ASCII char from integer by adding 48
  114.     int 21h                     ;Print (Execute interupt)
  115.   loop .tryloop2                ;
  116.  
  117.   popa                          ;Pop all from stack
  118.   ret                           ;Return from function
Advertisement
Add Comment
Please, Sign In to add comment