Home IA-32 beginner asm sys_write confusion
Reply: 0

IA-32 beginner asm sys_write confusion

user48998
1#
user48998 Published in September 20, 2018, 10:22 pm

I'm using NASM, IA-32 (I think). The homework is to take ASCII from a .txt file and print it to the console in hexadecimal using linux shell input redirection. I think I've got the ascii to hex part figured out, but I'm having a hard time making it print the console correctly. I'm sure I'm just missing something small and silly. The file I'm reading just to test is something simple like this: ABC 123 The output should be: 41 42 43 0A 31 20 32 20 33 20

The problem is happening in

_hexOut:

I've tried changing more stuff around than I can fit in this post, and I can't figure out where I'm going wrong. The only output I'm getting is: 41 42 43

section .text   
    global _start
_start:
    mov edx, inBuffSize         ; sys_read
    mov ecx, inBuffer           ; -
    mov ebx, 0x00               ; -
    mov eax, 0x03               ; - 
    int 0x80                    ; end sys_read

    mov [inputLen], eax
    cmp eax, 0      
    jle _exit       

_inLeft:
    mov ecx, [inCount]          ; move 
    cmp ecx, [inputLen]                       
    je _hexOut                        
    movzx eax, byte [inBuffer + ecx]             
    mov [temp], al                   
    shr al, 4                        
    mov bl, 9                     
    cmp al, bl                         
    jle _leftIsNumber             
    mov bl, 10                        
    cmp al, bl                          
    jge _leftIsLetter           

_leftIsNumber:
    mov ecx, [outCount]                 
    add al, 48                         
    mov [outBuffer + ecx], al             
    mov ecx, [outCount]               
    inc ecx                           
    mov [outCount], ecx              
    jmp _inRight               

_leftIsLetter:
    mov ecx, [outCount]                 
    add al, 55                         
    mov [outBuffer + ecx], al           
    mov ecx, [outCount]                 
    inc ecx                            
    mov [outCount], ecx                
    jmp _inRight              

_inRight:
    mov ecx, [inCount]                  
    inc ecx                            
    mov [inCount], ecx                  
    movzx eax, byte [temp]                     
    and al, 0x0F                       
    mov bl, 9                          
    cmp al, bl                         
    jle _rightIsNumber              
    mov bl, 10                         
    cmp al, bl                     
    jge _rightIsLetter            

_rightIsNumber:
    mov ecx, [outCount]                 
    add al, 48                   
    mov [outBuffer + ecx], al       
    mov ecx, [outCount]            
    inc ecx                            
    mov [outBuffer + ecx], byte 32 
    inc ecx                       
    mov [outCount], ecx             
    jmp _inLeft                

_rightIsLetter:
    mov ecx, [outCount]                 
    add al, 55             
    mov [outBuffer + ecx], al            
    mov ecx, [outCount]                 
    inc ecx                          
    mov [outBuffer + ecx], byte 32        
    inc ecx                         
    mov [outCount], ecx                 
    jmp _inLeft              

_hexOut:;------------------ PROBLEMS HERE ----------------------
   ; mov ecx, [inputLen]           
   ; mov [outCount + ecx], byte 0x0A   
   ; inc ecx                         
   ; mov [inputLen], ecx             

    mov edx, [inputLen] 
    mov ecx, outBuffer  
    mov ebx, 0x01       
    mov eax, 0x04       
    int 0x80            
;------------------------------------------------------
_exit:
    mov ebx, 0
    mov eax, 1    
    int 0x80         


section .data   


section .bss 
    inBuffer resb inBuffSize
    outBuffer resb outBuffSize
    inBuffSize equ 200
    outBuffSize equ 600
    inCount resd 1
    outCount resd 1
    temp resb 1
    inputLen resd 1   

The second part of the problem is that I need to be able to change how many hex bytes are shown per line in the console, but I can't start to tackle that until I get the first problem solved. Any nudge in the right direction would be appreciated!

share|improve this question
  • You seem to be printing inputLen bytes when in fact you will have three times as many characters if you convert to two digits hex and a space. – Jester Feb 14 at 0:12
  • Ah, ok that makes sense. But I can't to something like [inputLen * 3] right? – 1girl2pups Feb 14 at 0:16
  • Not like that, no. – Jester Feb 14 at 0:18
  • Any suggestions on how I might to do that? I'm new to asm, sorry – 1girl2pups Feb 14 at 0:20
  • 3
    You realize you have an outCount variable, right? Is this even your own code? – Jester Feb 14 at 0:30

active oldest votes

Your Answer

StackExchange.ifUsing("editor", function () { StackExchange.using("externalEditor", function () { StackExchange.using("snippets", function () { StackExchange.snippets.init(); }); }); }, "code-snippets"); StackExchange.ready(function() { var channelOptions = { tags: "".split(" "), id: "1" }; initTagRenderer("".split(" "), "".split(" "), channelOptions); StackExchange.using("externalEditor", function() { // Have to fire editor after snippets, if snippets enabled if (StackExchange.settings.snippets.snippetsEnabled) { StackExchange.using("snippets", function() { createEditor(); }); } else { createEditor(); } }); function createEditor() { StackExchange.prepareEditor({ heartbeatType: 'answer', convertImagesToLinks: true, noModals: false, showLowRepImageUploadWarning: true, reputationToPostImages: 10, bindNavPrevention: true, postfix: "", onDemand: true, discardSelector: ".discard-answer" ,immediatelyShowMarkdownHelp:true }); } });
 
StackExchange.ready( function () { StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f48777691%2fia-32-beginner-asm-sys-write-confusion%23new-answer', 'question_page'); } );

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged linux assembly x86 nasm or ask your own question.

StackExchange.ready(function(){$.get('/posts/48777691/ivc/cf21');});
StackExchange.ready(function () { StackExchange.responsiveness.addSwitcher(); }) (function(i, s, o, g, r, a, m) { i['GoogleAnalyticsObject'] = r; i[r] = i[r] || function() { (i[r].q = i[r].q || []).push(arguments) }, i[r].l = 1 * new Date(); a = s.createElement(o), m = s.getElementsByTagName(o)[0]; a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, 'script', 'https://www.google-analytics.com/analytics.js', 'ga'); StackExchange.ready(function () { StackExchange.ga.init({ sendTitles: true, tracker: window.ga, trackingCodes: [ 'UA-108242619-1' ] }); StackExchange.ga.setDimension('dimension2', '|linux|assembly|x86|nasm|'); StackExchange.ga.setDimension('dimension3', 'Questions/Show'); StackExchange.ga.trackPageView(); }); /**/ var _qevents = _qevents || [], _comscore = _comscore || []; (function() { var ssl = 'https:' == document.location.protocol, s = document.getElementsByTagName('script')[0], qc = document.createElement('script'); qc.async = true; qc.src = (ssl ? 'https://secure' : 'http://edge') + '.quantserve.com/quant.js'; s.parentNode.insertBefore(qc, s); _qevents.push({ qacct: "p-c1rF4kxgLUzNc" }); /**/ var sc = document.createElement('script'); sc.async = true; sc.src = (ssl ? 'https://sb' : 'http://b') + '.scorecardresearch.com/beacon.js'; s.parentNode.insertBefore(sc, s); _comscore.push({ c1: "2", c2: "17440561" }); })();
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.303185 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO