我必须编写一个程序,它从文件中读取字符,更改每个字符中的位,并将更改写入TASM中的新文件。
我编写了一个程序,它从文件中读取字符并将它们写入新文件,但我不知道如何更改字符串中的位。
例如,这里将是我的chars文件:
a // 01100001
b // 01100010
c // 01100011
d // 01100100
因此,如果我们将第一位和第二位更改为1,则输出应为:
c // 01100011
c // 01100011
c // 01100011
g // 01100111
如何更改char中的位
这是我的代码:
.model small
ASSUME CS:code, DS:data, SS:stack
stack segment word stack 'STACK'
dw 400h dup (00)
stack ends
data segment para public 'DATA'
ourFile:
dw 0FFFh
byteInFile:
db 00, 00, ' $'
handle:
dw ?
outputTextFile:
db 'TEXTOUT.CSV',0
inputTextFile:
db 'TEXT.CSV',0
data ends
writeToFile macro byte
push ax
push bx
push cx
push dx
mov ah, 40h
mov bx, word ptr[handle]
mov cx, 1
int 21h
pop dx
pop cx
pop bx
pop ax
endm
LOCALS @@
code segment para public 'CODE'
openFile proc near
push ax
push dx
mov ah, 3Dh
mov al, 00h
int 21h
jc @@end
mov word ptr ourFile, ax
@@end:
pop dx
pop ax
ret
openFile endp
closeFile proc near
push ax
push bx
mov ah, 3Eh
int 21h
@@end:
pop dx
pop ax
ret
closeFile endp
readLinesInFile proc near
push ax
push dx
push bx
push cx
push si
push di
mov si, dx
mov di, 0
@@repeat:
mov cx, 01
mov ah, 3Fh
int 21h
jc @@end
cmp ax, 00
je @@end
// here we have to change chars' bit?
// outputting chars
push ax
push dx
mov dl, byte ptr[si]
mov ah, 02h
int 21h
pop dx
pop ax
writeToFile byte ptr[si]
jmp @@repeat
@@end:
pop di
pop si
pop cx
pop bx
pop dx
pop ax
ret
readLinesInFile endp
begin:
mov ax, seg data
mov ds, ax
mov si, offset outputTextFile
mov cl, [ si ]
mov ch, 0
inc cx
add si, cx
mov al, 0
mov [ si ], al
; We create file
mov ah, 3ch
mov cx, 0
mov dx, offset outputTextFile
int 21h
; save handle
mov word ptr[handle], ax
; We open file
mov dx, offset inputTextFile
call openFile
mov bx, word ptr ourFile
mov dx, offset byteInFile
call readLinesInFile
; We close file
mov bx, word ptr ourFile
call closeFile
jmp @@Ok
mov ah, 3Eh
mov bx, word ptr[handle]
int 21h
@@Ok:
mov ah, 4ch
int 21h
code ends
end begin
答案 0 :(得分:1)
您可以使用指令AND
将位设置为0,使用指令OR
将位设置为1,例如:
BIT 7 BIT 0
▼ ▼
mov al, '9' ;◄■■ AL = 00111001 (57).
将最高位(7)设置为1并保持其他位不变(使用OR
0将保留位不变):
BIT 7
▼ ▼
or al, 10000000b ;◄■■ AL = 10111001
现在将最低位(0)设置为0并保持其他位置不变(AND
1s保留位不变):
BIT 0
▼ ▼
and al, 11111110b ;◄■■ AL = 10111000
注意每条指令是如何与另一条指令相反的,OR
设置为1,AND
设置为0,OR
使用0的掩码,AND
使用面具1s。
您询问如何一次更改第一个(0)和第二个(1)位:
▼▼
mov al, 'a' ;◄■■ AL = 01100001
or al, 00000011b ;◄■■ AL = 01100011
▲▲ ▲▲
再次注意OR
如何使用0的掩码来保持其他位不变。还要注意" b"在二进制数字的末尾。
最后,在你的代码中,你一次只从文件读取一个字节,这个字节存储在变量byteInFile
中,所以:
// here we have to change chars' bit?
or byteInFile, 00000011b ;◄■■ SET BITS 0 AND 1, LEAVE THE REST UNCHANGED.