You are on page 1of 11

PART 3 ASSEMBLY LANGUAGE

CHAPTER 8 PC ASSEMBLERs Microsoft Macro Assembler for PC Turbo Assemble - Borland DOS/BIOS Functions

CHAPTER 8: MS/TURBO ASSEMBLER


8.1. Definitions: Assembler for an assembly language, a computer program to translate between lowerlevel representations of computer programs A full tool to create a Assembly Language Program (Assembler):
An any standard Editor (ASCII) Assembler:
Checks syntax, error,

Replaces names of label, variable real address, constant symbols 1 pass Converts Assembly Mnemonics to Object Code, 2 pass. Links several modules become an executable file (.EXE file). This procedure is needed even program has one module Option: Converts to .COM file : exe2bin

8.2. DIRECTIVES Pseudo OpCode


Assembly Directives are instructions that are executed by the Assembler at assembly time, not by the CPU at run time. They can make the assembly of the program dependent on parameters input by the programmer, so that one program can be assembled different ways, perhaps for different applications. They also can be used to manipulate presentation of the program to make it easier for the programmer to read and maintain.

PROC ENDP DIRECTIVES:


Are used to identify the start and end of procedure The PROC follows name you give to procedure A term NEAR of FAR is used to specify type of procedure FE:
LED_CON PROC NEAR LED_CON ENDP

Caution: In MASM, main program is known a proc too

SEGMENTENDS DIRECTIVES
To declare a logical segment is used
CODE SEGMENT CODE ENDS

PUBLIC directive to tell assembler that this segment may be put together with other segments named CODE from other modules when the modules are linked together.
CODE SEGMENT PUBLIC

DIRECTIVES
END directives: is put after the last statement of a program to tell assebler that this is the end of program module. Assembler will ignore any statement after END directive. GROUP dir: is used to group the logical segments named after directive into one local group segment

DEFINE DATA DIRECTIVES


DB - define byte: is used to declare a byte type variable or to set aside one or more storage locations of type byte in memory
Temp Name_Here LED_Table DataStorage DB DB DB DB (?); Not initialized BARACK OBAMA; a string 21, 45h, C, 345, (?); 100h DUP (?); Not Init

DW Define word, DD Define Double Word, DT Define TenByte EQU: is used give a name to some value or symbol.
PA_Addr EQU 300h MOV DX,PA_Addr;

Simplified Segment Definition


For more simple, some latter versions of Assembler accept some directives to declare segments: .Stack .Data .Code These directives dont need to combine the ENDS directives

CASE STUDY LED BURNING

EXERCISE: 6 x 7segment - LED interfacing


PPI 8255:
All ports are mode 0, PA and PC are output, PB is Input Address PA is 0E0H, PB: 0E1H, PC: 0E2H and CR: 0E3H PA0: Serial Data PA1: Serial Clock = rise edge PA2: HC595 Latch = rise edge PA3: Clear HC595 = 0 low pulse

LEDs: 6 Common anode, 7 segment LEDs, driven by 74HC595 chips, Fig 2

Description: Suppose that IO space, 0E0h to 0E3h. The system is supported by BIOS and DOS functions for standard Input (Key board, Int 16h), Output (Screen) and terminate In this exercise, some PROCes and MACROes are used CONTENT: Read 6 number characters from Key Board, suppose that no user error in this phase. Convert these digits to BCD then convert to 7 Segment and display to 6 LED the model display board with 74HC595.

;= = = = = = = = = DECLARING = = = = = = = = = = = = = = = = = = = = = .MODEL SMALL ; Declaring model of program: ; tiny/small/medium/large/huge/flat .STACK 64 ; Stack Mem .DATA ; !!! there is a dots front of logical segment Table7Seg DB 3FH, 06H, 5BH, 4FH, 66H, 6DH, 7DH, 07H, 7FH, 6FH ; ; Table contains 10 bytes of 7 seg codes, 0 - 9 @7SegBuf DB 6 DUP (?) ; declaring 6 byte array variable, not ;initialized BCD_Buf DB 6 DUP (?) ; declaring an array 6 bytes, not initialized ; = = = = = = PROGRAM = = = = = = = = = = = = = = = = = = = .CODE ; Program starts here Chinh PROC NEAR ; Main is named for program MOV AX,@DATA ; Assigning logical data segment to ; physical DATA segment MOV DS,AX INIT_PPI ; inserting a macro here Clear_595 ; use macro to clear 6 LEDs to zeros ;====================================

MOV BX, offset BCD_Buf; Read 6 digit ASCII from KB, convert to BCD MOV CX,6 ; and save to BCD_Buf M1: MOV AH,0 ; Wait for character from Standard Input (Key Board) INT 16H ; using the BIOS function, DOSs available also SUB AL,30H ; Convert ASCII to BCD MOV [BX],AL ; saving BCD to BCD_Buf Array INC BX ; Next digit LOOP M1 ; after 6 loops, 6 BCD num are located in BCD_Buff ;======================================== CALL BCD_7Seg ; Converting 6 bytes of un-package BCD to 7 segment CALL SHIFT6BYTE ; Shifting to HC595 IN AL,0E0h ; Displaying OR AL,00000100b ; SET LATCH PULSE OUT 0E0h,AL ; LEDs are turned on after NOP NOP ; Delay 2 machine cycles for Latch pulse is wide enough AND AL,11111011b ; RESET PULSE OUT 0E0h, AL ;

;==================================== MOV AH, 0 Int 16h ; Wait for key pressed to terminate MOV AH,4CH ; Terminating an .exe file a DOS function INT 21H Chinh ENDP ; = = = = = = PROCEDURES AND MACROES ARE DECLARED HERE Init_PPI MACRO PUSH AX MOV AL,10000010b ; PA, PB, PC mode 0, PA, PC out, PB in OUT 0E3h,AL ; Sending to PPI Control Register MOV AL,00001000b ; Un-Clear HC595 OUT 0E0h,AL ; Sending to HC595: data=0, Clk=0, latch=0, Clr=1 POP AX ; Init_PPI ENDM

Clear_595 MACRO IN AL,0E0h ; Read PA AND AL,11110111b ; Clear pins = 0 OUT 0E0h,AL NOP ; Delay for 2 Machine cycles NOP OR AL,00001000b ; Un-clear HC595 OUT 0E0h,AL ; Send to PA Clear_595 ENDM ;================== ShiftBIT MACRO BITT ; BITT is dummy parameter, 0 or 1 PUSH AX ;save content AX to Stack mem MOV AL,BITT ;bit 0 of AL = 0 or 1 OUT 0E0h,AL ; Out Data bit via PA0 NOP OR AL, 00000010b ; Set Clock bit

OUT 0E0h,AL ; Send Clock via PA1 NOP ; delay 2 cycles for clock pulses fat enough NOP ; No Operation AND AL, 11111101b ; Reset Clock bit OUT 0E0h,AL POP AX ShiftBIT ENDM ;=================== BCD_7SEG PROC Near ; Cnvrt 6 digit BCD in BCD_Buf to 7S MOV CX,6 ; Counter = 6 MOV SI,offset @7SegBuf MOV BP,offset BCD_Buf B1: MOV BX,offset Table7Seg ; LEA BX, Table7Seg

MOV AL,[BP] ; read BCD to AL XLAT ; Convert BCD to 7 segment by TRANSLATE MOV [SI],AL INC BP INC SI LOOP B1 RET BCD_ 7SEG ENDP ;=================== SHIFT6BYTE PROC Near ; Shift a byte from @7SegBuf to ;HC595s MOV BX,offset @7SegBuf MOV DL,6 ; Counter of 6 digits S4: MOV CX,8 ; counter of 8 bit/digit

10

MOV AL, [BX] S3: SHL AL,1 ; Shift Left AL via Carry Flag, MSB is first JNC S1 ; jump if this bit is zero, If..Then..Else strctr ShiftBIT 1 ; a MACRO, shifting a bit 1 JMP S2 ; Jumping over shift BIT 0 to continue S1: ShiftBIT 0 :Macro shift a bit S2: LOOP S3 ; CX==CX-1 if CX <>0 then goto S2 INC BX ; Next bit DEC DL ; Have 6 digits shifted? JNE S4 ; If not equ zero, goto S4 RET ; SHIFT6BYTE ENDP END

11

You might also like