int 10h 13h bios string output not working - nasm

i am using nasm and this is my code :
org 0x7c00
bits 16
section .data
zeichen dw 'hello2'
section .text
mov ax,0x7c00
mov es,ax
mov bh,0
mov bp,zeichen
mov ah,13h
mov bl,00h
mov al,1
mov cx,6
mov dh,010h
mov dl,01h
int 10h
jmp $
times 510 - ($-$$) hlt
dw 0xaa55
it does put the cursor on the right position but it prints nothing.
i load this file with qemu-system-i386.
The int10 ah=13h is a string output and in register es:bp has to be the address of the string

For future reference, since i have been trying to get this working for a long time now, here is a working version!
org 0x7c00
bits 16
xor ax, ax
mov es, ax
xor bh, bh
mov bp, msg
mov ah, 0x13
mov bl, [foreground]
mov al, 1
mov cx, [msg_length]
mov dh, [msg_y]
mov dl, [msg_x]
int 0x10
hlt
foreground dw 0xa
msg db 'Beep Boop Meow'
msg_length dw $-msg
msg_x dw 5
msg_y dw 2
times 510 - ($-$$) db 0
dw 0xaa55
here is a version closest to original.
org 0x7c00
bits 16
; video page number.
mov bh, 0
; ES:BP is the pointer to string.
mov ax, 0x0
mov es, ax
mov bp, msg
; attribute(7 is light gray).
mov bl, 0x7
; write mode: character only, cursor moved.
mov al, 1
; string length, hardcoded.
mov cx, 6
; y coordinate
mov dh, 16
; x coordinate
mov dl, 1
; int 10, 13
mov ah, 0x13
int 0x10
; keep jumping until shutdown.
jmp $
msg dw 'hello2'
times 510 - ($-$$) db 0
dw 0xaa55

Related

Assembly 8086 TASM - 0Ah 21h remembers last entry

So I'm writing a uni assignment - a program that subtracts two entered decimals (max 10 characters ea). The first iteration works as intended. However, when I restart the program, for some reason the second operand is remembered.
The prompt to enter it does come up, but is then skipped as if I've entered something already - the same thing I entered the first iteration, in fact.
The question is: why does it happen and how do I fix it? The first prompt works correctly.
The prompt is under INPUT_2:
.model small
.386
stack 100h
dataseg
inputMsg1 db 0Ah, 0Dh, 'Enter first operand', 0Ah, 0Dh, '$'
inputMsg2 db 0Ah, 0Dh, 'Enter second operand', 0Ah, 0Dh, '$'
inputMax1 db 11
inputLen1 db ?
input1 db 12 dup(?)
input1Packd db 5 dup(0)
inputMax2 db 11
inputLen2 db ?
input2 db 12 dup(?)
input2Packd db 5 dup(0)
packMode db 0 ;Режим упаковки: 1 - первая цифра, 2 - вторая
resMsg db 0Ah, 0Dh, 'Result: $'
res db 9 dup(' '),'$'
retryMsg db 0Ah, 0Dh
db 'Press Any Key to continue, ESC to quit'
db '$'
errorMsg db 0Ah, 0Dh, 'Something went wrong. Try again$'
codeseg
START:
startupcode
jmp INPUT_1
INPUT_1_ERROR:
lea DX, errorMsg
mov AH, 09h
int 21h
INPUT_1:
lea DX, inputMsg1
mov AH, 09h
int 21h
lea DX, inputMax1
mov AH, 0Ah
int 21h
cmp inputLen1, 0
jz INPUT_1_ERROR
INPUT_1_PROCESS:
lea BX, input1
lea DX, input1Packd
xor CX, CX
mov CL, inputLen1
mov SI, CX
dec SI
mov DI, 4
INPUT_1_LOOP:
mov AL, [BX][SI]
cmp AL, '0'
jb INPUT_1_ERROR
cmp AL, '9'
ja INPUT_1_ERROR
and AL, 0Fh
mov AH, packMode
cmp AH, 0
jnz INPUT_1_PACK_SECOND
INPUT_1_PACK_FIRST:
inc AH
push BX
mov BX, DX
mov [BX][DI], AL
pop BX
jmp INPUT_1_PACK_FINISHED
INPUT_1_PACK_SECOND:
dec AH
shl AL, 4
push BX
mov BX, DX
or [BX][DI], AL
pop BX
dec DI
INPUT_1_PACK_FINISHED:
mov packMode, AH
dec SI
loop INPUT_1_LOOP
mov packMode, 0
jmp INPUT_2
INPUT_2_ERROR:
lea DX, errorMsg
mov AH, 09h
int 21h
INPUT_2:
lea DX, inputMsg2
mov AH, 09h
int 21h
lea DX, inputMax2
mov AH, 0Ah
int 21h
cmp inputLen2, 0
jz INPUT_2_ERROR
INPUT_2_PROCESS:
lea BX, input2
lea DX, input2Packd
xor CX, CX
mov CL, inputLen2
mov SI, CX
dec SI
mov DI, 4
INPUT_2_LOOP:
mov AL, [BX][SI]
cmp AL, '0'
jb INPUT_2_ERROR
cmp AL, '9'
ja INPUT_2_ERROR
and AL, 0Fh
mov AH, packMode
cmp AH, 0
jnz INPUT_2_PACK_SECOND
INPUT_2_PACK_FIRST:
inc AH
push BX
mov BX, DX
mov [BX][DI], AL
pop BX
jmp INPUT_2_PACK_FINISHED
INPUT_2_PACK_SECOND:
dec AH
shl AL, 4
push BX
mov BX, DX
or [BX][DI], AL
pop BX
dec DI
INPUT_2_PACK_FINISHED:
mov packMode, AH
dec SI
loop INPUT_2_LOOP
MATH_SETUP:
mov SI, 4
mov CX, 4
mov DI, 7
MATH:
lea BX, input1Packd
mov AL, [BX][SI]
lea BX, input2Packd
mov AH, [BX][SI]
sbb AL, AH
pushf
das
dec SI
mov AH, AL
lea BX, res
and AL, 0Fh
or AL, 30h
mov [BX][DI], AL
dec DI
shr AH, 4
or AH, 30h
mov [BX][DI], AH
dec DI
popf
loop MATH
lea BX, res
mov CX, 7
mov SI, 0
SHORTEN:
mov AL, [BX][SI]
cmp AL, '0'
jnz WRAPUP
inc SI
loop SHORTEN
WRAPUP:
push CX
lea DX, resMsg
mov AH, 09h
int 21h
lea DX, res
pop CX
cmp CX, 0
jz SKIP_SHORTEN
PRINT_SHORTEN:
add DX, 7
sub DX, CX
jmp FINISH_SHORTEN
SKIP_SHORTEN:
add DX, 6
FINISH_SHORTEN:
mov AH, 09h
int 21h
lea DX, retryMsg
mov AH, 09h
int 21h
mov AH, 01h
int 21h
cmp AL, 1Bh
jz QUIT
lea BX, input1Packd
mov DX, 1
BCD_CLEANUP:
mov DI, 0
mov CX, 5
BCD_CLEANUP_LOOP:
mov [BX][DI], 0
inc DI
loop BCD_CLEANUP_LOOP
lea BX, input2Packd
cmp DX, 1
mov DX, 0
jz BCD_CLEANUP
jmp START
QUIT:
exitcode 0
end START
Any better code suggestions welcome as well, but not needed if you don't have an answer for the question.
The DOS buffered input function 0Ah allows you to have a preset text in the storage space of the input buffer that you provide. For a complete explanation of this DOS function see how buffered input works.
The first time that your program runs the inputLen1 and inputLen2 fields are empty because of how you defined them in the source using db ? which translates to zero.
But when you re-run the code this is no longer the case! The lengths still show what you got in the previous run. You need to zero both these fields before invoking function 0Ah again on the same input buffers.
mov DX, 0
jz BCD_CLEANUP
mov inputLen1, DL ;DL=0
mov inputLen2, DL ;DL=0
jmp START
The MATH loop has a couple of problems regarding the CF.
The sbb al, ah instruction depends on the value in the carry flag, but you neglect to make sure it is off on the first iteration of this loop. Just add clc:
clc
MATH:
lea BX, input1Packd
mov AL, [BX][SI]
lea BX, input2Packd
mov AH, [BX][SI]
sbb AL, AH
The das instruction consumes the carry flag that you get from the sbb instruction, but it's the carry flag that you get from the das instruction that you need to preserve/restore to have it propagate through the loop.
sbb AL, AH
das
pushf
...a program that subtracts two entered decimals (max 10 characters ea)...
If ever you enter 9 or 10 characters, those most significant digits will not be taken into account because MATH_SETUP really limits you to 8 characters (which in turn is a good thing since the res buffer only has room to show 8 characters)!
MATH_SETUP:
mov SI, 4 <-- Could permit 10 packed BCD digits
mov CX, 4 <-- Max 8 characters
mov DI, 7 <-- Max 8 characters

IA-32 beginner asm sys_write confusion

I'm using NASM, IA-32 (I think). The homework is to take ASCII from a .txt file and print it to the console in hexadecimal using linux shell input redirection. I think I've got the ascii to hex part figured out, but I'm having a hard time making it print the console correctly. I'm sure I'm just missing something small and silly. The file I'm reading just to test is something simple like this:
ABC
123
The output should be:
41 42 43 0A 31 20 32 20 33 20
The problem is happening in
_hexOut:
I've tried changing more stuff around than I can fit in this post, and I can't figure out where I'm going wrong.
The only output I'm getting is:
41 42 43
section .text
global _start
_start:
mov edx, inBuffSize ; sys_read
mov ecx, inBuffer ; -
mov ebx, 0x00 ; -
mov eax, 0x03 ; -
int 0x80 ; end sys_read
mov [inputLen], eax
cmp eax, 0
jle _exit
_inLeft:
mov ecx, [inCount] ; move
cmp ecx, [inputLen]
je _hexOut
movzx eax, byte [inBuffer + ecx]
mov [temp], al
shr al, 4
mov bl, 9
cmp al, bl
jle _leftIsNumber
mov bl, 10
cmp al, bl
jge _leftIsLetter
_leftIsNumber:
mov ecx, [outCount]
add al, 48
mov [outBuffer + ecx], al
mov ecx, [outCount]
inc ecx
mov [outCount], ecx
jmp _inRight
_leftIsLetter:
mov ecx, [outCount]
add al, 55
mov [outBuffer + ecx], al
mov ecx, [outCount]
inc ecx
mov [outCount], ecx
jmp _inRight
_inRight:
mov ecx, [inCount]
inc ecx
mov [inCount], ecx
movzx eax, byte [temp]
and al, 0x0F
mov bl, 9
cmp al, bl
jle _rightIsNumber
mov bl, 10
cmp al, bl
jge _rightIsLetter
_rightIsNumber:
mov ecx, [outCount]
add al, 48
mov [outBuffer + ecx], al
mov ecx, [outCount]
inc ecx
mov [outBuffer + ecx], byte 32
inc ecx
mov [outCount], ecx
jmp _inLeft
_rightIsLetter:
mov ecx, [outCount]
add al, 55
mov [outBuffer + ecx], al
mov ecx, [outCount]
inc ecx
mov [outBuffer + ecx], byte 32
inc ecx
mov [outCount], ecx
jmp _inLeft
_hexOut:;------------------ PROBLEMS HERE ----------------------
; mov ecx, [inputLen]
; mov [outCount + ecx], byte 0x0A
; inc ecx
; mov [inputLen], ecx
mov edx, [inputLen]
mov ecx, outBuffer
mov ebx, 0x01
mov eax, 0x04
int 0x80
;------------------------------------------------------
_exit:
mov ebx, 0
mov eax, 1
int 0x80
section .data
section .bss
inBuffer resb inBuffSize
outBuffer resb outBuffSize
inBuffSize equ 200
outBuffSize equ 600
inCount resd 1
outCount resd 1
temp resb 1
inputLen resd 1
The second part of the problem is that I need to be able to change how many hex bytes are shown per line in the console, but I can't start to tackle that until I get the first problem solved. Any nudge in the right direction would be appreciated!

NASM: SegFault on MOV ECX

I'm trying to make a very simple assembly program run, however I seem to get segfaults whatever I do.
Here is my code (should print 'a' on a linux machine)
section .data
buffer times 50 db 97
pointer db 0
section .text
global _start
_start:
mov ECX , pointer
mov EDX , [buffer + ECX]
mov EAX , 4
mov EBX , 1
mov ECX , EDX
mov EDX , 1
int 0x80
It causes a segfault on the first MOV but it seems obvious to me that it should work.
I reduced it to almost nothing and it still segfault.
section .data
msg db "hello"
section .text
global _start
_start:
mov EAX,1
I've run this succesfully:
section .text
global _start
_start:
mov ax, 0b
dec ax
sub ax, 11111111b
mov bx, 97
add ax, bx
mov [INVENTORY], ax ; put a in first inventory pos
mov eax, 4
mov ebx, 1
mov ecx, INVENTORY
mov edx, 1
int 0x80
mov ax, [INVENTORY]
add ax, 1
mov [INVENTORY + 1], ax ; put b in second inventory pos
mov [VAR], ax
mov eax, 4
mov ebx, 1
mov ecx, VAR
mov edx, 1
int 0x80
mov eax, 4
mov ebx, 1
mov ecx, '\n'
mov edx, 1
int 0x80
mov eax,1
int 0x80
_newline:
section .data
VAR DW 0
INVENTORY TIMES 8 DW 0
Is it possible that it has to do with the symbols I use for newlines or tabs? I generate the assembly from java and I use \t for tabs and \n for new lines (and spaces so it doesn't look too bad.
I'm using NASM and I'm running it here:
https://www.tutorialspoint.com/compile_assembly_online.php
Thank you!
If you are just trying to print out a set of 'a's.
section .data
buffer times 50 db 97
len.buffer equ $-buffer
pointer db 0
section .text
global _start
_start:
; ssize_t write(int fd, const void *buf, size_t count);
; i386 ebx ecx edx esi edi ebp
mov EAX , 4 ; write syscall
mov EBX , 1 ; std out
lea ecx, [buffer] ; buffer
mov edx, len.buffer ; size
int 0x80
_exit:
mov eax, 1 ; exit syscall
int 0x80
output:
./yvon_001
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\ndavid#ubuntuserver00A:~/asm$ ./yvon_001
In the end #MichealPetch was right, I needed to add an EXIT syscall at the end of the code. The sample I tried still did a SEGFAULT because I was moving pointer instead of [pointer] in a registry.
Thanks for comments and answers!

Reversing a string in assembly language 80x86

In a program I am currently doing, I have to reverse a string a user has entered. I have to leave the word that the user entered in where I prompted them to enter it and right below it I want to print out the word in reverse. When I try to run it in DOSBox with the Tasm compiler it gives me an error that says "Illegal memory reference" on line 189 which is the line that contains the variable I plan to put the reversed word in. Can someone help me find out what I am doing wrong? I would greatly appreciate it! Also only in my program there are 4 boxes. The first box I try to print the reverse word below the prompt. The rest of the boxes prints the user entered word instead of the reversed version of it.
title fill in title ;program name
;------------------------------------------------------------------
stacksg segment para stack 'Stack' ;define the stack
db 32 dup(0) ;32 bytes, might want larger
stacksg ends
;------------------------------------------------------------------
datasg segment para 'Data' ;data segment
paralst Label Byte
maxlen db 21
actlen db ?
dbdata db 21 dup('$')
outit db 'Enter String: $' ;14 chars minus $
switch db 21 dup('$')
datasg ends
;------------------------------------------------------------------
codesg segment para 'Code' ;code segment
main proc far ;main procedure
assume ss:stacksg, ds:datasg, cs:codesg ;define segment registers
mov ax, datasg ;initialize data segment register
mov ds, ax
;--------------- --------------------------top left corner
mov ah, 06h
mov al, 00
mov bh, 01000001b ; 4eh
mov ch, 0
mov cl, 0
mov dl, 39
mov dh, 12
int 10h
;-------------------------------------------top right corner
mov ah, 06h
mov al, 0
mov bh, 11110010b
;mov cx, 0c00h
;mov dx, 184fh
mov ch, 0
mov cl, 39
mov dh, 12
mov dl, 79
int 10h
;--------------------------------------------bottom left corner
mov ah, 06h
mov al, 0
mov bh, 11100100b ;yellow
mov ch, 12
mov cl, 0
mov dh, 24
mov dl, 39
int 10h
;------------------------------------------bottom right corner
mov ah, 06h
mov al, 0
mov bh, 01011111b ; magenta 80
mov ch, 12
mov cl, 39
mov dh, 24
mov dl, 79
int 10h
;--------------------------------------------------- 1st quad
mov ah, 02h
mov bh, 0
mov dh, 5
mov dl, 5
int 10h
mov ah, 09h
lea dx, outit
int 21h
;------------------------------------input
mov ah, 0ah
lea dx, paralst
int 21h
; -----------------------------------move cursor
mov ah, 02h
mov bh, 0
mov dh, 7
mov dl, 5
int 10h
call REVERSE
;--------------------------------------print output
mov ah, 09h
lea dx, switch
int 21h
;----------------------------------------------------2nd quad
mov ah, 02h
mov bh, 0
mov dh, 5
mov dl, 44
int 10h
mov ah, 09h
lea dx, outit
int 21h
;------------------------------------input
mov ah, 0ah
lea dx, paralst
int 21h
; -----------------------------------move cursor
mov ah, 02h
mov bh, 0
mov dh, 7
mov dl, 44
int 10h
;--------------------------------------print output
mov ah, 09h
lea dx, dbdata
int 21h
;------------------------------------------------------3rd quad
mov ah, 02h
mov bh, 0
mov dh, 17
mov dl, 5
int 10h
mov ah, 09h
lea dx, outit
int 21h
;------------------------------------input
mov ah, 0ah
lea dx, paralst
int 21h
; -----------------------------------move cursor
mov ah, 02h
mov bh, 0
mov dh, 19
mov dl, 5
int 10h
;--------------------------------------print output
mov ah, 09h
lea dx, dbdata
int 21h
;------------------------------------------------------4th quad
mov ah, 02h
mov bh, 0
mov dh, 17
mov dl, 44
int 10h
mov ah, 09h
lea dx, outit
int 21h
;------------------------------------input
mov ah, 0ah
lea dx, paralst
int 21h
; -----------------------------------move cursor
mov ah, 02h
mov bh, 0
mov dh, 19
mov dl, 44
int 10h
;--------------------------------------print output
mov ah, 09h
lea dx, dbdata
int 21h
mov ax, 4c00h ;end processing
int 21h
main endp ;end of procedure
;----------------------------------------reverse procedure
REVERSE PROC NEAR
mov cx, 0
;-----figure out actlen here
mov actlen, 0
lea bx, dbdata ;may need to use paralst instead
hi: cmp [bx], '$'
jne sup
inc actlen
inc bx
jmp hi
sup:
;------------
mov cx, 0
mov cl, actlen
lea bx, dbdata
add bx, cx
yo: cmp actlen, 0
je hola
mov switch, byte ptr[bx]
dec bx
inc switch
dec actlen
jmp yo
hola:
RET
REVERSE ENDP
codesg ends ;end of code segment
end main ;end of program
You can't do mov memory, memory. Move the source into a register first, then place it at the destination.Also, inc switch doesn't do what you seem to think it's doing. You can't change the address of switch at runtime, so what's actually happening there is that you're incrementing the first element at switch (as if you had written inc [switch]). If you need to modify an address; again, put it in a register.
So for example:
lea si, dbdata
add si,cx
lea di, switch
mov al,[bx]
jcxz hola ; jump if cx == 0
cld ; clear the direction flag; for stosb
yo: mov al,[si]
stosb ; [di++] = al
dec si
loop yo ; if (--cx) jmp yo
hola:
I didn't look at the entire piece of code, so it's unclear to me whether your reverse is supposed to copy the terminator ('$') or not. If it should, you should always execute the loop at least once (and then you don't need the jcxz). If it shouldn't, you should set the source address to dbdata + cx - 1 instead of dbdata + cx.

how to check compare if palindrome or not?

I don't know much of assembly language but I tried making this palindrome and it's quite hard. First I have to enter a string then show its original and reversed string then show if its a palindrome or not.
I already figured out how to show the reverse string by pushing and popping it through a loop from what I have read in another forum and figured it out
now the only problem for me is to compare the reverse string and original string to check if its a palindrome or not.
call clearscreen
mov dh, 0
mov dl, 0
call cursor
mov ah, 09h
mov dx, offset str1
int 21h
palin db 40 dup(?)
mov ah, 0ah
mov palin, 40
mov dx, offset palin
int 21h
mov dh, 1
mov dl, 0
call cursor
mov ah, 09h
mov dx, offset str2
int 21h
mov si, 2
forward:
mov al, palin + si
cmp al, 13
je palindrome
mov ah, 02h
mov dl, al
push ax 'push the letter to reverse
int 21h
inc si
jmp forward
palindrome:
mov dh, 2
mov dl, 0
call cursor
mov ah, 09h
mov dx, offset str3
int 21h
mov cx, 40 'pop each letter through a loop to show its reverse
reverse:
mov ah, 02h
pop ax
mov dl, al
int 21h
loop reverse
int 20h
clearscreen:
mov ax, 0600h
mov bh, 0Eh
mov cx, 0
mov dx, 8025
int 10h
ret
cursor:
mov ah, 02h
mov bh, 0
int 10h
ret
str1: db "Enter A String : $"
str2: db "Forward : $"
str3: db "Backward : $"
str4: db "Its a Palindrome! $"
str5: db "Not a Palindrome!$"
You have a string the user typed in, what you need to do is compare the first byte with the last byte, do the same for the 2nd one and the 2nd to last one. Keep doing this for the whole string. You also need the length of the string. To make life easier, you should convert the string to all UPPER case letters or all lowercase letters to make comparison easier.
It is unfortunate they are still teaching 16bit DOS code. This is a sample with the word defined in the data section. You will have to modify it to receive input and work on that string
.data
pal db "racecar"
pal_len equ $ - pal - 1
szYes db "yes$"
szNo db "no$"
.code
start:
mov ax,#data
mov ds,ax
call IsPalindrome
mov ah,4ch
int 21h
IsPalindrome:
lea si, pal
lea di, pal
add di, pal_len
mov cx, 0
CheckIt:
mov al, byte ptr [si]
mov dl, byte ptr [di]
cmp al, dl
jne No
inc si
dec di
inc cx
cmp cx, pal_len
jne CheckIt
mov ah,9
lea dx,szYes
int 21h
ret
No:
mov ah,9
lea dx,szNo
int 21h
ret
end start
For completeness and to bring us into the 21st century, 32bit NASM code:
section .data
fmt db "%s", 0
szPal db "RACECAR"
Pal_len equ $ - szPal - 1
szYes db "Yes", 10, 0
szNo db "No", 10, 0
extern printf, exit
global _start
section .text
_start:
call IsPalindrome
call exit
IsPalindrome:
mov ecx, 0
mov ebx, Pal_len
mov esi, szPal
.CheckIt:
mov al, byte [esi + ecx]
mov dl, byte [esi + ebx]
cmp al, dl
jne .No
inc ecx
dec ebx
jns .CheckIt
push szYes
push fmt
call printf
add esp, 4 * 2
mov eax, 1
jmp Done
.No:
push szNo
push fmt
call printf
add esp, 4 * 2
xor eax, eax
Done:
ret

Resources