I want to print out the even charachters of a string. So far I got this code (this code print out the whole string):
.MODEL SMALL
.STACK
.DATA
adat DB "Test test",0
.CODE
main proc
MOV AX, DGROUP
MOV DS, AX
LEA BX, adat
new:
MOV DL, [BX]
OR DL, DL
CALL WRITE_CHAR
JZ stop
INC BX
JMP new
stop:
MOV AH,4Ch ;Kilépés
INT 21h
main endp
write_char proc
PUSH AX
MOV AH, 2
INT 21h
POP AX
RET
write_char endp
END main
So far I have been able to get there. I've tried a few things before, but they didn't work.
CodePudding user response:
OR DL, DL CALL WRITE_CHAR JZ stop
By the time the CALL WRITE_CHAR
returns, the flags that were defined from the OR DL, DL
instruction will have vanished! So it's possible the loop doesn't run the required number of times.
What are the even characters in a string ?
- Is it the characters that reside on an even offset within the string?
main proc
mov ax, DGROUP
mov ds, ax
xor bx, bx ; (*)
jmp char
next:
inc bx ; The sought for EVEN offset just became ODD here ...
test bx, 1
jz char ; ... so using opposite condition to skip
mov ah, 02h ; DOS.WriteChar
int 21h
char:
mov dl, adat[bx]
test dl, dl
jnz next
mov ax, 4C00h ; DOS.Terminate
int 21h
main endp
- Is it the characters at the 2nd, 4th, 6th, ... place in the string, so at odd offsets?
main proc
mov ax, DGROUP
mov ds, ax
xor bx, bx ; (*)
jmp char
next:
inc bx ; The sought for ODD offset just became EVEN here ...
test bx, 1
jnz char ; ... so using opposite condition to skip
mov ah, 02h ; DOS.WriteChar
int 21h
char:
mov dl, adat[bx]
test dl, dl
jnz next
mov ax, 4C00h ; DOS.Terminate
int 21h
main endp
- Or is it the characters whose ASCII code is an even number?
main proc
mov ax, DGROUP
mov ds, ax
xor bx, bx
jmp char
next:
inc bx
test dl, 1 ; Testing ASCII
jnz char ; Skip on ODD code
mov ah, 02h ; DOS.WriteChar
int 21h
char:
mov dl, adat[bx]
test dl, dl
jnz next
mov ax, 4C00h ; DOS.Terminate
int 21h
main endp
(*) Instead of using an extra register to test for even-ness, we can work from the address register by which we address the string.
In these code snippets, repeating the unconditional jump was avoided by placing the test for string termination near the bottom of the loop and executing a one-time-only jump to it.