1 / 15

ICS312 Set 4

ICS312 Set 4. Program Structure. Outline for a SMALL Model Program Note the quiz at the next lecture will be to reproduce this slide. .MODEL SMALL .586 ; allows Pentium instructions to be used .STACK 100H .DATA  ; put data definitions here .CODE MAIN  PROC 

sema
Download Presentation

ICS312 Set 4

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ICS312 Set 4 Program Structure

  2. Outline for a SMALL Model ProgramNote the quiz at the next lecture will be to reproduce this slide .MODEL SMALL .586 ; allows Pentium instructions to be used .STACK 100H .DATA  ; put data definitions here .CODE MAIN  PROC  ; put instructions for MAIN procedure here MAIN  ENDP ; include additional procedures after MAIN procedure SUB1 PROC ... SUB1 ENDP END MAIN      ; END directive ends program              ; Label used with END directive matches               ; label of first instruction to be executed in the            ; code segment. (Any legal label can be used.) 

  3. Program Structure (1) Memory Model Use SMALL memory model for assignments --- creates 1 code segment, 1 data segment and 1 stack segment. .MODEL SMALL Processor Directive To use the Pentium instruction set for PCs, code the 586 processor directive immediately after the .MODEL directive Example .MODEL SMALL .586

  4. Program Structure (2) • Stack Segment • Set the stack size to 100h bytes, using • .STACK 100h • If no stack size is set, the default size is • 1000h bytes. A smaller stack size (100h bytes) • is more than enough for programs in this • course.

  5. Program Structure (3) Data Segment Define all variables in the data segment, so the memory allocated for them will be in the program's data area. Different ways data can be coded Numbers (all converted into binary) binary numberswith suffix “b”, eg: 0101101b hexadecimal numberswith suffix “h”, eg: 3Fh decimal number Characters Use single or double quotes eg: ‘HI THERE’ or “HI THERE” Variables Variables require that memory space be allocated for storing values of a specified data type.  In assembler programming, a variable's data type determines the amount of space needed:  one byte, two bytes, 4 bytes, etc. Byte Variables declared using DB pseudo-opcode Word Variables (two bytes) declared using DW pseudo-opcode Double Word Variables (four bytes) declared using DD pseudo-opcode

  6. Program Structure (4) • Example of a Data Segment • .DATA • CRLF   EQU 0DH, 0AH • PROMPT DB  'Enter a digit between 0 and 9$’ • VAR1   DB  ? • ARRAY  DW  1234h, 23h, 0FF54h

  7. Program Structure (5) • Code Segment • The code segment contains all the source code for the program. • Starts with the directive:    .CODE • Use procedures to organize the program code (example below) • Assembly Language Syntax • General form of assembly language Instructions: • [name]    [operation code]    [operand(s)]     [;comment] • Types of Assembly Language Statements:  • Instructions - part of the machine language instruction set.  • Instructions will be converted into machine code by the • assembler translator. • Directives/Pseudo-Operations - directives are NOT part of the • machine language instruction set and will be processed by the • assembler translator at assembly time.

  8. Function Description Register Setup Result 1 Input one character from keyboard with echo to screen AH = 1 AL = ASCII code of the character, if char key pressed, or AL = 0, if non-char key pressed eg:the up arrow 2 Output one character to screen AH = 2 DL = ASCII code or control character code Contents of DL are written on screen Input and Output Instructions Some basic INT 21H Input/Output Functions: • Examples shown above: • Read a character from the keyboard • Display a character on the monitor

  9. ASCII code Symbol Function 07h BEL beep 08h BS backspace 09h HT horizontal tab 0Ah LF line feed 0Dh CR carriage return ASCII codes for control characters

  10. Function: Description: Input: 9 Display a string AH = 9 DX = offset address of string. String must end with '$' Displaying a String

  11. Write a program that displays a message on the screen. • Define the message in the data segment of the program. • Use LEA Instruction to put the address of the message • in the DX register.  • Display the Message. • TITLE SAMPLE PROGRAM • .MODEL SMALL • .586 • .STACK 100H • .DATA • MES DB ‘This is a sample program$’ • .CODE • MAIN PROC • MOV AX, @DATA • MOV DS, AX • MOV AH, 9 • LEA DX, MES • INT 21H • MOV AH, 4CH • INT 21H • MAIN ENDP • END MAIN Example The portions in the blue boxes will occur in every main program exactly as shown, except that the label “main” (in its three ocurrences) can be replaced by any other label.

  12. HMW: Your First Program • Write a program to read a character from the keyboard • and display it at the beginning of the next line. • Program outline: • Display a question mark to prompt for input. • Read a character • Display the character on the next line • save the character in another register • move the cursor to the next line (execute carriage • return and line feed) • display the character

  13. Creating and running your program Create the source file in text format using anyword processor. Give it a name with “asm” as suffix, e.g: first.asm To use the word processor available in dos, employ: edit first.asm Refer to the instructions on downloading Masm 6.15 on the web page. Assemble andrun first.asm as illustrated there, i.e: ml first.asm first

  14. Notes: • For the purpose of readibility, employ function 2 to output a single character, and function 9 to output a string of more than one character • You can’t assume that the int 21h subroutines will preserve the values of al, ax or eax. So your code for a new line might wipe out any character that you have just read into al.

  15. Textbook Reading (Jones): Chapter 2 Assembler Overview & first part of Chapter 3

More Related