Author Topic: Choice of .model affects the output of program  (Read 2564 times)

Hedegaard

  • Regular Member
  • *
  • Posts: 2
Choice of .model affects the output of program
« on: July 15, 2017, 03:48:45 AM »
Hello

I'm new to Assembly and am following a tutorial on youtube (https://www.youtube.com/watch?v=pMzjKkj1ldk&list=PLPedo-T7QiNsIji329HyTzbKBuCAHwNFC&t=1m54s).

The purpose of the current part of the tutorial is to show how the .data section works and how ASCII values are used. However, when I run the program of the tutorial (included below), it prints out the symbol '²' instead of '2', which was what was supposed to be the output. The output in the youtube video does the same thing, but the guy doing the tutorial doesn't notice the difference between the two symbols.

In fact, no matter what I choose for the value of the variable "count1", it still outputs '²'. As someone else in the comments of the youtube video remarked, it works the way you want it to, if you replace ".model small" by ".model tiny". What is going on here?

Code: [Select]
.model small
.data

    count1 db 2

.code

main proc
   
    mov dl, count1  ; Input character to dl
   
   
    add dl, 48 ; add 48 to dl
    mov ah, 2h ; Code for write character
   
    int 21h    ; Display character in dl
   
endp

end main

Best regards.
Using emu8086, v 4.08 (8086 microprocessor emulator).

nidud

  • Member
  • *****
  • Posts: 1614
    • https://github.com/nidud/asmc
Re: Choice of .model affects the output of program
« Reply #1 on: July 15, 2017, 04:07:37 AM »
you need to set DS to the data segment first.
try this:
Code: [Select]
    mov ax,SEG _DATA
    mov ds,ax
    mov dl, count1  ; Input character to dl

or

Code: [Select]
    mov ax,@data

Hedegaard

  • Regular Member
  • *
  • Posts: 2
Re: Choice of .model affects the output of program
« Reply #2 on: July 15, 2017, 05:53:14 AM »
nidud, thanks, though I cannot get it to work. I tried it, but now I get an output of 'á', which looks like it is ASCII character 160. If I change the .model to tiny, the output will be '0'. I'm not sure why the model affects the output.

My code is now:

Code: [Select]
.model small
.data

    count1 db 4

.code

main proc
   
    mov ax, SEG _DATA
    mov ds, ax
   
    mov dl, count1  ; Input character to dl
   
   
    add dl, 48 ; add 48 to dl
    mov ah, 2h ; Code for write character
   
    int 21h    ; Display character in dl
   
endp

end main

Using emu8086, v 4.08 (8086 microprocessor emulator).

nidud

  • Member
  • *****
  • Posts: 1614
    • https://github.com/nidud/asmc
Re: Choice of .model affects the output of program
« Reply #3 on: July 15, 2017, 06:19:43 AM »
You could try using @data instead of SEG _DATA. Using DGROUP should also work in that case.

nidud

  • Member
  • *****
  • Posts: 1614
    • https://github.com/nidud/asmc
Re: Choice of .model affects the output of program
« Reply #4 on: July 15, 2017, 06:33:50 AM »
In the small model the stack is equal data so using SS should also work.
Code: [Select]
    mov ax, ss
    mov ds, ax
    ..
    push ss
    pop ds

Or simply use SS directly:
Code: [Select]
    mov dl,ss:count1

RuiLoureiro

  • Member
  • ****
  • Posts: 819
Re: Choice of .model affects the output of program
« Reply #5 on: July 15, 2017, 06:52:24 AM »
Hi Hedegaard,
                     Wellcome to the forum.

                     The example is too old DOS. If you want to learn asm, download MASM32 from top right.
                     There are a lot of examples and you may start to write your own examples and post it here.
                     See you  :t

felipe

  • Member
  • ****
  • Posts: 970
  • Eagles are just great!
Re: Choice of .model affects the output of program
« Reply #6 on: July 15, 2017, 07:57:46 AM »
Hedegaard:
I seen that in the video the guy is using emu8086 or something like that and i searched for the syntax of that tool and is not the same as masm's syntax. In the sense that is more flexible and probably will allow you to write things that won't run in real machines (you have to consider the OS). If you want to learn assembly language, i strongly recommend you, (even if it is for 16 bit  DOS programs), to learn a real assembly language, like masm.  :icon14:

And welcome to this forum!  :t
Felipe.

AW

  • Member
  • *****
  • Posts: 1551
  • Let's Make ASM Great Again!
Re: Choice of .model affects the output of program
« Reply #7 on: July 15, 2017, 08:15:17 AM »
Try this:

Code: [Select]
;ml /c test.asm
;link16 test.obj

.186

.model  small

.stack  100h

.data
count1 db 2
 
.code

start:
mov   ax,seg count1
        mov   ds,ax
mov dl, count1  ; Input character to dl
   
add dl, 48 ; add 48 to dl
mov ah, 2h ; Code for write character
   
int 21h    ; Display character in dl
 
mov  ax, 4c00h       ; terminate program
        int 21h
 
end start

Tested in DosBox and works  :biggrin:
« Last Edit: July 15, 2017, 04:35:18 PM by aw27 »

casey121

  • Regular Member
  • *
  • Posts: 1
  • https://jsonformatter-online.com
    • JSON formatter
Re: Choice of .model affects the output of program
« Reply #8 on: June 21, 2018, 12:00:03 PM »
Using DGROUP also work in this case
Hello guys, I'm Casey from JSON formatter team . Our project are http://rolltheball.co and http://impossiblegame.co . Hope your help, thank you!

daydreamer

  • Member
  • ****
  • Posts: 557
  • reach for the stars
Re: Choice of .model affects the output of program
« Reply #9 on: June 21, 2018, 02:48:15 PM »
Welcome to forum :t
I recommend try masm32 and 32bit assembly
Quote from Flashdance
Nick  :  When you give up your dream, you die.
*wears a flameproof asbestos suit*

zedd151

  • Member
  • ****
  • Posts: 850
Re: Choice of .model affects the output of program
« Reply #10 on: June 21, 2018, 02:49:55 PM »
Welcome to forum :t
I recommend try masm32 and 32bit assembly

That's a spam-bot.   8)

not a good one either. first link points to
Code: [Select]
http://%22https//jsonformatter-online.com/%22
notice the odd formatting? the second is screwed up as well. the third, well, has results.
I'm not always the sharpest knife in the drawer, but I have my moments.  :P

jj2007

  • Member
  • *****
  • Posts: 8806
  • Assembler is fun ;-)
    • MasmBasic
Re: Choice of .model affects the output of program
« Reply #11 on: June 21, 2018, 09:46:07 PM »
My code is now:
... almost perfect. You only need the 16-bit ExitProcess equivalent, and it works just fine:

Code: [Select]
.model small
.data

    count1 db 4

.code

main proc
   
    mov ax, SEG _DATA
    mov ds, ax
   
    mov dl, count1  ; Input character to dl
   
   
    add dl, 48 ; add 48 to dl
    mov ah, 2h ; Code for write character
   
    int 21h    ; Display character in dl

    mov ax, 4C00h ; ExitProcess, DOS style
    int 21h   

main endp

end main

It seems like a minor detail, but it isn't; without a proper exit, you get all kinds of weird output to the console.
I also added main before endp.

felipe

  • Member
  • ****
  • Posts: 970
  • Eagles are just great!
Re: Choice of .model affects the output of program
« Reply #12 on: June 22, 2018, 03:58:31 AM »
I think the old ml (for 16 bits) even has a directive that do exactly that (the exit app code in dos). I think is called EXIT. I also remembers that it had one to initialize the data section (loading ds with @data) but i'm not sure of the name of that directive. Both are in that old (i think is the only one) masm manual.  :idea:
Felipe.