FilipBogo

H2 ex 4

Oct 23rd, 2024
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Z80 Assembler 1.76 KB | Source Code | 0 0
  1. bits 32 ; assembling for the 32 bits architecture
  2.  
  3. ; declare the EntryPoint (a label defining the very first instruction of the program)
  4. global start        
  5.  
  6. ; declare external functions needed by our program
  7. extern exit               ; tell nasm that exit exists even if we won't be defining it
  8. import exit msvcrt.dll    ; exit is a function that ends the calling process. It is defined in msvcrt.dll
  9.                           ; msvcrt.dll contains exit, printf and all the other important C-runtime specific functions
  10.  
  11. ; our data is declared here (the variables needed by our program)
  12. segment data use32 class=data
  13.    
  14.     a db 20
  15.     b db 3
  16.     c dw 2
  17.     e dd -444
  18.     x dq 43
  19.     v1 resq 1
  20.  
  21. ; our code starts here
  22. segment code use32 class=code
  23.     start:
  24.         ;(a-2)/(b+c)+a*c+e-x; a,b-byte; c-word; e-doubleword; x-qword Signed
  25.        
  26.         mov eax,0
  27.         mov ecx,0
  28.         mov al,[b];al=b
  29.         cbw;ax=b
  30.         mov cx,[c];cx=c
  31.         add cx,ax;cx=b+c
  32.         mov eax,0
  33.         mov al,[a]
  34.         sub al,2;al=a-2
  35.         cbw
  36.         cwd
  37.         idiv cx
  38.         cwde
  39.         mov ebx,eax;ebx=(a-2)/(b+c)
  40.         mov eax,0
  41.         mov al,[a]
  42.         cbw
  43.         cwd
  44.         imul word[c];dx:ax=a*c
  45.         shl eax,16
  46.         mov ax,dx
  47.         rol eax,16
  48.         ;we converted dx:ax to eax
  49.         add eax,ebx;eax=(a-2)/(b+c)+a*c
  50.         add eax,[e]
  51.         ;eax=(a-2)/(b+c)+a*c+e
  52.         cdq
  53.         sub eax,dword[x+0]
  54.         sbb edx,dword[x+4];edx:eax=(a-2)/(b+c)+a*c+e-x
  55.         mov [v1+0],eax
  56.         mov [v1+4],edx
  57.         ;we assigned v1 the value of the expresion
  58.         ; exit(0)
  59.         push    dword 0      ; push the parameter for exit onto the stack
  60.         call    [exit]       ; call exit to terminate the program
  61.  
Advertisement
Add Comment
Please, Sign In to add comment