cmptUser
cmptUser

Reputation: 51

Converting a .string to .quad in assembly language

I just started learning assembly language and we got a homework assignment where the objective of the assignment is to write a program that will convert signed integers expressed as character strings into 2’s complement encodings.

So I thought one thing we need to do is when we enter a string like "10" we will need to break it up into like "1" and "0" and then do the conversion 2’s complement encodings(let me know if you think this is a correct approach).

So I made this small program - just to extract the "1" of the "10".

Here are some comments for the code where the numbers are in the code

  1. Moves the character code of "1" (0x31) into register rax.
    That is R[rax] <- M[R[rbx]+0] = M[asc] = 0x31.

  2. Moves the character code of "0" (0x30) into register rcx.
    That is R[rcx] <- M[R[rbx]+1] = M[asc+1] = 0x30.

  3. Moves the character code of "1" (0x31) into answ. Also, I'm not really sure what %al does, it was recommended by my teacher.

Then in gdb I put a break on the ret statement like break *main+18 and then I enter x/xg $answ

After that I get an error

value can't be converted to an integer

I'm not sure how to fix this. Thanks for the help. The program is:

.data 
  asc: .string "10"
  answ: .quad

.text
.globl main

main: 
  mov $asc, %rbx
  mov 0(%rbx), %rax  #1
  mov 1(%rbx), %rcx   #2
  mov %al, 3(%rbx)    #3
  ret

Upvotes: 0

Views: 1028

Answers (1)

paulsm4
paulsm4

Reputation: 121649

To answer your questions:

  • %al (and %ar) are registers. Here's a good diagram:

    • RAX is the 64-bit value
    • EAX is the 32-bit value
    • AX is the lower 16-bits
    • AL is the lower 8 bits
    • AH is the bits 8 through 15 (zero-based)
  • The ASCII character for "0" is 0x30. The ASCII character for "1" is 0x31. So your "string of 1s and 0s will look something like 0x30303130...

  • The reason x/xg $answ gives an error is that it's a gdb syntax error. Here's a good "cheat sheet":

ALSO:

  • "Programming from the Ground Up", by Jonathan Barlett, is a wonderful resource. And it's completely free - he posted the entire text on-line:

http://savannah.nongnu.org/projects/pgubook/

Upvotes: 1

Related Questions