1

I am new to assembly and I am trying to understand the tools at my disposal as well. I have to create a callable assembly function for a tiny C program. The goal is for the user to type in the letter by hex value -so 41 for A. Then the C passes this hex value as an unsigned char to the assembly function. Below is the C code

extern char *printbin(unsigned char);

int main(void)
{
  unsigned int x;

  printf("enter the character's ascii value in hex: \n");
  scanf("%x",&x);
  printf("The binary format for character %c is %s\n", 
                                 x,   printbin((unsigned char)x));
  return 0;
}

My understanding that on the stack there should be a value of 41 stored in one byte as in C chars are really just numbers as well. Is that assumption correct. The task is to covert the input into an ouput string of two 4 digit binary numbers for each digit separated by a space so 0100 0001 My first question is how can I split up the byte in a register to just 4 bits or just the 1 or the 4? I set up a skeleton for the assembly file

.text
.globl _printbin

_printbin:

        pushl %ebp
        movl %esp, %ebp

        movl 4(%ebp), %ecx  #store the char in ecx register
        jmp end

donibble: #function to convert digit to binary

end:
        movl %ebp, %esp
        popl %ebp
        ret

However I am lost on how to proceed with grabbing just half of the byte. I could grab just the one byte with %el to make it just 8 bits instead of the %ecx when I do "work" on it but I am unaware of any kind of register to grab anything fewer then just 1 byte. Any help or ideas would be greatly appreciated.

Updated assembly code

.text
.globl _printbin

_printbin:

        pushl %ebp
        movl %esp, %ebp

        movl 4(%ebp), %edx #grabs the hex byte
        movl $0, %eax   #sets $eax to 0

        shrb $4,%dl
        jmp donibble
        L2:
                incl %eax
        #       addl 0x20, %return string #add the space.
                movl 4(%ebp), %edx
                AND %dl, 0x0f 
                jmp donibble    
        L3:
                jmp end

donibble:
#Takes %dl and gets string and adds string to return String
        cmpb %dl, 0x0
        je Zero
        cmpb %dl, 0x1
        je One

Zero:
         #add 0000 to string
 jmp donibbleEnd
One: 
        #add 0001 to string

        donibbleEnd:
        #to send back to printbin
                cmpb $0, %eax
                je L2
                jmp L3


end:
        movl $returnString, %eax
        movl %ebp, %esp
        popl %ebp
        ret

.data

returnString:
        .asciz "1001 1100\n"

.end

Is there a way to add to my $returnString following this updated structure above? apparently addl "0000", $returnString doesn't work at all :)

6
  • 2
    There is none. You will have to do bit shitfing and masking, as you would in C too. x >> 4 and x & 0x0f to get the two halves.
    – Jester
    Mar 16, 2016 at 18:07
  • Can you shift and mask in assembly? I have only seen basic math used with commands like addl instead of a + sign
    – Peter3
    Mar 16, 2016 at 18:08
  • 1
    You shift and mask using the appropriate instructions :) I left that as an exercise to you. You can also shift and mask constants with the operators of your assembler, but that's not applicable here.
    – Jester
    Mar 16, 2016 at 18:11
  • Terminology note: they're instructions, not "commands". Shifts: look at the shl and shr instructions. Masks: you have and, or, xor, not. Mar 16, 2016 at 18:24
  • Thanks for the last two comments. I will try and see what I can accomplish from here. I should be able to mask and shift the bits to separate them. I will work on converting that one number into the binary string.
    – Peter3
    Mar 16, 2016 at 18:30

2 Answers 2

1

That's a weird prototype for the function. Are you supposed to return a pointer to a static buffer? A more typical design would be for the caller to pass a pointer to a buffer where the function could store the string.


The usual way to do this is to take advantage of the fact that the shift instructions set CF to the last bit shifted out. You can then use setc to get a zero or one, and convert that to an ascii '0' or '1'. Another trick is adc $0, or adc $'0' to read the carry flag.

copying a register and using and to get just the low bit is also viable, though.

If you're stuck, use the search. There have been previous SO questions about this. Try searching within the and/or tags. Have a look at the x86 tag wiki for many good links to beginner stuff and reference material.

0

For the record to close this out. This was the code I ended with. I appreciate the help from everyone.

.text
.globl _printbin

_printbin:

    pushl %ebp
    movl %esp, %ebp

    movb 8(%ebp), %dl #grabs the hex byte
    movl $0, %eax   #sets $eax to 0

    shrb $4, %dl
    movl $returnString, %ecx
    pushl %ebx #free up ebx
    jmp donibble

returnFromDonibble:
    incl %ecx
    movb 8(%ebp), %dl
    movb $0x0f, %al
    andb $15, %dl
    movb $0x39, %al
    cmpb (%ecx), %al
    je donibble
    jne end




donibble:
#Takes %dl and gets string and adds string to return String
    movb %dl, %bl
    movb $0x08, %al
    andb $8,%dl
    je zero
    jne one

    one:
        movb $0x31, (%ecx)
        incl %ecx
        jmp L2
    zero:
        movb $0x30, (%ecx)
        incl %ecx
        jmp L2
L2:
    movb %bl, %dl
    movb $0x04, %al 
    andb $4, %dl
    je zero2
    jne one2

    one2:
        movb $0x31, (%ecx)
        incl %ecx
        jmp L3
    zero2:
        movb $0x30, (%ecx)
        incl %ecx
        jmp L3

L3:
    movb %bl, %dl
    movb $0x02, %al
    andb $2, %dl
    je zero3
    jne one3

    one3:
        movb $0x31, (%ecx)
        incl %ecx
        jmp L4
    zero3:
        movb $0x30, (%ecx)
        incl %ecx
        jmp L4

L4:
    movb %bl, %dl
    movb $0x01, %al
    andb $1, %dl
    je zero4
    jne one4

    one4:
        movb $0x31, (%ecx)
        incl %ecx
        jmp returnFromDonibble
    zero4:
        movb $0x30, (%ecx)
        inc %ecx
        jmp returnFromDonibble

end:
    popl %ebx
    movl $returnString, %eax
    movl %ebp, %esp
    popl %ebp
    ret




.data

returnString:   .asciz "9999 9999"

.end
4
  • Glad you were able to get your code sorted out. returnString could be in the .bss instead of the .data section, since the initial value doesn't matter (you always fully overwrite it). Also, your je zero3 / jne one3 pairs are silly: just use a single branch that falls through or not. Note that the jump target of the 2nd branch is the insn following it, so it goes to the same place whether it's taken or not. Also note that after isolating the bit you want in %dl with and, you can shr $1, %dl / add $0x30, %dl, and then you don't need a branch. Mar 25, 2016 at 8:01
  • incl %ecx is present in both sides of your branches, so you should pull it out and put it where the branches rejoin. And again, remove the jmp L4 that comes right before L4. You don't need to jump over white-space. Mar 25, 2016 at 8:04
  • You can also loop over the bits in a nibble using a shift, so the bit you're interested in is always in the low bit (or high bit for shl) of the register you're shifting. To do even better than that, take advantage of the fact that shift instructions set the carry flag to the bit just shifted out. So you can use shl $1, %al / adc $0x30, %a_zeroed_reg or something. Mar 25, 2016 at 8:07
  • It's normal for beginners to write giant inefficient asm code. As long as you understand why it works, you're learning, and will pick up tricks for writing more efficient code as you learn more. Mar 25, 2016 at 8:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.