Author Topic: Read in a file and display ascii symbols?  (Read 36141 times)

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Read in a file and display ascii symbols?
« Reply #15 on: November 30, 2012, 06:40:34 AM »
CRLF? Well, I didn't do quite what you said, but same general effect, I think. Still doesn't look much like a "board". This is what I've got:
Code: [Select]
; nasm -f elf32 readboard.asm
; ld -o readboard readboard.o

global _start

section .data
    board db "cBoard.txt", 0

section .bss
    buffer resb 1000h

section .text
_start:
    nop
    mov ecx, 0
    mov ebx, board
    mov eax, 5
    int 80h
    test eax, eax
    js exit

    mov ebx, eax
    mov edx, 1000h
    mov ecx, buffer
    mov eax, 3
    int 80h
    lea edi, [eax + ecx]

    mov edx, buffer   
.top:
    push edx
    call atoi
    add esp, 4
    cmp cl, 10
    jnz .skipnl
    mov al, 10
.skipnl:
    call putc
    cmp edx, edi
    jnz .top
   
exit:
    mov ebx, eax
    mov eax, 1
    int 80h

;--------------------
atoi:
    push ebx
   
    mov edx, [esp + 8]  ; pointer to string
    xor ebx, ebx ; assume not negative
   
    cmp byte [edx], '-'
    jnz notneg
    inc ebx ; indicate negative
    inc edx ; move past the '-'
notneg:

    xor eax, eax        ; clear "result"
.top:
    movzx ecx, byte [edx]
    inc edx
    cmp ecx, byte '0'
    jb .done
    cmp ecx, byte '9'
    ja .done
   
    ; we have a valid character - multiply
    ; result-so-far by 10, subtract '0'
    ; from the character to convert it to
    ; a number, and add it to result.
   
    lea eax, [eax + eax * 4]
    lea eax, [eax * 2 + ecx - 48]

    jmp short .top
.done:
    test ebx, ebx
    jz notminus
    neg eax
notminus:
    pop ebx
    ret
;------------------------
putc:
    push edx
    push ecx
    push ebx
    push eax
    mov ecx, esp
    mov edx, 1
    mov ebx, 1
    mov eax, 4
    int 80h
    pop eax
    pop ebx
    pop ecx
    pop edx
    ret
;-------------------

Told ya it was sloppy! :)

Best,
Frank


Offline Keith Kanios

  • Full Member
  • **
  • Posts: 383
  • Country: us
    • Personal Homepage
Re: Read in a file and display ascii symbols?
« Reply #16 on: December 01, 2012, 04:28:23 AM »
This:

Code: [Select]
201 205 203 205 203 205 203 205 203 205 203 205 203 205 203 205 187
186 177 186 224 186 177 186 224 186 177 186 224 186 177 186 224 186
204 205 206 205 206 205 206 205 206 205 206 205 206 205 206 205 185
186 224 186 177 186 224 186 177 186 224 186 177 186 224 186 177 186
204 205 206 205 206 205 206 205 206 205 206 205 206 205 206 205 185
186 177 186 224 186 177 186 224 186 177 186 224 186 177 186 224 186
204 205 206 205 206 205 206 205 206 205 206 205 206 205 206 205 185
186 32 186 177 186 32 186 177 186 32 186 177 186 32 186 177 186
204 205 206 205 206 205 206 205 206 205 206 205 206 205 206 205 185
186 177 186 32 186 177 186 32 186 177 186 32 186 177 186 32 186
204 205 206 205 206 205 206 205 206 205 206 205 206 205 206 205 185
186 225 186 177 186 225 186 177 186 225 186 177 186 225 186 177 186
204 205 206 205 206 205 206 205 206 205 206 205 206 205 206 205 185
186 177 186 225 186 177 186 225 186 177 186 225 186 177 186 225 186
204 205 206 205 206 205 206 205 206 205 206 205 206 205 206 205 185
186 225 186 177 186 225 186 177 186 225 186 177 186 225 186 177 186
200 205 202 205 202 205 202 205 202 205 202 205 202 205 202 205 188

... with this:

Code: [Select]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

FILE *fp = NULL;
char buffer[4096];
char tmp[4] = {0,0,0,0};
int cnt = 0;

void convert(void)
{
int c;
if(cnt > 0){
c = atoi((char *)&tmp);
if(c > 0) printf("%c",c);
memset((void *)tmp,0,4);
cnt = 0;
}
return;
}

int main(int argc, char *argv[])
{
int i,c,n;

fp = fopen("cBoard.txt","rb");
if(fp == NULL){
printf("Could not open cBoard.txt");
return 1;
}

while(!feof(fp)){
n = fread(buffer,1,4096,fp);
if((n == 0) || ((n != 4096) && ferror(fp))) break;

for(i=0; i<n; i++)
{
c = buffer[i];
if((c >= 0x30) && (c <= 0x39)){
tmp[cnt] = c;
cnt++;
if(cnt > 3) convert();
}else{
convert();
if((c == 0x0D) || (c == 0x0A)){
printf("%c",c);
}
}
}
}
convert();

fclose(fp);
return 0;
}

... or this:

Code: [Select]
global _main
extern _atoi
extern _printf
extern _fopen
extern _fread
extern _fclose
extern _feof
extern _ferror
extern _exit

[section .data]
file_name DB 'cBoard.txt',0
file_mode DB 'rb',0
file_handle DD 0
error_open DB 'Could not open cBoard.txt',0
char DB '%c',0
cnt DD 0

[section .bss]
tmp RESD 1
buffer RESB 4096

[section .text]
_main:
push file_mode
push file_name
call _fopen
add esp,8
mov DWORD[file_handle],eax
test eax,eax
jnz .read

push error_open
call _printf
add esp,4
jmp exit

.read:
call read
jc .last
mov edi,eax
mov esi,buffer
cld
.parse:
lodsb
cmp al,0x30
jb .whitespace
cmp al,0x39
ja .whitespace

mov ebx,tmp
add ebx,DWORD[cnt]
mov BYTE[ebx],al
inc DWORD[cnt]
cmp DWORD[cnt],3
jb .next
xor eax,eax

.whitespace:
push eax
call convert
pop eax

cmp al,0x0D
je .print

.linefeed:
cmp al,0x0A
jne .next

.print:
call print

.next:
dec edi
jnz .parse
jmp .read
.last:
call convert

exit:
mov eax,DWORD[file_handle]
test eax,eax
jz .ret

push eax
call _fclose
add esp,4

.ret:
xor eax,eax
ret

convert:
push tmp
call _atoi
add esp,4
test eax,eax
je .ret

call print

.ret:
mov DWORD[tmp],0
mov DWORD[cnt],0
ret

read:
push DWORD[file_handle]
call _feof
add esp,4
test eax,eax
jz .read
.eof:
stc
ret

.read:
push DWORD[file_handle]
push 4096
push 1
push buffer
call _fread
add esp,16
test eax,eax
jz .eof
cmp eax,4096
je .ret

push eax
push DWORD[file_handle]
call _ferror
add esp,4
mov ecx,eax
pop eax
test ecx,ecx
jnz .eof

.ret:
clc
ret

print:
movzx eax,al
push eax
push char
call _printf
add esp,8
ret

... produces this:

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Read in a file and display ascii symbols?
« Reply #17 on: December 01, 2012, 07:07:32 AM »
Well... I'm evidently doing something wrong. I still suspect a code page issue. From your C version. I get something quite similar to my simpleminded version, but with 17 characters per line (the CR?). Too lazy to pluck the underscores out of your .asm version at the moment (need an "--unprefix" option). Maybe later.

Best,
Frank


Offline Keith Kanios

  • Full Member
  • **
  • Posts: 383
  • Country: us
    • Personal Homepage
Re: Read in a file and display ascii symbols?
« Reply #18 on: December 01, 2012, 08:46:59 AM »
I still suspect a code page issue. From your C version.

Yep. Your version works under a Windows command prompt. Mine prints out garbled text within a Tiny Core Linux live cd terminal instance.

From what I can gather, you'll have to change the codepage to 437, or perhaps pipe the output through something like iconv.

All the more reason to use UTF-8 and not waste time with these silly types of issues :)

Offline XxWh1t3gh0stxX

  • Jr. Member
  • *
  • Posts: 13
Re: Read in a file and display ascii symbols?
« Reply #19 on: December 03, 2012, 03:09:43 AM »
Wow, hey Keith do you mind if I implement your method into my program? I am wondering now how would I move the characters, alpha and beta? This is for a checkers game by the way guys. But with Keith's code I'm thinking that if I moved one of the alphas it would move all the alpha's? would I have to have another .txt file that corresponded to what alphas and betas they were say 1 was the first 2 was the next alpha, etc. ? or make an array of some sort? kinda lost there? or if I looked at it like a grid or made a grid where current selected alpha at (1, 2) could move to an appropriate position somewhere (?, ?). And then there is the movement question. I know that I can move the cursor, but I was kinda thinking that if I just made 2 parameters for destination and source of the checkers that that could maybe work. Any suggestions, tips, or anything. Thanks to all that have helped.

Offline XxWh1t3gh0stxX

  • Jr. Member
  • *
  • Posts: 13
Re: Read in a file and display ascii symbols?
« Reply #20 on: December 03, 2012, 03:28:05 AM »
Keith where in the code do you actually find the 3 string of characters to convert. What variable are you saving them in?

Offline XxWh1t3gh0stxX

  • Jr. Member
  • *
  • Posts: 13
Re: Read in a file and display ascii symbols?
« Reply #21 on: December 03, 2012, 03:33:40 AM »
k I found out that I can move the cursor with
Code: [Select]
Note: replace # with the number of rows to move.
§ represents the escape character, which is number 27 in the ASCII table

move cursor up §[#A
move cursor down §[#B
move cursor right §[#C
move cursor left §[#D

I'm still looking where you save the 3 string so that I might change the colors of them with

Code: [Select]
§[#m
Note: Replace # with:

30 for black
31 for red
32 for green
33 for yellow
34 for blue
35 for magenta
36 for cyan
37 for light gray

Thanks guys

Offline Keith Kanios

  • Full Member
  • **
  • Posts: 383
  • Country: us
    • Personal Homepage
Re: Read in a file and display ascii symbols?
« Reply #22 on: December 04, 2012, 01:44:44 AM »
Wow, hey Keith do you mind if I implement your method into my program?

Sure, but that's really a question for your instructor.

Keith where in the code do you actually find the 3 string of characters to convert. What variable are you saving them in?

Start at the end, the convert function, and work your way back.

Overall, I recommend taking the next step, and decoupling your presentation from your logic.