flat assembler
Message board for the users of flat assembler.

Index > DOS > What is wrong with this code?

Author
Thread Post new topic Reply to topic
Kristian_



Joined: 13 Nov 2004
Posts: 38
Kristian_
Hi!
I'm having trouble with this simple code. It doesn't print anything out, but it should!
Code:
org 256
mov ah, 9        ; for text output
mov ax, 4     
push ax          ; push ax into the stack
pop ax           ; get the last push'ed value from the stack

mov bx, 4
cmp ax, bx       
je true              ; if(ax == bx){ goto true; } else {
jmp false         ; goto false; }

   true:
     dx, msg
     int 21h
     int 20h
   false:
    int 20h

msg DB "TRUE$"    


And another thing. AX and BX registers are like normal 16 bit variables, but the difference are that they are in CPU? And CX is used for loops and DX is used for math (multiplication/devision) is it bad to use CX and DX the same way as AX, BX like normal variables? And what else registers are there, without any purpose like AX and BX? Thank you!
Post 19 Nov 2004, 13:19
View user's profile Send private message Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1171
Location: Overflow
Matrix
Kristian_ wrote:
Hi!
I'm having trouble with this simple code. It doesn't print anything out, but it should!
Code:
org 256
mov ah, 9        ; for text output
mov ax, 4     
push ax          ; push ax into the stack
pop ax           ; get the last push'ed value from the stack

mov bx, 4
cmp ax, bx        
je true              ; if(ax == bx){ goto true; } else {
jmp false         ; goto false; }

   true:
     dx, msg
     int 21h
     int 20h
   false:
    int 20h

msg DB "TRUE$"    


And another thing. AX and BX registers are like normal 16 bit variables, but the difference are that they are in CPU? And CX is used for loops and DX is used for math (multiplication/devision) is it bad to use CX and DX the same way as AX, BX like normal variables? And what else registers are there, without any purpose like AX and BX? Thank you!

that is because al is lower 8 bits of ax, and ax is lower 16 bits of eax,
so,
if you put 1 in ax, then ah will be zeroed out
btw.: ax has ah - high part and al - low part
you can use the registers for whatever except segment registers, and sp
if you modify them , you should then restore, or don't use stack for example if you're using ss,sp

you got this wrong there
Code:
mov ah, 9        ; for text output
mov ax, 4     
    

ah is high part of ax
to see what you're doing:
Code:
mov ah,$09        ; for text output
mov ax,$0004    ; high 2 nibbles, then low 2 nibbles
    
Post 19 Nov 2004, 13:26
View user's profile Send private message Visit poster's website Reply with quote
crc



Joined: 21 Jun 2003
Posts: 637
Location: Penndel, PA [USA]
crc
Code:
org 256

mov ax, 4     
mov bx, 4
cmp ax, bx      
je true          ; if(ax == bx){ goto true; } else {
jmp false        ; goto false; }

        true:
          mov dx, msg    ; You forgot the 'mov'
          mov  ah, 9       ; Setting ax above cleared this, so we move it here
          int 21h
          int 20h
        false:
          int 20h

msg DB "TRUE$"    


1) Setting ax will clear ah and al, so set ah later in the function.
2) No need to do push ax followed immediately by pop ax
3) This could be done more cleanly...
Post 19 Nov 2004, 13:34
View user's profile Send private message Visit poster's website Reply with quote
crc



Joined: 21 Jun 2003
Posts: 637
Location: Penndel, PA [USA]
crc
A cleaner version:

Code:
org 256

mov ax, 4     
mov bx, 4
cmp ax, bx      

jne false          ; skip the following if ax <> bx
                      ; True: do this
          mov dx, msg
          mov  ah, 9
          int 21h
false:              ; True condition will reach this and exit
          int 20h


msg DB "TRUE$"    
Post 19 Nov 2004, 13:36
View user's profile Send private message Visit poster's website Reply with quote
Kristian_



Joined: 13 Nov 2004
Posts: 38
Kristian_
Thank you! Very Happy
Quote:
ax has ah - high part and al - low part
Confused
What does it mean? For example AX register is 16bit:

[ AH 8bit]+[ ?? 8bit] = AX

AX has like two registers in it right? For example if I store this value into AX register 'ab', then AH will contain 'a' and other will contain b?
Post 19 Nov 2004, 17:55
View user's profile Send private message Reply with quote
bubach



Joined: 17 Sep 2004
Posts: 341
Location: Trollhättan, Sweden
bubach
[ AH 8bit]+[ AL 8bit] = AX ...

example:
Code:
mov ah, 0x03
mov al, 0x7E     ; AX is now: 0x037E
    
Post 19 Nov 2004, 18:20
View user's profile Send private message Reply with quote
Kristian_



Joined: 13 Nov 2004
Posts: 38
Kristian_
Thank you!
So if AH = 1 and AL = 2 then AX = 12 NOT 3 right?
And is this the same for BX, CX and DX?
Post 19 Nov 2004, 19:00
View user's profile Send private message Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1171
Location: Overflow
Matrix
0 1 = BIT Boolean Algebra is 0 or 1
AL = 8 bits = byte
AL = 10101010 ; 8 bits
AH = 01010101 ; 8 bits
AX = 16 Bits = word
now AX is AH then AL
AH = 01010101 ; 8 bits + AL = 10101010 ; 8 bits =
AX = 01010101+10101010 = 0101010110101010
and if AX = 0101010110101010 then EAX low 16 bits are AX so eax is now
EAX = 16 times 0 then 16 BITS AX=0101010110101010
EAX = 0000000000000000 0101010110101010

all registers are the same EAX EBX ECX EDX

ESI (32 bit register) 's low 16 bits are accessible via SI
32 bit one > 16 bit part
EDI > DI
ESP > SP
EBP > BP
before you ask they do not have a byte part seperately

segments are 16 bits only
ds es fs gs ss
Post 19 Nov 2004, 21:13
View user's profile Send private message Visit poster's website Reply with quote
Kristian_



Joined: 13 Nov 2004
Posts: 38
Kristian_
Thank you! But for what are segments used for?
Post 19 Nov 2004, 21:26
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
http://www.decard.net/?body=tajga&chapter=chap03
it's chapter 3.3. Not very detailed but may give you a pricture.
Post 20 Nov 2004, 17:10
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.