You are on page 1of 27

Code for PROGRAM TO FIND THE REVERSE OF A STRING USING MACRO in Assembly Languag e GETSTR MACRO STR MOV

AH,0AH LEA DX,STR INT 21H ENDM PRINTSTR MACRO STR MOV AH,09H LEA DX,STR INT 21H ENDM DATA SEGMENT STR1 DB 80,80 DUP('$') STR2 DB 80,80 DUP('$') MSG1 DB 10,13,'ENTER THE STRING :$' MSG2 DB 10,13,'THE REVERSE STRING IS :$' STORE DB 2 DUP('$') DATA ENDS CODE SEGMENT ASSUME CS:CODE,DS:DATA START: MOV AX,DATA MOV DS,AX PRINTSTR MSG1 GETSTR STR1 PRINTSTR MSG2 LEA SI,STR1+2 LEA DI,STR2+2 MOV MOV MOV MOV LAB1: INC SI LOOP LAB1 MOV CX,BX LAB2: MOV AL,DS:BYTE PTR[SI] ;FOR COPYING CONTENTS OF STR1 TO AL MOV DS:BYTE PTR[DI],AL ;FOR COPYING CONTENTS OF AL TO STR2 INC DI DEC SI LOOP LAB2 PRINTSTR STR2+2 MOV AH,DS:BYTE PTR[SI] MOV STORE,AH ;FOR GOING TO THE END OF THE STRING CL,STR1+1 CH,00H BL,CL BH,00H ;FOR STORING THE LENGTH OF THE STRING

PRINTSTR STORE MOV AX,4C00H INT 21H CODE ENDS END START OUTPUT *********** D:\tasm\tasm1>m1 ENTER THE STRING :abc cba REVERSE STRING IS

I wrote a program a few weeks ago for class, and this program was used to find a ll the 3 digit integers which equal the sum of the cubes of their digits. Now im supposed to modify this program to print out all prime numbers in range from 100 to 999. Could someone please help me out in getting me started or tell me what steps i should take in doing this. Here is my first program: ;Program to Find all the 3 digit integers which ;equal the sum of the cubes of their digits .MODEL SMALL .STACK 200H .DATA CRLF db " $",13d,10d First_Digit DW 1 ;most significant 100's digit Second_Digit DW 0 Third_Digit DW 0 ;least significant .CODE ;establish a value in DS, the data segment register MOV Ax,@DATA ;access to data now MOV DS, Ax ;is established ;first, get the sum of the cubes of the digits New_Number: MOV Ax, First_Digit MOV Cx, Ax MUL Cx ;this gets the square ;second argument is always Ax, so this MUL Cx by Ax ;Result is 32 bit and spread over into Dx and Ax ;The highest possible answer is 729 and that fits plenty into the Ax and never have to use Dx b/c it will always be 0 MUL Cx ;this gets the cube in Dx and Ax, but Dx=0 MOV Bx,Ax ;save the cube into Bx MOV Cx,Second_Digit MOV Ax,Cx

MUL Cx MUL Cx ;cube of the 2nd digit in Ax (dx=0) ADD Bx,Ax ;accunulates the sum of the first two cubes MOV MOV MUL MUL Cx,Third_Digit Ax,Cx Cx Cx ;cube of 3rd digit

ADD Bx,Ax ;accumulate the final result ;get the number as a regular number, not the sum of its cubes MOV Cx,10d ;calculate 100 x 1st digit + 10 x 2nd digit + 3rd MOV Ax,First_Digit ;worst case is 10 x 9, fits in Ax alone (Dx=0) MUL Cx ADD Ax,Second_Digit MUL Cx ADD Ax,Third_Digit ;now the actual number is in Ax ;Compare sum of cubes with the number CMP Ax,Bx ;Jump over the print statements if the numbers are NOT equal JNZ New_Digit ;If here, the two quantites were indeed equal MOV AH,2d ;BIOS code for print a character MOV Dx,First_Digit ;worst case is 9 so the number goes into DL and you can igno re DH ADD DL,30h ;convert to ASCII INT 21h ;The first digit is printed MOV MOV ADD INT MOV MOV ADD INT AH,2d Dx,Second_Digit DL,30H 21h ;The second digit is printed AH,2d Dx,Third_Digit DL,30H 21h ;The third digit is printed

MOV ah, 9d MOV dx, offset CRLF INT 21h

New_Digit: ;Increment like an odometer INC Third_Digit CMP THird_Digit,10d ;To check if we have a digit that equals ten JNZ New_Number ;Because the third digit is still OK MOV THIRD_Digit,0d ;resets the third digit INC Second_Digit CMP Second_Digit,10d JNZ New_Number MOV Second_Digit,0d INC First_Digit CMP First_Digit,10d JZ Label_A

JMP New_number Label_A: ;If here we are done Mov Ah,4ch INT 21h end

%include "asm_io.inc" %macro prologue 0 push rbp mov rbp,rsp push rbx push r12 push r13 push r14 push r15 %endmacro %macro epilogue 0 pop r15 pop r14 pop r13 pop r12 pop rbx leave ret %endmacro section .bss input resd 1

section .rodata prompt db "Enter a number: ",0 promptlen equ $-prompt cube db "The cube of the number is: ",0 square db "The square of the number is: ",0 cube25 db "The value of cube of the number times 25 is: ",0 quotient db "The quotient of cube/100 is: ",0 remainder db "The remainder of cube/100 is: ",0 negation db "The negation of the remainder is: ",0 section .text global main main: prologue ; print the prompt for the user mov rdx, promptlen mov rsi, dword prompt push 0x1 pop rdi mov rax,rdi syscall ; read the integer mov rdi, dword input call read_int

; calculate its square and print the output mov rdi, dword square call print_string mov rdi, [input] imul rdi, rdi mov rbx, rdi call print_int call print_nl ; calculate its cube and print the output mov rdi,dword cube call print_string mov rdi, [input] imul rdi,rdi imul rdi,[input] mov rbx, rdi call print_int call print_nl ; calculate the cube times 25 and print the output mov rdi, dword cube25 call print_string mov rdi, rbx imul rdi,0x19 call print_int call print_nl ; calculate cube/100 and print the output mov rdi, dword quotient call print_string ; initialize rax and sign extend it to rdx mov rax,rbx cqo mov rcx,0x64 ; this is the hex representation of 100 idiv rcx push rdx mov rdi, rax call print_int call print_nl ; calculate the remainder of cube/100 and print the output mov rdi, dword remainder call print_string pop rdx mov rdi,rdx push rdx call print_int call print_nl ; calculate the negation of the remainder and print the output mov rdi, dword negation call print_string pop rdi neg rdi call print_int call print_nl epilogue

This program is nothing but repetitive multiplication of a single number. The nu mber is first save in a memory variable 'num' and then used twice to obtain the cube of that number. The hexadecimal to decimal conversion algorithm is the same that I've used in other sample programs. data SEGMENT msg1 DB 'Enter a number: $' msg2 DB 0dh,0ah,'The cube of this number is: $' exit DB 0dh,0ah,'press any key to exit...$' num DB ? ENDS code SEGMENT start: MOV AX, data MOV DS, AX print MACRO msg ;Boiler plate code to specify ;Segment Registers ;Define macro 'print'

MOV AH,09h ;Prints the string whose MOV DX,OFFSET msg ;starting offset is stored INT 21h ENDM print msg1 MOV AH,01h INT 21h SUB AL,'0' PUSH AX print msg2 POP AX CMP AL,1 ;Calculates the binary value ;Saves AX, prints string ;'msg2' then retrieves the ;COntent of AX ;If the number entered is 1 ;Prints string 'msg1' ;Asks user for a digit ;in DX register

JBE skip MOV num,AL MUL num MUL num MOV BX,10 MOV CX,0 hex2dec: MOV DX,0 DIV BX ADD DL,'0' PUSH DX INC CL CMP AX,10 JGE hex2dec MOV DL,AL ADD DL,'0' MOV AH,02h INT 21h disp: POP DX MOV AH,02h INT 21h LOOP disp JMP xit skip: MOV DL,AL ADD DL,'0' MOV AH,02h INT 21h xit: print exit MOV AH, 1 INT 21h MOV AX, 4c00h INT 21h ENDS END start

;or 0,skips the calculation ;Saves number in 'num' ;Make square of 'num' ;Make cube of 'num' ;Loads divisor ;Clears counter ;Clears DX ;Divide DX-AX by BX (10) ;Gets ASCII value ;Saves value on stack ;Increases count ;COntinues looping until ;quotient is >=10 ;Brings final quotient ;in DL,gets the ASCII and ;Prints the character

;Retrieves remainder ;and Prints it

;If number entered is a ;0 or a 1, just prints ;the character and exits

;Prints string 'exit' ;Waits for a key to be ;pressed by user ;Exit to operating system.

;Set entry point and stop the assembler.

Gets the sum of two single digit numbers in assembly This is a program that calculates the sum of two numbers that are single digit using assembly language. jmp start mess1 db mess2 db mess3 db mess4 db sum db 0 start: mov ax,3 int 10h 10,13,"Enter 1st value: $" 10,13,"Enter 2nd value: $" 10,13,"Total sum value: $" 10,13,"Do you want ot continue (y/n): $"

lea dx,mess1 mov ah,9 int 21h mov ah,1 int 21h sub al,30h cmp al,0 jl start cmp al,9 jg start mov bl,al lea dx,mess2 mov ah,9 int 21h mov ah,1 int 21h sub al,30h cmp al,0 jl start cmp al,9

jg start add al,bl ; adds the two numbers mov sum,al lea dx,mess3 mov ah,9 int 21h cmp al,9 mov al,sum cmp al,9 jg twodigit mov dl,al or dl,30h mov ah,2 int 21h jmp continue int 20h continue: xor dx,dx lea dx,mess4 mov ah,9 int 21h mov ah,1 int 21h cmp al,'y' je start cmp al,'n' je quit twodigit: ; prints sum if above 9 xor ax,ax mov al,sum mov bl,10 div bl mov bh,ah mov dl,al or dl,30h mov ah,2 int 21h mov dl,bh or dl,30h mov ah,2 int 21h jmp continue quit: int 20h

jmp start mess1 db 10,13, "Enter value for (n) [0-9 only]: $" mess2 db 10,13, "There is nothing to display $" mess3 db 10,13, "Do you want to continue (y/n): $" lincar db 10,13, "$" space db " $" asterisk db "*$" value db 0 start: xor ax,ax xor bx,bx xor cx,cx mov ax,3 int 10h lea dx,mess1 mov ah,9 int 21h mov ah,1 int 21h sub al,30h cmp al,0 je zero cmp al,0 jl start cmp al,9 jg start mov value,al mov bh,al mov ch,bh mov cl,al again: cmp bh,0 je continue lea dx,lincar mov ah,9 int 21h mov al,value sub cl,al back:

cmp cl,0 je display lea dx,space mov ah,9 int 21h inc cl jmp back display: lea dx,asterisk mov ah,9 int 21h lea dx,space mov ah,9 int 21h inc cl cmp cl,ch jne display dec bh dec ch dec cl jmp again zero: lea dx,mess2 mov ah,9 int 21h continue: xor dx,dx lea dx,mess3 mov ah,9 int 21h mov ah,1 int 21h cmp al,'y' je start_bridge cmp al,'n' je exit jmp continue start_bridge: call start exit: int 20h

Source Code: jmp start mess1 db 10,13, "Enter value for (n) [0-9 only]: $" mess2 db 10,13, "There is nothing to display $" mess3 db 10,13, "Do you want to continue (y/n): $" lincar db 10,13, "$" space db " $" asterisk db "*$" value db 0 start: xor ax,ax xor bx,bx xor cx,cx mov ax,3 int 10h lea dx,mess1 mov ah,9 int 21h mov ah,1 int 21h sub al,30h cmp al,0 je zero cmp al,0 jl start cmp al,9 jg start mov bh,al mov value,al mov ch,1 again: cmp bh,0 je continue lea dx,lincar mov ah,9 int 21h

mov bl,value mov al,ch sub al,bl mov cl,al back: cmp cl,0 je display lea dx,space mov ah,9 int 21h inc cl jmp back display: lea dx,asterisk mov ah,9 int 21h lea dx,space mov ah,9 int 21h inc cl cmp cl,ch jne display dec bh inc ch jmp again zero: lea dx,mess2 mov ah,9 int 21h continue: xor dx,dx lea dx,mess3 mov ah,9 int 21h mov ah,1 int 21h cmp al,'y' je start_bridge cmp al,'n' je exit jmp continue

start_bridge: call start exit: int 20h

d.Source Code: jmp start mess1 db 10,13, "Enter value for (n) [0-9 only]: $" mess2 db 10,13, "There is nothing to display $" mess3 db 10,13, "Do you want to continue (y/n): $" lincar db 10,13, "$" space db " $" asterisk db "*$" value db 0 start: xor ax,ax xor bx,bx xor cx,cx mov ax,3 int 10h lea dx,mess1 mov ah,9 int 21h mov ah,1 int 21h sub al,30h cmp al,0 je zero cmp al,0 jl start cmp al,9 jg start

mov bh,al mov value,al mov ch,1 again: cmp bh,0 je continue lea dx,lincar mov ah,9 int 21h mov bl,value mov al,ch sub al,bl mov cl,al back: cmp cl,0 je display lea dx,space mov ah,9 int 21h lea dx,space mov ah,9 int 21h inc cl jmp back display: lea dx,asterisk mov ah,9 int 21h lea dx,space mov ah,9 int 21h inc cl cmp cl,ch jne display dec bh inc ch jmp again zero: lea dx,mess2 mov ah,9

int 21h continue: xor dx,dx lea dx,mess3 mov ah,9 int 21h mov ah,1 int 21h cmp al,'y' je start_bridge cmp al,'n' je exit jmp continue start_bridge: call start exit: int 20h c.Source Code: jmp start mess1 db 10,13, "Enter value for (n) [0-9 only]: $" mess2 db 10,13, "There is nothing to display $" mess3 db 10,13, "Do you want to continue (y/n): $" lincar db 10,13, "$" space db " $" asterisk db "*$" value db 0 start: xor ax,ax xor bx,bx xor cx,cx mov ax,3 int 10h lea dx,mess1 mov ah,9 int 21h mov ah,1 int 21h sub al,30h cmp al,0 je zero cmp al,0 jl start cmp al,9 jg start mov value,al

mov bh,al mov ch,bh mov cl,al again: cmp bh,0 je continue lea dx,lincar mov ah,9 int 21h mov al,value sub cl,al back: cmp cl,0 je display lea dx,space mov ah,9 int 21h lea dx,space mov ah,9 int 21h inc cl jmp back display: lea dx,asterisk mov ah,9 int 21h lea dx,space mov ah,9 int 21h inc cl cmp cl,ch jne display dec bh dec ch dec cl jmp again zero: lea dx,mess2 mov ah,9 int 21h continue: xor dx,dx lea dx,mess3 mov ah,9 int 21h

mov ah,1 int 21h cmp al,'y' je start_bridge cmp al,'n' je exit jmp continue start_bridge: call start exit: int 20h

b.jmp start mess1 db 10,13, "Enter a value (n) [0-9 only]: $" mess2 db 10,13, "Do you want to continue (y/n): $" mess3 db 10,13, "There is nothing to display $" space db " $" lincar db 10,13, "$" value db 0 counter db 0 s_counter db 0 start: xor ax,ax xor bx,bx xor cx,cx mov ax,3 int 10h lea dx,mess1 mov ah,9 int 21h mov ah,1 int 21h sub al,30h cmp al,0 jl start cmp al,9 jg start mov value,al cmp al,0 je zero lea dx,lincar mov ah,9 int 21h mov ah,0 mov al,value

mov cx,ax mov counter,1 mov bh,counter mov bl,value mov al,0 display: cmp bh,bl jg next mov dl,'*' mov ah,2 int 21h lea dx,space mov ah,9 int 21h inc bh jmp display next: dec bl inc al mov bh,counter lea dx,lincar mov ah,9 int 21h loop display jmp quit zero: lea dx, mess3 mov ah,9 int 21h quit: int 20h

a.jmp start mess1 db 10,13, "Enter a value (n) [0-9 only]: $" mess2 db 10,13, "Do you want to continue (y/n): $" mess3 db 10,13, "There is nothing to display $" space db " $" lincar db 10,13, "$" value db 0 counter db 0 start: xor ax,ax xor bx,bx xor cx,cx mov ax,3 int 10h lea dx,mess1 mov ah,9 int 21h

mov ah,1 int 21h sub al,30h cmp al,0 jl start cmp al,9 jg start mov value,al cmp al,0 je zero lea dx,lincar mov ah,9 int 21h mov ah,0 mov al,value mov cx,ax mov counter,1 mov bh,counter mov bl,1 jmp display start_bridge: jmp start display: cmp bh,bl jg next mov dl,'*' mov ah,2 int 21h lea dx,space mov ah,9 int 21h inc bh jmp display next: inc bl mov bh,counter lea dx,lincar mov ah,9 int 21h loop display jmp continue zero: lea dx, mess3 mov ah,9 int 21h jmp continue continue: xor dx,dx

lea dx,mess2 mov ah,9 int 21h mov ah,1 int 21h cmp al,'y' je start_bridge cmp al,'n' je quit jmp continue quit: int 20h

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ jmp start mess1 db 10,13, "Enter a value (n) [0-9 only]: $" mess2 db 10,13, "Do you want to continue (y/n): $" mess3 db 10,13, "There is nothing to display $" space db " $" lincar db 10,13, "$" value db 0 counter db 0 start: xor ax,ax xor bx,bx xor cx,cx mov ax,3 int 10h lea dx,mess1 mov ah,9 int 21h mov ah,1 int 21h sub al,30h

cmp al,0 jl start cmp al,9 jg start mov value,al cmp al,0 je zero lea dx,lincar mov ah,9 int 21h mov ah,0 mov al,value mov cx,ax mov counter,1 mov bh,counter mov bl,1 jmp display start_bridge: jmp start display: cmp bh,bl jg next mov dl,'*' mov ah,2 int 21h lea dx,space mov ah,9 int 21h inc bh jmp display next: inc bl mov bh,counter lea dx,lincar mov ah,9 int 21h loop display jmp continue zero: lea dx, mess3 mov ah,9 int 21h jmp continue continue: xor dx,dx lea dx,mess2 mov ah,9 int 21h

mov ah,1 int 21h cmp al,'y' je start_bridge cmp al,'n' je quit jmp continue quit: int 20h

##############################

mov al,total call hex2asc mov dx,offset res mov ah,09h int 21h mov ax,4c00h int 21h main endp readinteger proc near mov bx,00

mov bl,10 mov ah,01h int 21h sub al,'0' mul bl mov num1,al mov ah,01h int 21h sub al,'0' mov num2,al ret readinteger endp hex2asc proc near push ax push bx push cx push dx push si mov cx,00h mov bx,0Ah rpt1: mov dx,00 div bx add dl,'0' push dx inc cx cmp ax,0Ah jge rpt1 add al,'0' mov [si],al rpt2: pop ax inc si mov [si],al loop rpt2 inc si mov al,'$' mov [si],al pop si pop dx pop cx pop bx pop ax ret hex2asc endp end Output: Program to accepts two numbers from single ASCII digit: Multiply two numbers and store it in AL register: Enter two values whose result is less then 250: Enter the two number <xx>:23 First number in 10th position is :20 Second number in 1th position is:03 Result is:60

2)Write and run an assembly program that checks if two strings are identical. As sume that the strings are stored one after the other in the memory. In case the two strings ar e identical the

program output should be IDENTICAL, otherwise the output should be NOT IDENTICA L. Prg(stride.asm) Title program to check if two string are identical or not. dosseg

.model small .stack .data msg db 13,10,"Program to check two strings entered identical or Not:$" msg1 db 13,10,"End the string with dolor as break symble:$" msg2 db 13,10,"Enter two strings of equal length:$" msg3 db 13,10,"Enter first string:$" msg4 db 13,10,"Enter the second string:$" msg5 db 13,10,"Entered two string are identical:$" msg6 db 13,10,"Entered two string are not identical:$" buf db 80 DUP(0) revbuf db 80 DUP(0) .code main proc mov ax,@data mov ds,ax lea dx,msg mov ah,09h int 21h lea dx,msg1 mov ah,09h int 21h lea dx,msg2 mov ah,09h int 21h ;reading first string lea dx,msg3 mov ah,09h int 21h lea si,buf read: mov ah,01h int 21h mov [si],al inc si cmp al,24h je sec jmp read ;reading second string sec: lea dx,msg4 mov ah,09h int 21h lea si,revbuf read1: mov ah,01h int 21h mov [si],al inc si cmp al,24h je cou jmp read1 cou: mov bx,00

mov cx,00 lea si,buf cou1: mov al,[si] inc si cmp al,24h je dis inc bx jmp cou1 dis: lea si,buf lea di,revbuf mov cx,00 mov cx,bx check:mov al,[si] cmp [di],al jne pal inc di inc si loop check lea dx,msg5 mov ah,09h int 21h jmp ou pal:lea dx,msg6 mov ah,09h int 21h ou:mov ax,4c00h int 21h main endp end Output: Program to check two strings entered identical or Not: End the string with dolor as break symble: Enter two strings of equal length: Enter first string:saint$ Enter the second string:saint$ Entered two string are identical: F:\masm>stride Program to check two strings entered identical or Not: End the string with dolor as break symble: Enter two strings of equal length: Enter first string:staint$ Enter the second string:saint$ Entered two string are not identical:

3)Write and run a assembly subroutine that multiplies two numbers passed to it a s parameters. You must write and run the appropriate main program that calls the subroutine. Prg(submul.asm) Title program to multiply two numbers passed as parameter dosseg

.model small .stack .data cr equ 0dh lf equ 0ah msg db cr,lf,"Program to multiply two number passed as paramets to subfunction.. ..:$" msg1 db cr,lf,"Enter firs numbe:$" msg2 db cr,lf,"Enter second number:$" msg3 db cr,lf,"After multiply the two number.......................:$" msg4 db cr,lf,"Total=:$" num1 dw ? num2 dw ? total dw ? result db 30 DUP(0) buff db 80 db 0 db 80 DUP(?) .code main proc mov ax,@data mov ds,ax lea dx,msg mov ah,09h int 21h lea dx,msg1 mov ah,09h int 21h call readinteger lea dx,msg2 mov ah,09h int 21h call readinteger1 ;multiplying the two numbers lea dx,msg3 mov ah,09h int 21h call multiple ;printing the total lea dx,msg4 mov ah,09h int 21h mov ax,00 mov si,offset result mov ax,total call hex2asc mov dx,offset result mov ah,09h int 21h

You might also like