10 = 0AH
start: jmp main option1 db 0ah, 0dh, "1. OPTION 1 $" option2 db 0ah, 0dh, "2. OPTION 2 $" exit db 0ah, 0dh, "3. EXIT $" str1 db 0ah, 0dh, "Press Key: $" x db 0ah, 0dh, "You Pressed option 1 $" y db 0ah, 0dh, "You Pressed option 2 $" z db 0ah, 0dh, "End $" nvl db 0ah, 0dh, "Invalid Option $" one db "1" two db "2" tre db "3" main proc mov ah,09h lea dx,option1 int 21h lea dx,option2 int 21h lea dx,exit int 21h again: mov ah,09h lea dx,str1 int 21h mov ah,01 int 21h mov bl,al cmp bl,"1" je disp1 cmp bl,"2" je disp2 cmp bl,"3" je dispexit cmp al,one jne n cmp al,two jne n cmp al,tre jne n n: mov ah,09h lea dx,nvl int 21h jmp again disp1: mov ah,09h lea dx,x int 21h jmp again disp2: mov ah,09h lea dx,y int 21h jmp again dispexit: mov ah,09h lea dx,z int 21h int 20h main endp end start
name "str2bin" ; convert string number to binary! ; this program written in 8086 assembly language to ; convert string value to binary form. ; this example is copied with major modifications ; from macro "scan_num" taken from c:\emu8086\inc\emu8086.inc ; ; the original "scan_num" not only converts the string number ; but also reads the string from the keyboard and supports ; backspace key, this example is a shorten version ; of original "scan_num" macro. ; here we assume that the string number is already given, ; and the string number does not contain non-digit chars ; and it cannot cause buffer overflow (number is in word range ; and/or has only 4 digits). ; negative values are allowed in this example. ; the original "scan_num" does not allow to enter non-digits ; and it also checks for buffer overflow. ; you can the original file with other macro definitions ; in c:\emu8086\inc\emu8086.inc org 100h jmp start ; text data: msg1 db 0Dh,0Ah, " enter any number from -32768 to 65535 inclusive, or zero to stop: $" msg2 db 0Dh,0Ah, " binary form: $" ; buffer for int 21h/0ah ; fist byte is buffer size, ; second byte is number of chars actually read (set by int 21h/0ah). buffer db 7,?, 5 dup (0), 0, 0 ; for result: binary dw ? start: ; print welcome message: mov dx, offset msg1
Org 1000 mov si,1100 mov di,1400 cld mov bl,20h next: lodsb cmp al, bl je exit sub al,30h cmp al,0ah jc store sub al,07h store: stosb jmp next exit: hlt
Source program :MVI D, COUNT : Initialize counterMVI B, 00 : Initialize variable to store previous numberMVI C, 01 : Initialize variable to store current numberMOV A, B :[Add two numbers]BACK: ADD C :[Add two numbers]MOV B, C : Current number is now previous numberMOV C, A : Save result as a new current numberDCR D : Decrement countJNZ BACK : if count 0 go to BACKHLT : Stop.
10 = 0AH
start: jmp main option1 db 0ah, 0dh, "1. OPTION 1 $" option2 db 0ah, 0dh, "2. OPTION 2 $" exit db 0ah, 0dh, "3. EXIT $" str1 db 0ah, 0dh, "Press Key: $" x db 0ah, 0dh, "You Pressed option 1 $" y db 0ah, 0dh, "You Pressed option 2 $" z db 0ah, 0dh, "End $" nvl db 0ah, 0dh, "Invalid Option $" one db "1" two db "2" tre db "3" main proc mov ah,09h lea dx,option1 int 21h lea dx,option2 int 21h lea dx,exit int 21h again: mov ah,09h lea dx,str1 int 21h mov ah,01 int 21h mov bl,al cmp bl,"1" je disp1 cmp bl,"2" je disp2 cmp bl,"3" je dispexit cmp al,one jne n cmp al,two jne n cmp al,tre jne n n: mov ah,09h lea dx,nvl int 21h jmp again disp1: mov ah,09h lea dx,x int 21h jmp again disp2: mov ah,09h lea dx,y int 21h jmp again dispexit: mov ah,09h lea dx,z int 21h int 20h main endp end start
name "str2bin" ; convert string number to binary! ; this program written in 8086 assembly language to ; convert string value to binary form. ; this example is copied with major modifications ; from macro "scan_num" taken from c:\emu8086\inc\emu8086.inc ; ; the original "scan_num" not only converts the string number ; but also reads the string from the keyboard and supports ; backspace key, this example is a shorten version ; of original "scan_num" macro. ; here we assume that the string number is already given, ; and the string number does not contain non-digit chars ; and it cannot cause buffer overflow (number is in word range ; and/or has only 4 digits). ; negative values are allowed in this example. ; the original "scan_num" does not allow to enter non-digits ; and it also checks for buffer overflow. ; you can the original file with other macro definitions ; in c:\emu8086\inc\emu8086.inc org 100h jmp start ; text data: msg1 db 0Dh,0Ah, " enter any number from -32768 to 65535 inclusive, or zero to stop: $" msg2 db 0Dh,0Ah, " binary form: $" ; buffer for int 21h/0ah ; fist byte is buffer size, ; second byte is number of chars actually read (set by int 21h/0ah). buffer db 7,?, 5 dup (0), 0, 0 ; for result: binary dw ? start: ; print welcome message: mov dx, offset msg1
01start: jmp main 02 03 option1 db 0ah, 0dh, "1. OPTION 1 $" 04 option2 db 0ah, 0dh, "2. OPTION 2 $" 05 exit db 0ah, 0dh, "3. EXIT $" 06 07 str1 db 0ah, 0dh, "Press Key: $" 08 09 x db 0ah, 0dh, "You Pressed option 1 $" 10 y db 0ah, 0dh, "You Pressed option 2 $" 11 z db 0ah, 0dh, "End $" 12 13 nvl db 0ah, 0dh, "Invalid Option $" 14 15 one db "1" 16 two db "2" 17 tre db "3" 18 19 20main proc 21 22 23 mov ah,09h 24 lea dx,option1 25 int 21h 26 27 lea dx,option2 28 int 21h 29 30 lea dx,exit 31 int 21h 32again: 33 mov ah,09h 34 lea dx,str1 35 int 21h 36 37 mov ah,01 38 int 21h 39 40 mov bl,al 41 42 cmp bl,"1" 43 je disp1 44 45 cmp bl,"2" 46 je disp2 47 48 cmp bl,"3" 49 je dispexit 50 51 cmp al,one 52 jne n 53 54 cmp al,two 55 jne n 56 57 cmp al,tre 58 jne n 59 60n: 61 mov ah,09h 62 lea dx,nvl 63 int 21h 64 jmp again 65 66 67disp1: 68 mov ah,09h 69 lea dx,x 70 int 21h 71 jmp again 72 73 74disp2: 75 mov ah,09h 76 lea dx,y 77 int 21h 78 jmp again 79 80dispexit: 81 mov ah,09h 82 lea dx,z 83 int 21h 84 int 20h 85 86 87main endp 88end start
Computer languages vary in their complexity, speed and ease of use. The easiest languages to use are insulated very far from the hardware; these are "high level" languages. Low level languages are much "closer to the metal" of the computer i.e. they are very detailed, very complex and vary widely based on the hardware they are being used to program. Binary (the computers 'natural' language) and Assembly are examples of low-level languages as they vary widely from computer to computer.Low level languages are great for speed in execution but are not portable (most likely unable to be run on different types of computers) and are tedious to use. An example:Basic -10 Print "Hello World"Assembly -name "hello-world"org 100hmov ax, 3int 10hmov ax, 1003hmov bx, 0int 10hmov ax, 0b800hmov ds, axmov [02h], 'H'mov [04h], 'e'mov [06h], 'l'mov [08h], 'l'mov [0ah], 'o'mov [0ch], ','mov [0eh], 'W'mov [10h], 'o'mov [12h], 'r'mov [14h], 'l'mov [16h], 'd'mov cx, 12mov di, 03ha: mov [di], 11101100badd di, 2loop amov ah, 0int 16hret
; This may not be what you wnat in that it displays in Decimal but, it should ; get you started. It was writtne in x86 Assembly using emu8086. Bruce name Powers ; author: Bruce Hesher ; ; View the flags register, Variables, and the Stack while running ; this program. ; AX - used to store sum ; BX - used to count the number of time the subroutine loops ; CX - used by scan_num to store value ; DX - used to return value ; org 100h ; Create COM file include 'emu8086.inc' ; emu8086.inc has some useful subroutines. ; prompt user for the number of Fibonacci numbers to display. mov ah, 9 mov dx, offset msg_prompt int 21h call scan_num ; Type digits then ENTER, result goes in CX lea si, msg_CRLFLF ; 1 CR and 2 LF call print_string lea si, space ; Used PRINT_STRING inside the program loop. mov ax, 0 ; Clear AX. mov al, cl ; Seed AL with number to show powers of. mov [value], cx ; mov number to value since CX used by loop mov cx, 20 NEXT: mul [value] ; (DX AX) = AX * operand. call print_num_uns ; print value in AX call print_string loop NEXT ; jump if no overflow to next ret ; to OS ; Define variables msg_prompt db 'Enter the number to calc the powers of: $' msg_CRLFLF db 0Dh, 0Ah, 0Ah space dw 20h value dd 0h ; dd directive to define double length word (4bytes). ; Define selected subroutines from emu8086.inc DEFINE_SCAN_NUM DEFINE_PRINT_STRING DEFINE_PRINT_NUM_UNS ; Print unsigned number in AX END ; directive to stop the assembler.
Sample problem 1:(2200H) = 67H(2300H) = 6 x OAH + 7 = 3CH + 7 = 43HSource program :LDA 2200H : Get the BCD numberMOV B, A : Save itANI OFH : Mask most significant four bitsMOV C, A : Save unpacked BCDI in C registerMOV A, B : Get BCD againANI FOH : Mask least significant four bitsRRC : Convert most significant four bits into unpacked BCD2RRCRRCRRCMOV B, A : Save unpacked BCD2 in B registerXRA A : Clear accumulator (sum = 0)MVI D, 0AH : Set D as a multiplier of 10Sum: ADD D : Add 10 until (B) = 0DCR B : Decrement BCD2 by oneJNZ SUM : Is multiplication complete? i if not, go back and add againADD C : Add BCD1STA 2300H : Store the resultHLT : Terminate program executionFlowchart for program
print macro msg lea dx,msg movah,09h int 21h endm read macro n,j1,j2 mov cx,0ah j1:mov ah,01h int 21h cmpal,0dh je j2 sub al,30h mov bl,al mov ax,n mul cx xor bh,bhadd ax,bx mov n,axjmp j1 j2 :nop endm .model small .stack 100h .data msg1 db 10,13,'Enter the 1st number: $' msg2 db 10,13,'Enter the 2nd number: $' msg3 db 10,13,'The Sum= $' num1 dw 0 num2 dw 0 .code main proc mov ax,@data movds,ax print msg1 ;reading 1st multidigit number read num1,jump1,jump2 print msg2 ;reading 2nd multidigit number read num2,jump3,jump4 ;finding sum mov ax,num1 add ax,num2 ;printing number mov bx,000ah xorcx,cx ;push into stack p1:xordx,dx div bx pushdx inc cx cmp ax,0000h jne p1 print msg3 ;pop from stack display:popdx add dl,30h movah,02h int 21h loop display movah,4ch int 21h main endp end
Org 1000 mov si,1100 mov di,1400 cld mov bl,20h next: lodsb cmp al, bl je exit sub al,30h cmp al,0ah jc store sub al,07h store: stosb jmp next exit: hlt
Source program :MVI D, COUNT : Initialize counterMVI B, 00 : Initialize variable to store previous numberMVI C, 01 : Initialize variable to store current numberMOV A, B :[Add two numbers]BACK: ADD C :[Add two numbers]MOV B, C : Current number is now previous numberMOV C, A : Save result as a new current numberDCR D : Decrement countJNZ BACK : if count 0 go to BACKHLT : Stop.
.MODEL SMALL .STACK 100H .DATA PROMPT_1 DB 'Enter a string : $' PROMPT_2 DB 0DH,0AH,'No. of Vowels = $' PROMPT_3 DB 0DH,0AH,'No. of Consonants = $' STRING DB 50 DUP (?) C_VOWELS DB 'AEIOU' S_VOWELS DB 'aeiou' C_CONSONANTS DB 'BCDFGHJKLMNPQRSTVWXYZ' S_CONSONANTS DB 'bcdfghjklmnpqrstvwxyz' .CODE MAIN PROC MOV AX, @DATA ; initialize DS and ES MOV DS, AX MOV ES, AX LEA DX, PROMPT_1 ; load and display the string PROMPT_1 MOV AH, 9 INT 21H LEA DI, STRING ; set DI=offset address of variable STRING CALL READ_STR ; call the procedure READ_STR XOR DX, DX ; clear DX LEA SI, STRING ; set SI=offset address of variable STRING OR BX, BX ; check BX for 0 JE @EXIT ; jump to label @EXIT if BX=0 @COUNT: ; jump label LODSB ; set AL=DS:SI LEA DI, C_VOWELS ; set DI=offset address of variable C_VOWELS MOV CX, 5 ; set CX=5 REPNE SCASB ; check AL is capital vowel or not JE @INCREMENT_VOWELS ; jump to label @INCREMENT_VOWELS if AL is ; capital vowel LEA DI, S_VOWELS ; set DI=offset address of variable S_VOWELS MOV CX, 5 ; set CX=5 REPNE SCASB ; check AL is small vowel or not JE @INCREMENT_VOWELS ; jump to label @INCREMENT_VOWELS if AL is ; small vowel LEA DI, C_CONSONANTS ; set DI=offset address of variable ; C_CONSONANTS MOV CX, 21 ; set CX=21 REPNE SCASB ; check AL is capital consonant or not JE @INCREMENT_CONSONANTS ; jump to label @INCREMENT_CONSONANTS if AL ; is capital consonant LEA DI, S_CONSONANTS ; set DI=offset address of variable ; S_CONSONANTS MOV CX, 21 ; set CX=21 REPNE SCASB ; check AL is small consonant or not JE @INCREMENT_CONSONANTS ; jump to label @INCREMENT_CONSONANTS if AL ; is small consonants JMP @NEXT ; otherwise, jump to label @NEXT @INCREMENT_VOWELS: ; jump label INC DL ; increment DL JMP @NEXT ; jump to label @NEXT @INCREMENT_CONSONANTS: ; jump label INC DH ; increment DH @NEXT: ; jump label DEC BX ; decrement BX JNE @COUNT ; jump to label @COUNT while BX!=0 @EXIT: ; jump label MOV CX, DX ; set CX=DX LEA DX, PROMPT_2 ; load and display the string PROMPT_2 MOV AH, 9 INT 21H XOR AX, AX ; clear AX MOV AL, CL ; set AL=CL CALL OUTDEC ; call the procedure OUTDEC LEA DX, PROMPT_3 ; load and display the string PROMPT_3 MOV AH, 9 INT 21H XOR AX, AX ; clear AX MOV AL, CH ; set AL=CH CALL OUTDEC ; call the procedure OUTDEC MOV AH, 4CH ; return control to DOS INT 21H MAIN ENDP READ_STR PROC ; this procedure will read a string from user and store it ; input : DI=offset address of the string variabel ; output : BX=number of characters read ; : DI=offset address of the string variabel PUSH AX ; push AX onto the STACK PUSH DI ; push DI onto the STACK CLD ; clear direction flag XOR BX, BX ; clear BX @INPUT_LOOP: ; loop label MOV AH, 1 ; set input function INT 21H ; read a character CMP AL, 0DH ; compare AL with CR JE @END_INPUT ; jump to label @END_INPUT if AL=CR CMP AL, 08H ; compare AL with 08H JNE @NOT_BACKSPACE ; jump to label @NOT_BACKSPACE if AL!=08H CMP BX, 0 ; compare BX with 0 JE @INPUT_ERROR ; jump to label @INPUT_ERROR if BX=0 MOV AH, 2 ; set output function MOV DL, 20H ; set DL=20H INT 21H ; print a character MOV DL, 08H ; set DL=08H INT 21H ; print a character DEC BX ; set BX=BX-1 DEC DI ; set DI=DI-1 JMP @INPUT_LOOP ; jump to label @INPUT_LOOP @INPUT_ERROR: ; jump label MOV AH, 2 ; set output function MOV DL, 07H ; set DL=07H INT 21H ; print a character MOV DL, 20H ; set DL=20H INT 21H ; print a character JMP @INPUT_LOOP ; jump to label @INPUT_LOOP @NOT_BACKSPACE: ; jump label STOSB ; set ES:[DI]=AL INC BX ; set BX=BX+1 JMP @INPUT_LOOP ; jump to label @INPUT_LOOP @END_INPUT: ; jump label POP DI ; pop a value from STACK into DI POP AX ; pop a value from STACK into AX RET READ_STR ENDP OUTDEC PROC ; this procedure will display a decimal number ; input : AX ; output : none PUSH BX ; push BX onto the STACK PUSH CX ; push CX onto the STACK PUSH DX ; push DX onto the STACK CMP AX, 0 ; compare AX with 0 JGE @START ; jump to label @START if AX>=0 PUSH AX ; push AX onto the STACK MOV AH, 2 ; set output function MOV DL, "-" ; set DL='-' INT 21H ; print the character POP AX ; pop a value from STACK into AX NEG AX ; take 2's complement of AX @START: ; jump label XOR CX, CX ; clear CX MOV BX, 10 ; set BX=10 @OUTPUT: ; loop label XOR DX, DX ; clear DX DIV BX ; divide AX by BX PUSH DX ; push DX onto the STACK INC CX ; increment CX OR AX, AX ; take OR of Ax with AX JNE @OUTPUT ; jump to label @OUTPUT if ZF=0 MOV AH, 2 ; set output function @DISPLAY: ; loop label POP DX ; pop a value from STACK to DX OR DL, 30H ; convert decimal to ascii code INT 21H ; print a character LOOP @DISPLAY ; jump to label @DISPLAY if CX!=0 POP DX ; pop a value from STACK into DX POP CX ; pop a value from STACK into CX POP BX ; pop a value from STACK into BX RET ; return control to the calling procedure OUTDEC ENDP END MAIN