accessing character in Assembly language


I WAS WRITING A PROGRAM TO THAT PRINTS "HELLO WORLD" LIKE THIS:

H
E
L
L
O

W
O
R
L
D

THE PROGRAM THAT I WROTE GIVES ME THIS RESULT:

HELLO WORLD
ELLO WORLD
LLO WORLD
LO WORLD
O WORLD
WORLD
WORLD
ORLD
RLD
LD
D

WHAT AM I DOING WRONG? THE PROGRAM IS BELOW:

i


include irvine32.inc    

.data

hello byte "HELLO WORLD",0  ; message to write

.code

main proc
  call Clrscr
  call Crlf  
 
  mov ebx, offset hello
  call WriteChar  
  call Crlf

  mov ebx, offset hello+1
  call WriteChar  
  call Crlf

  mov ebx, offset hello+2
  call WriteChar  
  call Crlf
 
  mov ebx, offset hello+3
  call WriteChar  
  call Crlf

  mov ebx, offset hello+4
  call WriteChar  
  call Crlf

  mov ebx, offset hello+5
  call WriteChar  
  call Crlf

  mov ebx, offset hello+6
  call WriteChar  
  call Crlf

  mov ebx, offset hello+7
  call WriteChar  
  call Crlf

  mov ebx, offset hello+8
  call WriteChar  
  call Crlf

  mov ebx, offset hello+9
  call WriteChar  
  call Crlf

 
  mov ebx, offset hello+10
  call WriteChar  
  call Crlf

  exit                 ; a macro that calls ExitProcess
main endp

end main


sgbrtAsked:
Who is Participating?
 
craigwardmanConnect With a Mentor Commented:
Hello, I know this is an old question, but its not closed and ive run out of questions to answer :)

You can solve the above problem by only reading one char at a time, and finishing it off with a null char ( 0 )..

As follows:

include irvine32.inc    

.data

hello byte "HELLO WORLD",0  ; message to write
buf db 2 dup(0)           ;this is MASM syntax to define 2 bytes, not sure if works on all compilers

.code

main proc
  call Clrscr
  call Crlf  
 
  xor ecx, ecx
NEXTLETTER:
  mov esi, offset hello + [ecx]       ;put text into esi
  lea edi, buf                           ;assign 2byte buffer
  movsb                            ;move 1 character (ESI --> EDI)
  mov byte ptr [edi], 0        ;append a 0 (nullchar) [dont use +1, since movsb has incremented edi]
  dec edi                           ;get back to character position
  mov ebx, edi                   ;assuming that WriteChar is using ebx register
  pushad                           ;save all registers incase modified in macros
  call WriteChar  
  call Crlf
  popad                            ;get our registers back
  cmp ecx, 0Bh                  ;11 characters
  je EOW
  inc ecx
  jmp NEXTLETTER

EOW:

  exit                 ; a macro that calls ExitProcess
main endp

end main


I havent tested that, since I havent got those macros and havent got a quick way to re-write the code to output characters, but something along those lines should work..
0
 
LRHGuyCommented:
You don't show the WriteChar routine, which is the "culprit".

It seems WriteChar outputs everything from the starting position until a null (0). You'll have to fix that routine or use a different one.
0
 
sgbrtAuthor Commented:
I tried using WriteString, but didn't work!
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
LRHGuyCommented:
You'll either have to extract the part of WriteChar that output's the character, or you could do something like this:


hello byte "H",13,10,"E",13,10,"L",13,10,"L",13,10,"O",13,10,0  ; message to write

.code

main proc
  call Clrscr
  call Crlf  
 
  mov ebx, offset hello
  call WriteChar  

  exit                 ; a macro that calls ExitProcess
main endp

end main


0
 
sgbrtAuthor Commented:
LRHGuy, thanks but doesn't work!
0
 
LRHGuyCommented:
So, what does it do?

Can you show me the WriteChar routine?
0
 
PaulCaswellCommented:
Could WriteChar have a parameter of how many chars? Perhaps:

mov eax,w

before each call would help. Without 'WriteChar' we cannot help further.

Paul
0
 
BrianGEFF719Commented:
The problem exists in your WriteChar macro, you are writing the total length of the string instead of 1.
I imageing that in that macro you are looping until you hit a null byte. just make it print a single byte!!

-Brian
0
 
craigwardmanCommented:
just a thought, you might need to change this line:

mov esi, offset hello + [ecx]       ;put text into esi

I think that might give error, since ecx = 0 and [] means from address of..

so you could try changing it to:

lea esi, offset hello
add esi, ecx

:)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.