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

Increment like an odometer INC Third_Digit CMP THird_Digit.Cx Cx Cx .10d .Ax .30H 21h . fits in Ax alone (Dx=0) MUL Cx ADD Ax.worst case is 10 x 9.2d Dx.The first digit is printed MOV MOV ADD INT MOV MOV ADD INT AH.cube of the 2nd digit in Ax (dx=0) ADD Bx.Compare sum of cubes with the number CMP Ax.0d .10d JNZ New_Number MOV Second_Digit.accunulates the sum of the first two cubes MOV MOV MUL MUL Cx.First_Digit .now the actual number is in Ax .resets the third digit INC Second_Digit CMP Second_Digit. the two quantites were indeed equal MOV AH.Second_Digit DL.Jump over the print statements if the numbers are NOT equal JNZ New_Digit .First_Digit .Third_Digit DL.2d Dx.accumulate the final result . 9d MOV dx.0d INC First_Digit CMP First_Digit.2d .Third_Digit Ax.The second digit is printed AH.Ax .Third_Digit .10d JZ Label_A .MUL Cx MUL Cx .10d .calculate 100 x 1st digit + 10 x 2nd digit + 3rd MOV Ax. offset CRLF INT 21h New_Digit: .get the number as a regular number.To check if we have a digit that equals ten JNZ New_Number .cube of 3rd digit ADD Bx.Second_Digit MUL Cx ADD Ax. not the sum of its cubes MOV Cx.convert to ASCII INT 21h .The third digit is printed MOV ah.Because the third digit is still OK MOV THIRD_Digit.30h .Bx .30H 21h .BIOS code for print a character MOV Dx.If here.worst case is 9 so the number goes into DL and you can igno re DH ADD DL.

promptlen mov rsi.4ch INT 21h end %include "asm_io. print the prompt for the user mov rdx.inc" %macro prologue 0 push rbp mov rbp.0 quotient db "The quotient of cube/100 is: ".If here we are done Mov Ah. read the integer mov rdi.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 .0 promptlen equ $-prompt cube db "The cube of the number is: ".rodata prompt db "Enter a number: ".0 cube25 db "The value of cube of the number times 25 is: ". dword input call read_int .JMP New_number Label_A: .0 section . dword prompt push 0x1 pop rdi mov rax.0 negation db "The negation of the remainder is: ".rdi syscall .0 square db "The square of the number is: ".text global main main: prologue .bss input resd 1 section .0 remainder db "The remainder of cube/100 is: ".

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

OFFSET msg .Define macro 'print' MOV AH.'The cube of this number is: $' exit DB 0dh. data MOV DS. The nu mber is first save in a memory variable 'num' and then used twice to obtain the cube of that number. AX print MACRO msg . The hexadecimal to decimal conversion algorithm is the same that I've used in other sample programs.If the number entered is 1 .Saves AX.Prints string 'msg1' .'0' PUSH AX print msg2 POP AX CMP AL.01h INT 21h SUB AL.COntent of AX .Prints the string whose MOV DX..starting offset is stored INT 21h ENDM print msg1 MOV AH.in DX register .Segment Registers .1 .0ah.This program is nothing but repetitive multiplication of a single number. data SEGMENT msg1 DB 'Enter a number: $' msg2 DB 0dh.0ah.09h .'press any key to exit..$' num DB ? ENDS code SEGMENT start: MOV AX.Boiler plate code to specify .Asks user for a digit . prints string .Calculates the binary value .'msg2' then retrieves the .

AL MUL num MUL num MOV BX.02h INT 21h LOOP disp JMP xit skip: MOV DL.COntinues looping until .02h INT 21h disp: POP DX MOV AH.AL ADD DL.Divide DX-AX by BX (10) .Brings final quotient .Set entry point and stop the assembler.10 MOV CX.0 DIV BX ADD DL.JBE skip MOV num.in DL.'0' PUSH DX INC CL CMP AX.'0' MOV AH. 4c00h INT 21h ENDS END start .Retrieves remainder .If number entered is a .Prints the character .10 JGE hex2dec MOV DL.Make square of 'num' .0 or a 1.or 0.Loads divisor .Clears counter . .Prints string 'exit' .Make cube of 'num' .pressed by user .02h INT 21h xit: print exit MOV AH.Saves number in 'num' .skips the calculation .'0' MOV AH. 1 INT 21h MOV AX.gets the ASCII and .Clears DX .Exit to operating system. just prints .Increases count .Gets ASCII value .Waits for a key to be .quotient is >=10 .AL ADD DL.and Prints it .Saves value on stack .0 hex2dec: MOV DX. .the character and exits .

30h cmp al.mess1 mov ah.3 int 10h 10.mess2 mov ah. jmp start mess1 db mess2 db mess3 db mess4 db sum db 0 start: mov ax.0 jl start cmp al.13.al lea dx.9 .13.1 int 21h sub al.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.9 jg start mov bl.1 int 21h sub al."Enter 1st value: $" 10."Total sum value: $" 10."Do you want ot continue (y/n): $" lea dx."Enter 2nd value: $" 10.0 jl start cmp al.13.30h cmp al.9 int 21h mov ah.13.9 int 21h mov ah.

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

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

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

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

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

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

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

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

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

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

cx mov ax. "Enter a value (n) [0-9 only]: $" mess2 db 10.2 int 21h lea dx.bl jg next mov dl.9 int 21h inc bh jmp display next: dec bl inc al mov bh.13.13.space mov ah.1 mov bh.jmp start mess1 db 10.13.counter mov bl.13.mov cx. "Do you want to continue (y/n): $" mess3 db 10.mess1 mov ah.9 int 21h .lincar mov ah.'*' mov ah. "$" value db 0 counter db 0 start: xor ax.9 int 21h loop display jmp quit zero: lea dx.bx xor cx.9 int 21h quit: int 20h a.ax xor bx.counter lea dx.value mov al. mess3 mov ah.0 display: cmp bh. "There is nothing to display $" space db " $" lincar db 10.3 int 10h lea dx.ax mov counter.

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

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

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

00 .offset res mov ah.09h int 21h mov ax.total call hex2asc mov dx.mov ah.4c00h int 21h main endp readinteger proc near mov bx.'y' je start_bridge cmp al.1 int 21h cmp al.'n' je quit jmp continue quit: int 20h ############################## mov al.

0Ah jge rpt1 add al.'0' push dx inc cx cmp ax.00h mov bx.01h int 21h sub al.mov bl.'0' mul bl mov num1.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.al mov ah.0Ah rpt1: mov dx.'0' mov [si].10 mov ah.'0' mov num2.'$' mov [si]. In case the two strings ar e identical the .al rpt2: pop ax inc si mov [si].al loop rpt2 inc si mov al. As sume that the strings are stored one after the other in the memory.al ret readinteger endp hex2asc proc near push ax push bx push cx push dx push si mov cx.00 div bx add dl.01h int 21h sub al.

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

00 lea si. Prg(submul.buf cou1: mov al.msg6 mov ah.msg5 mov ah.bx check:mov al.00 mov cx.[si] inc si cmp al.revbuf mov cx.asm) Title program to multiply two numbers passed as parameter dosseg . You must write and run the appropriate main program that calls the subroutine.mov cx.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.buf lea di.al jne pal inc di inc si loop check lea dx.09h int 21h jmp ou pal:lea dx.24h je dis inc bx jmp cou1 dis: lea si.[si] cmp [di].

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