Author Topic: Assembly Lowercase To Uppercas  (Read 19190 times)

Offline silvercats

  • Jr. Member
  • *
  • Posts: 5
Assembly Lowercase To Uppercas
« on: June 05, 2014, 09:18:40 AM »
this is the error I get when running the code (NASM is the assembler ). Thanks.

cat@cat-laptop:~/Desktop$ sudo ./uppercase2 >toUpper.txt <fromLow.txt
./uppercase2: 1: �f�f�f�tf�̀f��f=t?f��f�tfMg�: not found./uppercase2: 1:
arg�: not found
: not found2: 1:


Code: [Select]
; Author           : Jeff Duntemann
; Description      : A simple program in assembly for Linux,using NASM 2.05,
;    demonstrating simple text file I/O (through redirection) for reading an
;    input file to a buffer in blocks, forcing lowercase characters to
;    uppercase, and writing the modified buffer to an output file.
;
; Run it this way:
;    uppercaser2 > (output file) < (input file)
;
; Build using these commands:
;    nasm -f elf -g -F stabs uppercaser2.asm
;    ld -o uppercaser2 uppercaser2.o
;
SECTION .bss                   ; Section containing uninitialized data
        BUFFLEN equ 1024       ; Length of buffer
        Buff:   resb BUFFLEN   ; Text buffer itself
SECTION .data              ; Section containing initialised data
SECTION .text              ; Section containing code
global  _start             ; Linker needs this to find the entry point!
_start:
                          ; This no-op keeps gdb happy...
        nop
; Read a buffer full of text from stdin:
read:
        mov eax,3         ; Specify sys_read call
        mov ebx,0         ; Specify File Descriptor 0: Standard Input
        mov ecx,Buff      ; Pass offset of the buffer to read to
        mov edx,BUFFLEN ; Pass number of bytes to read at one pass
        int 80h           ; Call sys_read to fill the buffer
        mov esi,eax       ; Copy sys_read return value for safekeeping
        cmp eax,0         ; If eax=0, sys_read reached EOF on stdin
        je Done           ; Jump If Equal (to 0, from compare)
; Set up the registers for the process buffer step:
        mov ecx,esi       ; Place the number of bytes read into ecx
        mov ebp,Buff      ; Place address of buffer into ebp
        dec ebp           ; Adjust count to offset
; Go through the buffer and convert lowercase to uppercase characters:
Scan:
        cmp byte [ebp+ecx],61h ; Test input char against lowercase 'a’
        jb Next          ; If below 'a’ in ASCII, not lowercase
        cmp byte [ebp+ecx],7Ah ; Test input char against lowercase 'z’
        ja Next          ; If above 'z’ in ASCII, not lowercase
                         ; At this point, we have a lowercase char
        sub byte [ebp+ecx],20h ; Subtract 20h to give uppercase...
Next:   dec ecx          ; Decrement counter
        jnz Scan         ; If characters remain, loop back
; Write the buffer full of processed text to stdout:
Write:
        mov eax,4        ; Specify sys_write call
        mov ebx,1        ; Specify File Descriptor 1: Standard output
        mov ecx,Buff     ; Pass offset of the buffer
        mov edx,esi      ; Pass the # of bytes of data in the buffer
        int 80h          ; Make sys_write kernel call
        jmp read         ; Loop back and load another buffer full
; All done! Let’s end this party:
Done:
        mov eax,1        ; Code for Exit Syscall
        mov ebx,0        ; Return a code of zero
        int 80H          ; Make sys_exit kernel call


Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Assembly Lowercase To Uppercas
« Reply #1 on: June 05, 2014, 11:28:31 AM »
That's a really good puzzle, Silvercats! I've never seen anything quite like it.

Presumably, it's not the code. I notice, however, that there are some "weird characters" in the comments. Are you using some alphabet other than "latin"? I don't know what that would have to do with anything, but I just noticed it. I was thinking of pointing out that:
Code: [Select]
cmp [blah], byte 'a'
cmp [blah], byte 'z'
is a lot more readable than hex numbers, but then I noticed the "weird characters", so put that on hold. :)

The command to ld is okay if you've got a 32-bit system. If you've got a 64-bit system, you've gotta tell ld that you want 32-bit code (and you do) - add "-m elf_i386" to the command line to ld. That could cause trouble, but I don't see why it would act like "that". It's as if it's trying to tell you "bash: file or directory not found" but half in Martian. Again, I suspect a "character set" issue of some kind.

What's not found? I notice you call it "uppercase2" and Jeff calls it "uppercaser2". Could that be an issue? I ASSume that fromLow.txt exists. This isn't your problem, but I'll tell you about it in case you encounter it later. You can call "printf" etc. in your code and use ld to link it. When you try to run the result, you'll get "bash: file or directory not found". I can see my program right there, even examine the contents! What's "not found" is the default interpreter / dynamic linker that ld tries to use (for reasons not known to mere mortals). Telling ld "-I /lib/ld-linux.so.2" (on my old system) got rid of the "not found".

sudo - Why sudo? You shouldn't need to run this as root, should you? Perhaps better NOT to run untested programs as root. It will probably make "toUpper.txt" owned by root. Maybe that's what you want. If you need root permissions for system calls you're using, you can (as root) "chown root:root myprog" and "chmod +s myprog" and a regular user can run it.

Anyway, I can't think of anything to tell you but to pick away at the possibilities as you think of 'em until you solve it. Good luck.

Best,
Frank


Offline silvercats

  • Jr. Member
  • *
  • Posts: 5
Re: Assembly Lowercase To Uppercas
« Reply #2 on: June 05, 2014, 03:31:31 PM »
Thanks. Great suggestions.


Those Weird character were there on the Terminal windows as it was, when I ran the command. I copied the whole thing on the Terminal windows to here. So I think they were a result of the program probably? Are you suggesting that this is a linker problem?
I am a noob.

I got this example from a book called "Step by step assembly" by Jeff dunteman. I used the same way to assemble/link all the .asm files so far.

It gave me an error saying "can't execute the file........." (or similar) first. So I used the sudo and then it worked and that was the weird output.

Yeah, the original author calls the the uppercaser. The executable file name is uppercase. So I didn't think that was the problem.

p.s I am using the Ubuntu 9.10 (the book was written in 2009). The reason is that new versions have many problems with the old tools mentioned in the book.

Thanks.

Offline silvercats

  • Jr. Member
  • *
  • Posts: 5
Re: Assembly Lowercase To Uppercas
« Reply #3 on: June 05, 2014, 03:44:18 PM »
I can see my program right there, even examine the contents! What's "not found" is the default interpreter / dynamic linker that ld tries to use (for reasons not known to mere mortals). Telling ld "-I /lib/ld-linux.so.2" (on my old system) got rid of the "not found".


Did you mean that this worked perfectly in your pc? without any errors? The output was as expected too?

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Assembly Lowercase To Uppercas
« Reply #4 on: June 06, 2014, 06:48:40 AM »
I can see my program right there, even examine the contents! What's "not found" is the default interpreter / dynamic linker that ld tries to use (for reasons not known to mere mortals). Telling ld "-I /lib/ld-linux.so.2" (on my old system) got rid of the "not found".


Did you mean that this worked perfectly in your pc? without any errors? The output was as expected too?

No!  I was speaking of a hypothetical program there - one with a call to a C library function. I had not, at that time, tested your program (Jeff's program). Now I have. It worked perfectly. Since I'm afflicted with a 64-bit system, ld did throw an error with the command line as shown in the comments. Adding "-m elf_i386" cleared it up. This is good - it means you can't accidentally link it as 64-bit.

I don't have your "fromLow.txt" of course. What's in that? I just used its own source code as an input file. I didn't redirect stdout - just let it print to the screen. Try that?

I know what Ubuntu is (an African word meaning "one ring to rule them all" - just kidding) but I don't know what 9.10 implies. I take it it's a 32-bit system, which is probably much better for your purposes. I may go back to a 32-bit system myself.

It is possible to get "Martian" on your display as a result of just printing characters to the screen. In particular, an ESC character (ascii 27 or 1Bh) followed by certain other codes will switch character sets If this happens, typing "reset" - even if you can' see the letters - may restore your display to default parameters. I don't think it'll help you - but it shouldn't hurt to try.

The error message "can't execute binary file" is familiar. It comes from not having a valid ELF header - possibly caused by a linker error. I don't see why running as root would fix it. You really shouldn't have to do that. Something strange happening there, which may or may not be related to the strange output. Try assembling and linking again - not as root - and see if that changes anything?

I haven't actually read any of Jeff Duntemann's books. I'm familiar with them - very good reputation! I did download the example code from the Second Edition, and found a couple of bugs - imperfect translations of the Masm syntax used in the First Edition. I don't think there's any problem with this code.

I don't think I've solved the problem yet.

Best,
Frank


Offline dalfonso01

  • Jr. Member
  • *
  • Posts: 44
Re: Assembly Lowercase To Uppercas
« Reply #5 on: June 06, 2014, 05:41:24 PM »
Hi,
I had no issues with the code in the book, this is the one I used to start on assembly and NASM. Made all the examples including this, and was the book I thanks for both introducing me on assembly and leaving me on highway on foot, before mmapping something.

About the insight I suppose you refer about tools, I got a successful compiled version on 11.04 , I was already using (I trash only what is no longer able to contain the problem) and works better (or right ) than the backport from seven machines that shows a known bug in registers view.  If you intend to use the tools in the future 9.10 could become old too early to maintain the tools and go on in C/C++

Fabio D'Alfonso



« Last Edit: June 06, 2014, 05:56:46 PM by dalfonso01 »

Offline silvercats

  • Jr. Member
  • *
  • Posts: 5
Re: Assembly Lowercase To Uppercas
« Reply #6 on: June 08, 2014, 12:24:14 PM »
Finally found the problem.  ???
yeah it was a linker problem and its commands. My bad!

I use this editor called Geany. It has a button called "compile".I have used that button to convert the program by a mistake. I went to recheck the linking and its commands as you suggested and found what I have used. Thanks for the suggestions help Frank Kotler!

Offline silvercats

  • Jr. Member
  • *
  • Posts: 5
Re: Assembly Lowercase To Uppercas
« Reply #7 on: June 08, 2014, 12:32:01 PM »
Hi,
I had no issues with the code in the book, this is the one I used to start on assembly and NASM. Made all the examples including this, and was the book I thanks for both introducing me on assembly and leaving me on highway on foot, before mmapping something.

About the insight I suppose you refer about tools, I got a successful compiled version on 11.04 , I was already using (I trash only what is no longer able to contain the problem) and works better (or right ) than the backport from seven machines that shows a known bug in registers view.  If you intend to use the tools in the future 9.10 could become old too early to maintain the tools and go on in C/C++

Fabio D'Alfonso


yup, can get some tools to work on new versions. But I faced some other several issues as I remember. Using this for learning purposes temporarily. It is good if you can use a new version

Offline dalfonso01

  • Jr. Member
  • *
  • Posts: 44
Re: Assembly Lowercase To Uppercas
« Reply #8 on: June 09, 2014, 03:48:25 PM »
Hi,
I have no room now to make a check, but the insight is part of a package from RedHat named GNUPro, I just cannot check that is available also on CentOS or Oracle Linux.

The current version is a 7.0+ while the latest by source is the 6.8.1.  The problem with backport from seven machines is that it was built on a different platform and only the package was backported, so the landscape does not correspond when the tool is running.

this is the RH link: http://www.redhat.com/services/custom/gnupro/