display different attributes of character in a string in assembly - string

I would like to know if its possible to change the attributes of each character in a string?
For example in the string "hello" the character 'h' will have a different color, the same with 'e' and so on.
I use AH, 06 to call every character in the string. Then use AH, 09 INT 10h to change the attribute of each character but then its not working.
I want to know how can AL (in AH, 09) get the DL (AH, 06) and change the attribute of every character.
is this possible?
thanks for the help
here's my code
`
.DATA
hello DB "hello$"
.CODE
START:
MOV AX, #DATA
MOV DS, AX
LEA SI, hello
MOV CX, 0005H
E: MOV AH, 06H
MOV DL, [SI]
INC SI
;INT 21H
LOOP E
MOV CX, 0005H
MOV AH, 09H
MOV AL, [SI]
INC SI
MOV BL, 0001H
H: INT 10H
INC BL
LOOP H
MOV AX, 4C00H
INT 21H
END START `

First off this code is not Windows, it's 16-bit DOS code that calls the BIOS video routines.
The main body calls INT 10H, the documentation for that call is here: http://en.wikipedia.org/wiki/INT_10H
For int 10H,9 this is the relevant line:
Write character and attribute at cursor position AH=09h AL = Character, BH = Page Number, BL = Color, CX = Number of times to print character
This means there are a couple of errors you're making:
You cannot use CX as a loop counter, because it's a parameter for the call.
The color goes into bl so don't hardcode that.
bh is the page number, but you're not setting bh anywhere.
Increasing bl and then later resetting it back to 1 will obviously fix it at 1.
You've already increased si through the whole length of the string in the first loop, so in the second loop you're reading past the end of the string (a classic buffer overrun). At the start of the second loop you need to repeat the lea.
Ever since the 80486 using loop is a bad idea because it's much slower than the equivalent sub reg,1; jnz label; besides loop is tied to the cx register which is awkward.
If you're using bios int calls speed is hardly a requirement, but that's not the point.
If you want to learn x86 assembly you should also learn not to use the old cisc instructions on new processors.

Related

why does string declaring matter in assmebly 8086?

i wanted to write a program that iterate through a string and calculate how many times the letter "C" occured, ( i increase the value of ````BX``` ever time there is a ltter C)
now the code works correctly like this
LEA DI, STRING
MOV CX, 6h
FOR:
CMP CX, 0
JE END
cmp [DI], "C"
jnz siPasDeC
inc BX
siPasDeC:
INC DI
dec cx
jmp FOR
END:
ret
hlt
STRING DB "CCKCCD"
but when i write STRING DB "CCKCCD" in the first line the program does sort of an infinite loop. can you tell me why this happened?
PS: is is a best practice to write "ret" and "hlt" every time; because i didn't see people writing it online, but in college we are forced to write it.

How do i reverse a string on emu8086 assembly language [duplicate]

I have to do a simple calculator in assembly using EMU8086, but every time I try to launch it EMU8086 gives this error:
INT 21h, AH=09h -
address: 170B5
byte 24h not found after 2000 bytes.
; correct example of INT 21h/9h:
mov dx, offset msg
mov ah, 9
int 21h
ret
msg db "Hello$"
I checked the other stuff, but there were no mistakes:
data segment
choice db ?
snum1 db 4 dup(?)
snum2 db 4 dup(?)
sres db 4 dup(?)
num1 db ?
num2 db ?
res db ?
;;menu1 db "Chose a function to procced", 10, 13, "Add [+]", 10, 13, "Sub [-]", 10, 13
;;menu2 db "Mul [*]", 10, 13, "Div [/]", 10, 13, "Mod [%]", 10, 13, "Pow [^]", 10, 13, "Exit [x]$"
messStr db "Enter Your Choice:",10,13,"",10,13,"Add --> +",10,13,"Sub --> -",10,13,"Mul --> *",10,13,"Div --> /",10,13,"Mod --> %",10,13,"Pow --> ^",10,13,"Exit --> X",10,13,"$"
msg1 db "Enter first number$"
msg2 db "Enter second number$"
msg3 db "Press any key to procced$"
msg4 db "The result is $"
ends
stack segment
dw 128 dup(0)
ends
code segment
assume cs:code, ds:data, ss:stack
newline proc ;; new line
push ax
push dx
mov ah, 2
mov DL, 10
int 21h
mov ah, 2
mov DL, 13
int 21h
pop dx
pop ax
ret
endp
printstr proc ;; print string
push BP
mov BP, SP
push dx
push ax
mov dx, [BP+4]
mov ah, 9
int 21h
pop ax
pop dx
pop BP
ret 2
endp
inputstr proc ;; collect input
push BP
mov BP, SP
push bx
push ax
mov bx, [BP+4]
k1:
mov ah, 1
int 21h
cmp al, 13
je sofk
mov [bx], al
inc bx
jmp k1
sofk:
mov byte ptr [bx], '$'
pop ax
pop bx
pop BP
ret 2
endp
getNums proc ;; get the numbers
call newline
push offset msg1
call printstr
call newline
push offset snum1
call inputstr
call newline
push offset msg2
call printstr
call newline
push offset snum2
call inputstr
ret
endp
start:
mov ax, data
mov ds, ax
mov ax, stack
mov ss, ax
;; print the main menu
call newline
push offset msg4
call printstr
;; collect the input
call newline
mov bx, offset choice
mov ah, 1
int 21h
mov [bx], al
;; check it
mov al, choice
cmp al, '+'
jne cexit
call getNums
jmp cont
cexit:
cmp al, 'x'
je cend
cont:
;; pause before going to the main menu
call newline
push offset msg3
call printstr
mov bx, offset choice
mov ah, 1
int 21h
call newline
call newline
call newline
jmp start
cend:
mov ax, 4c00h
int 21h
ends
end start
I cut most of the code segment because it wasn't important here.
After experimenting with the code I found that the problem was related to the lengths of the messages in the data segment. menu1 & menu2 were too long and any message after them can't be printed (msg1 & msg2 are printed, but nothing after them). I checked if I should merge menu1 & menu2, but it didn't help out. Please help me find out what is wrong with it.
The error message means you use int 21h / AH=09h on a string that didn't end with a $ (ASCII 24h). The system-call handler checked 2000 bytes without finding one.
Often, that means your code or data is buggy, e.g. in a fixed string you forgot a $ at the end, or if copying bytes into a buffer then you maybe overwrote or never stored a '$' in the first place.
But in this case, it appears that EMU8086 has a bug assembling push offset msg4. (In a way that truncates the 00B5h 16-bit address to 8-bit, and sign-extends back to 16, creating a wrong pointer that points past where any $ characters are in your data.)
Based on the error message below I know you are using EMU8086 as your development environment.
INT 21h, AH=09h -
address: 170B5
byte 24h not found after 2000 bytes.
; correct example of INT 21h/9h:
mov dx, offset msg
mov ah, 9
int 21h
ret
msg db "Hello$"
I'm no expert on EMU8086 by any stretch of the imagination. I do know why your offsets don't work. I can't tell you if there is a proper way to resolve this, or if it's an EMU8086 bug. Someone with a better background on this emulator would know.
You have created a data segment with some variables. It seems okay to me (but I may be missing something). I decided to load up EMU8086 to actually try this code. It assembled without error. Using the debugger I single stepped to the push offset msg1 line near the beginning of the program. I knew right away from the instruction encoding what was going on. This is the decoded instruction I saw:
It shows the instruction was encoded as push 0b5h where 0b5h is the offset. The trouble is that it is encoded as a push imm8 . The two highlighted bytes on the left hand pane show it was encoded with these bytes:
6A B5
If you review an instruction set reference you'll find the encodings for PUSH instruction encoded with 6A is listed as:
Opcode* Instruction Op/En 64-Bit Mode Compat/Leg Mode Description
6A ib PUSH imm8 I Valid Valid Push imm8.
You may say that B5 fits within a byte (imm8) so what is the problem? The smallest value that can be pushed onto the stack with push in 16-bit mode is a 16-bit word. Since a byte is smaller than a word, the processor takes the byte and sign extends it to make a 16-bit value. The instruction set reference actually says this:
If the source operand is an immediate of size less than the operand size, a sign-extended value is pushed on the stack
B5 is binary 10110101 . The sign bit is the left most bit. Since it is 1 the upper 8 bits placed onto the stack will be 11111111b (FF). If the sign bit is 0 then then 00000000b is placed in the upper 8 bits. The emulator didn't place 00B5 onto the stack, it placed FFB5. That is incorrect! This can be confirmed if I step through the push 0b5h instruction and review the stack. This is what I saw:
Observe that the value placed on the stack is FFB5. I could not find an appropriate syntax (even using the word modifier) to force EMU8086 to encode this as push imm16. A push imm16 would be able to encode the entire word as push 00b5 which would work.
Two things you can do. You can place 256 bytes of dummy data in your data segment like this:
data segment
db 256 dup(?)
choice db ?
... rest of data
Why does this work? Every variable defined after the dummy data will be an offset that can't be represented in a single byte. Because of this EMU8086 is forced to encode push offset msg1 as a word push.
The cleaner solution is to use the LEA instruction. This is the load effective address instruction. It takes a memory operand and computes the address (in this case the offset relative to the data segment). You can replace all your code that uses offset with something like:
lea ax, [msg1]
push ax
AX can be any of the general purpose 16-bit registers. Once in a register, push the 16-bit register onto the stack.
Someone may have a better solution for this, or know a way to resolve this. If so please feel free to comment.
Given the information above, you may ask why did it seem to work when you moved the data around? The reason is that the way you reorganized all the strings (placing the long one last) caused all the variables to start with offsets that were less than < 128. Because of this the PUSH of an 8-bit immediate offset sign extended a 0 in the top bits when placed on the stack. The offsets would be correct. Once the offsets are >= 128 (and < 256) the sign bit is 1 and the value placed on the stack sign will have an upper 8 bits of 1 rather than 0.
There are other bugs in your program, I'm concentrating on the issue directly related to the error you are receiving.
I reviewed your code and concentrated on the following sequence of instructions:
mov bx, offset choice ; here you set BX to the address of 'choice'
mov ah, 1
int 21h ; here you 'READ CHARACTER FROM STANDARD INPUT, WITH ECHO'
mov [bx], al ; because INT 21h does preserve BX, you are writing back the result of the interrupt call (AL) back to the memory location at BX, which is named 'choice'
;; check it
mov al, choice ; HERE you are moving a BYTE variable named 'choice' to AL, overwriting the result of the last INT 21h call
cmp al, '+' ; ... and compare this variable to the ASCII value of '+'
jne cexit ; if this variable is unequal to '+' you jump to 'cexit'
call getNums ; otherwise you try to get another number from the input/STANDARD CONSOLE
So your sequence
mov bx, offset choice ; here you set BX to the address of 'choice'
...
mov [bx], al ; because INT 21h does preserve BX, you ...
...
mov al, choice
essentially means, that you are setting BX to the address of 'choice', then setting 'choice'([BX]) to AL and copying it back to AL.
This is redundant.
After that, you compare that char to '+' and...
if that char equals to '+', you get the next char with call getNums and then continue with cont:.
if that char does not equal to '+', you compare it to 'x', the exit-char. If it's not 'x', you fall through to cont:
No error here.
So your problem with menu1 and menu2 may stem from some escape characters included in your strings like %,/,\. For example, % is a MACRO character in some assemblers which may create problems.
simple solution is that your strings should always end in '$'
change DUP(?) to DUP('$') and all other strings end with ,'$'

8086 Write a program that takes one string from the input and inserts white space between every two letters

8086 Write a program that takes one string from the input and inserts white space between every two letters
using nasm and MSDOS
i have do following code but it is not working
start:
mov ax,data
mov ds, ax
mov es, ax
mov cx, size;cx will contain size,we need it to
; check if we have got 10 inputs from key board
lea dx, Enter_string;it will display a text on screen to enter text
mov ah, 9
int 21h
call get_string;input string from keyboard
mov ax, 4ch;terminating
int 21h
get_string:
mov si, 0; si will be used as index
mov bx, offset string
get_char:
mov ah, 1; get a char from keyboard
int 21h
mov [bx][si], al; saving input in string
inc si
cmp si,cx;if si=7 than, no need to take more input
jne get_char
ret
. You should prefer the simplicity of .COM programs while learning. They already start with all the segment registers pointing to the program.
. The DOS exit function expects the function number in AH.
. On NASM you don't use mov bx, offset string. Just write mov bx, string.
. It's easy to combine the tasks of inputting a string and interjecting space characters. See below code:
org 256 ;.COM programs have CS=DS=ES=SS
start:
mov cx, size ;cx will contain size,we need it to
; check if we have got 10 inputs from key board
mov dx, Enter_string ;it will display a text on screen to enter text
mov ah, 9
int 21h
call get_string ;input string from keyboard
mov ax, 4C00h ;terminating
int 21h
get_string:
push cx
mov si, 0 ; si will be used as index
get_char:
mov ah, 1 ; get a char from keyboard
int 21h
mov ah, " "
mov [string+si], ax ; saving input in string PLUS THE SPACE CHARACTER
add si, 2
dec cx ;if si=7 than, no need to take more input
jnz get_char
pop cx
ret
Remember that you don't actually need the last space character. Just overwrite it with the string terminator that you normally would add to this string!

Manipulate string in assembly x86 (mov and print to screen)

I'm working on a larger project but I'm stuck with string manipulation. My assembly file includes math coprocessor operations (it starts the coprocessor with "FINIT") but I don't think it should be interfering at all.
Basically, I have some strings that are 50 bytes long each:
$s db 50 dup (?), '$'
_cte_14 db "hello world", '$', 39 dup (?)
I need to assign the value stored in variable "_cte_14" to variable "$s"
I attempted to use a register to temporarily store the value, like this:
mov cx, _cte_14
mov $s, cx
but I get the "operand types do not match" error.
Since I know the AX, BX, CX, DX registers only hold 16 bits, I thought maybe I need to work with the memory address of the first string character, so I tried:
mov bx, offset _cte_14
mov $s, bx
but the same error shows up.
I'm using TASM to compile for an x86 processor. What would be the right way to accomplish this?
Thanks a lot in advance.
Example for to copy the characters in a loop:
s db 51 dup ('$')
_cte_14 db "hello world"
len = ($ - _cte_14) ; (current location - offset _cte_14)
40 dup ('$')
mov si, offset _cte_14 ; get source offset
mov di, offset s ; get destination offset
mov cl, len ; length of the string
P1:
mov al, [si] ; get byte from DS:SI
mov [di], al ; store byte to DS:DI
dec cl ; decrease counter, set zero flag if zero
jnz P1 ; jump if zero flag is not set
-- Variation with using a string instruction together with a repeat instruction prefix:
mov si, offset _cte_14
mov di, offset s
mov cx, len ; length of the string
cld ; clear direction flag
rep movsb ; copy from DS:SI to ES:DI, increase SI+DI, decrease CX, repeat CX times

Printing a string in assembly using no predefined function

I have to define a function in assembly that will allow me to loop through a string of declared bytes and print them using a BIOS interrupt. I'm in 16 bit real mode. This is an exercise on writing a little bootloader from a textbook, but it seems that it was only a draft and it's missing some stuff.
I have been given the following code:
org 0x7c00
mov bx, HELLO_MSG
call print_string
mov bx, GOODBYE_MSG
call print_string
jmp $ ;hang so we can see the message
%include "print_string.asm"
HELLO_MSG:
db 'Hello, World!', 0
GOODBYE_MSG:
db 'Goodbye!', 0
times 510 - ($ - $$) db 0
dw 0xaa55
My print_string.asm looks like this:
print_string:
pusha
mov ah, 0x0e
loop:
mov al, bl
cmp al, 0
je return
int 0x10
inc bx
jmp loop
return:
popa
ret
I have some idea of what I'm doing, but the book doesn't explain how to iterate through something. I know how to do it in C but this is my first time using assembly for something other than debugging C code. What happens when I boot through the emulator is that it prints out a couple of lines of gibberish and eventually hangs there for me to see my failure in all it's glory. Hahaha.
Well, it looks like it loads the address of the string into the BX register before calling the function.
The actual function looks like it is trying to loop through the string, using BX as a pointer and incrementing it (inc bx) until it hits the ASCII NUL at the end of the string (cmp al, 0; je return)...
...but something is wrong. The "mov al, bl" instruction does not look correct, because that would move the low 8 bits of the address into al to be compared for an ASCII NUL, which does not make a lot of sense. I think it should be something more like "mov al, [bx]"; i.e. move the byte referenced by the BX address into the AL register -- although it has been a long time since I've worked with assembly so I might not have the syntax correct.
Because of that bug, the 10h interrupt would also be printing random characters based on the address of the string rather than the contents of the string. That would explain the gibberish you're seeing.
I think the issue is you cannot count on the int preserving any of your registers, so you need to protect them. Plus, what Steven pointed out regarding loading of your string address:
; Print the string whose address is in `bx`, segment `ds`
; String is zero terminated
;
print_string:
pusha
loop:
mov al, [bx] ; load what `bx` points to
cmp al, 0
je return
push bx ; save bx
mov ah, 0x0e ; load this every time through the loop
; you don't know if `int` preserves it
int 0x10
pop bx ; restore bx
inc bx
jmp loop
return:
popa
ret

Resources