.CODE, .DATA, .STACK, and PROC Directives


Assembly programs are organized around segments, which are usually named code, data, and stack: The code segment contains one or more procedures, with one designated as the startup procedure. identifies the beginning of a procedure, e.g.,
    main PROC
where the procedure name is , which is also the startup procedure.

WriteString
It writes a null-terminated string to standard output. When calling it, place the string’s offset in register. The hexadecimal bytes and are end-of-line characters.
.data
  book  BYTE  "MASM Assembler",
              0Dh, 0Ah, 0
.code
  mov   edx, OFFSET book
  call  WriteString

.data
  hexVal  DWORD   ?
.code
  call    ReadHex
  mov     hexVal, eax
ReadHex
It reads a 32-bit hexadecimal integer from standard input, terminated by the key, and returns the value in register.

WriteHex
It writes a 32-bit unsigned integer to standard output in 8-digit hexadecimal format. Leading zeros are inserted if necessary. Before calling it, place the integer in register.
.data
  X  DWORD  50000h
.code
  mov   eax, X 
  call  WriteHex

ReadSub.asm
COMMENT!
*
* This program reads and subtracts 32-bit hexadecimal integer from 50000h.
*
!

INCLUDE Irvine32.inc

.data
  prompt1  BYTE   0Dh, 0Ah, "Calculate an Arithmetic Expression of 50000h - Y:",
                  0Dh, 0Ah, 0
  prompt2  BYTE   "Enter the value of operand Y:   ", 0
  prompt3  BYTE   0Dh, 0Ah, "The result is  ", 0
  X        DWORD  50000h
  Y        DWORD  ?
  result   DWORD  ?

.code
main PROC
  mov    edx, OFFSET prompt1
  call   WriteString            ; print prompt1

  mov    edx, OFFSET prompt2
  call   WriteString            ; print prompt2

  call   ReadHex
  mov    Y, eax                 ; Y   = eax (the input)
  mov    eax, X                 ; eax = X (50000h)

  ; calculate 50000h – Y
  sub    eax, Y                 ; eax    = X (50000h) – Y (the input)
  mov    result, eax            ; result = eax (X – Y)

  mov    edx, OFFSET prompt3
  call   WriteString            ; print prompt3

  call   WriteHex               ; print eax (result)
  exit

main ENDP
END  main
An Execution Example
 Calculate an Arithmetic Expression of 50000h – Y:
 Enter the value of operand Y:  4000 

 The result is  0004C000

The white italic text with a navy background color is entered by users.




      “If you want to be sure that you never forget your wife’s birthday,    
      just try forgetting it once.”   
      — Aldo Cammarota