Sie sind auf Seite 1von 26

Izzat awwad 1172815

Code of python:

from textwrap import wrap

import os

from typing import TextIO

print("Choose your option:\n1) Read the input from keyboard.\n2) Reat the input from a text
file.")

choice = int(input())

while choice != 1 and choice != 2:

choice = int(input("Please enter a valid input, or 9 to exit:\n"))

if choice == 9:

exit(0)

if choice == 1:

hexa = str(input("Enter Data in the format: ADDRESS:DATA ADDRESS:DATA ..: \n"))

elif choice == 2:

exist = False

filename = str(input("Enter file name: \n"))

while exist == False:

try:

with open(filename, "r") as f:

file_content = f.read()

while(file_content.endswith("\n")):

file_content = file_content[0:-1]

hexa = file_content.replace("\n"," ")

exist = True

except:

print("file doesn't exist")


filename = str(input("Enter file name: \n"))

each_address = hexa.split(" ")

data = ""

startAddress = ""

numberofbytes = 0

checksum = ""

sum = 0

sum1 = 0

for i in range(len(each_address)):

split = each_address[i].split(":")

address = split[0]

if i == 0:

startAddress = address

ADDRESSdata = split[1]

if len(ADDRESSdata)%2 != 0:

ADDRESSdata = "0" + ADDRESSdata

data = data+str(ADDRESSdata)

numberofbytes += int(len(ADDRESSdata)*0.5)

String = ""

len1 = 4 - len(startAddress)

startAddress = (len1*"0") + str(startAddress)

string = str(hex(numberofbytes))[2:]

len2 = 2-len(string)
string = (len2*"0") + string

sum = 0

sum = int(numberofbytes) + int(startAddress[0:2]) + int(startAddress[2:])

data1 = wrap(data, 2)

for i in range(len(data1)):

sum1 += int(data1[i], 16)

sum1 = sum1 + sum

xoring = "F" * len(str(sum1))

sum1 = sum1 ^ int(xoring, 16)

sum1 += 1

checksum = str(hex(sum1))[-2:]

hexformat = ":" + str(string).upper() + str(startAddress).upper() + "00" + str(data).upper() +


str(checksum).upper() + "\n" + ":00000001FF"

print(hexformat)

filename = "out.txt"

try:

os.remove("out.txt")
except:

pass

with open("out.txt", "x") as fp:

with open("out.txt","w") as fw:

fw.write(hexformat)
CODFE OF ASSEMBLY :

; multi-segment executable file template.

data segment

intro db "Choose one of the options: ",10,13

op1 db "1- Read Input from the keyboard.",10,13

op2 db "2- Read Input from a text file.",10,13,"$"

op db 0

error db 10,13,"Wrong data, You must choose 1 or 2: ",10,13,"$"

error1 db 10,13,"File doesn't exist, enter an existing file name",10,13,"$"

error2 db 10,13,"Error happened while reading the input !$"

line db 10,13,"$"

BUFFER db 255 dup ("$")

address db 100 dup ("$")

checksum db 2 dup ("$")

sum dw 0

bytes db 0

ask1 db 10,13,"Please enter your data in one line like: address:data address:data ..",10,13,"$"

ask2 db 10,13,"Please enter the data file name: $"

filename db 30 dup (0)

counter db 0

counter2 db 3 dup ('$')

counter3 db 4 dup ('$')

bytesnum db 2 dup ('0')


counter1 db 4 dup('0')

Datamemory db 20 dup ("$")

succeed db 10,13,"File written successfully.$"

file_handler dw 0

eom db 13,10,":00000001FF$"

hexformat db 100 dup (0)

printf macro string

mov ah, 09h

mov dx, offset string

int 21h

endm

scanf macro string

mov ah, 0Ah

mov dx, offset string

int 21h

endm

getc macro option

mov ah, 01h

int 21h

mov option, al

endm

printc macro char

mov ah, 02h

mov dl, char

int 21h
endm

readfilename macro

getchars:

mov ah, 01h

int 21h

cmp al, 0dh

jz ready

mov [di], al

inc di

jmp getchars

ready:

endm

outp db "out.txt",0

ends

stack segment

dw 128 dup(0)

ends

code segment

start:

; set segment registers:

mov ax, data

mov ds, ax
mov es, ax

printf intro

while:

getc op

cmp op, '1'

jz Keyboard

cmp op, '2'

jz File

printf error

jmp while

Keyboard:

printf ask1

scanf BUFFER

mov si, offset BUFFER

add si, 1

mov di,offset address

mov bx, offset datamemory

finished:
inc si

readAddress:

cmp [si], ':'

jz readData

mov al,[si]

mov [di], al

inc si

inc di

jmp readAddress

readData:

mov al, ','

mov [di], al

inc di

readData1:

inc si

cmp [si], ' '

jz finished

cmp [si], '$'

jz cont

mov al, [si]

mov [bx], al

inc bx

jmp readData1

File:
printf ask2

mov di, offset filename

readfilename

mov ax, 3d02h

mov dx, offset filename

int 21h

mov file_handler,ax

jc fileError

mov ah, 3fh

mov bx,file_handler

mov cx, 255

mov dx, offset BUFFER

int 21h

mov ah,3eh

mov bx,file_handler

int 21h

; filtering

mov si, offset BUFFER

mov di,offset address

mov bx, offset datamemory

jmp readAddress1
finished1:

add si, 2

readAddress1:

cmp [si], ':'

jz readData2

mov al,[si]

mov [di], al

inc si

inc di

jmp readAddress1

readData2:

mov al, ','

mov [di], al

inc di

readData11:

inc si

cmp [si], 13

jz finished1

cmp [si], 10

jz finished1

cmp [si], '$'

jz cont

mov al, [si]

mov [bx], al

inc bx
jmp readData11

; filtering

cont:

mov al,'$'

dec di

mov [di], al

cmp op, '2'

je cont1

dec bx

mov [bx], al

cont1:

mov si, offset Datamemory

mov cx, 0

getnumofbytes:

cmp [si], '$'

jz bytesdone

inc cx

inc si

jmp getnumofbytes

bytesdone:
mov ax, cx

mov cl, 2

div cl

mov bytes, al

mov ah,0

add sum, ax

mov si, offset address

num:

cmp [si], ','

jz gotnum

inc counter

inc si

jmp num

gotnum:

mov al, 4

mov si, offset address

sub al,counter

mov di, offset counter1

addtoaddress:

cmp al, 0

je cont2

mov bl, '0'

mov [di], bl

inc di
dec al

jmp addtoaddress

cont2:

cmp [si], ','

jz cont3

mov bl, [si]

mov [di], bl

inc di

inc si

jmp cont2

cont3:

mov si, offset counter1

mov cl, 4

mov di, offset counter2

cont4:

mov bl, [si]

mov [di], bl

inc si

inc di

dec cl

cmp cl, 2

jz func

cmp cl, 0

jz func

jmp cont4
func:

mov bx, offset counter2

call turn

add sum, dx

mov di,offset counter2

cmp cl, 2

je cont4

cont5:

mov si, offset Datamemory

mov di, offset counter2

cont6:

cmp [si], '$'

jz cont7

mov al, [si]

mov [di], al

inc si

inc di

mov al, [si]

mov [di], al

mov bx, offset counter2

call turn
add sum, dx

mov di, offset counter2

inc si

jmp cont6

cont7:

mov di, offset counter2

mov si, offset checksum

mov ax, sum

xor ax, 0ffffh

add ax,0001h

mov ah,0

mov dl, 10h

div dl

mov [di], ah

mov ah,0

div dl

inc di

mov [di], ah

mov bx, offset counter2

inc bx

call turn2

mov [si], al

dec bx

call turn2

inc si

mov [si], al
mov si, offset bytesnum

mov di, offset counter2

mov ah, 0

mov al, bytes

mov dl, 10h

div dl

mov [di], ah

mov ah,0

div dl

inc di

mov [di], ah

mov bx, offset counter2

inc bx

call turn2

mov [si], al

dec bx

call turn2

inc si

mov [si], al

tohex:

mov cx, 0

mov si, offset bytesnum

mov di, offset hexformat

mov bl, ':'

mov [di], bl

inc di

printf line
mov al, 0

mov counter, al

started:

cmp counter, 5

je cc

cmp [si], '$'

jz tochecksum

mov bl, [si]

mov [di], bl

inc cl

inc si

inc di

inc counter

jmp started

cc:

mov bl, '0'

mov [di], bl

inc di

mov [di], bl

inc counter

inc di

jmp started

tochecksum:

mov si, offset checksum

mov bl, [si]


mov [di], bl

inc cl

inc si

inc di

mov bl, [si]

mov [di], bl

inc di

mov si, offset eom

eof:

mov bl, [si]

mov [di], bl

inc cl

inc si

inc di

cmp bl, '$'

jz ended

jmp eof

ended:

inc cl

mov counter, cl

printf hexformat

mov al, 00h

mov ah, 3ch

mov dx, offset outp


mov cx, 0

int 21h

mov file_handler, ax

inc counter

inc counter

mov ah, 40h

mov bx, file_handler

mov cx, 0

mov cl, counter

mov dx, offset hexformat

int 21h

mov ah, 3eh

mov bx, file_handler

int 21h

jmp TheEnd

fileError:

mov si, offset filename

mov al,'$'

mov cx, 30d

deletion:

mov [si], al

inc si

loop deletion
printf error1

jmp File

finish:

readingerror:

printf error2

TheEnd:

printf succeed

mov ax, 4c00h ; exit to operating system.

int 21h

ends

turn proc

mov ah, 0

mov al, [bx]

cmp al, '0'

jl readingerror

cmp al, '9'

jg l1

sub al, 30h

mov dl, 10h

mul dl

jmp sec

l1:
cmp al, 'A'

jl readingerror

cmp al, 'F'

jg l2

sub al, 37h

mov dl, 10h

mul dl

jmp sec

l2:

cmp al, 'a'

jl readingerror

cmp al, 'f'

jg readingerror

sub al, 57h

mov dl, 10h

mul dl

sec:

inc bx

mov dx,ax

mov al, [bx]

cmp al, '0'

jl readingerror

cmp al, '9'

jg l3

sub al, 30h

mov ah, 0

add dx, ax

jmp return

l3:
cmp al, 'A'

jl readingerror

cmp al, 'F'

jg l4

sub al, 37h

mov ah, 0

add dx, ax

jmp return

l4:

cmp al, 'a'

jl readingerror

cmp al, 'f'

jg readingerror

sub al, 57h

mov ah, 0

add dx, ax

return:

ret

endp

turn2 proc

mov ah, 0

mov al, [bx]

cmp al, 0

jl readingerror
cmp al, 9

jg l5

add al, 30h

jmp return1

l5:

cmp al, 0Ah

jl readingerror

cmp al, 0Fh

jg l6

add al, 37h

jmp return1

l6:

cmp al, 0ah

jl readingerror

cmp al, 0fh

jg readingerror

add al, 57h

return1:

ret

endp

end start ; set entry point and stop the assembler.

Das könnte Ihnen auch gefallen