1

Currently I'm learning assembly, and I'm using a86 macro assembler, Oracle VM VirtualBox. I'm wondering why do we declare or put variables in such way (after jump command)? If I declare or put the variables before jump command the program will become error. Is there any explanation behind such structure? Thank you.

seg1 segment
   org 100h
   jump start
   ; variables here (comment)
   start:

   mov ah, 4ch
   int 21h
seg1 ends
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Andre
  • 41
  • 1
  • 6
  • If you place your variables before the jump they will be executed as code and that's not what you want to do generally. So you can jump over them or you can place them below the code that exits your program (after int 21h / ah=4ch). In other memory models you can place data in different segment altogether which would allow you to separate data and code. With org 100h I gather you are creating .COM programs where data and code are in the same segment. – Michael Petch Oct 02 '15 at 04:53
  • What will happend if the variable declaration is executed as a code? – Andre Oct 02 '15 at 04:57
  • The processor will decode the variables as instructions (it doesn't know the difference between bytes that make up variables and bytes that make up code - they are all bytes). In the best case nothing (not likely) will happen; worst case crash and burn (more than likely) because the data may form illegal instructions or cause the program to hang; or cause the processor to fault. It depends what is in the variables. – Michael Petch Oct 02 '15 at 05:00
  • @Michael so if the variables didn't executed as a code how does the variables made or exist in the memory? Thank you for answering my question – Andre Oct 02 '15 at 05:25
  • The assembler and linker will produce an executable with your variables inside along with your code. – Michael Petch Oct 02 '15 at 05:28

1 Answers1

1

The main idea is to make not possible for the variables to be executed as a code. Don't forget that from the CPU point of view, the data and the programs are the same. If you make jump to the variables area or if the IP comes to it by execution of the instructions, the CPU will try to execute them, but as long as the variables probably will contains no meaningful instructions some CPU exception will happen and the program will crash or hangs.

Although, jumping over the variables is not the best way to do these things. You can with the same success put all the variables at the end of the program and they will never be executed as well. With the advantage to use one instruction less and making the code more readable avoiding unnecessary jumps:

seg1 segment
   org 100h

; code here
   mov ah, 4ch
   int 21h      ; this will never return, but end the program.

; variables here will never be executed.

seg1 ends
johnfound
  • 6,857
  • 4
  • 31
  • 60