首页
社区
课程
招聘
请教一段汇编代码
发表于: 2018-12-14 16:54 3326

请教一段汇编代码

2018-12-14 16:54
3326
    看各位大佬参加CTF比赛,感觉挺过瘾的,所以就想彻底的学习一下汇编指令,那样应该就可以看懂大佬们的解题分析了,然后我就找到了《Intel 80386程序员手册》,虽然相比最新的CPU,指令要少很多,但都是一些最基本和最核心的指令。

    第3.4.4.5节中有5段演示SHLD、SHRD用法的代码:
    第一个例子我看懂了:
    虽然注释写的是插入,实际上是替换,用ESI指向的length长度的位串,替换目的串中指定offset处的位串 ,我还画了图辅助理解。
    

    第二个例子只是将替换位串的长度放宽了,但我看了很长时间都没看懂, 希望有大佬出现,帮忙解释一下这段代码:
  2.  Bit String Insert into Memory (when bit string is 1-31 bits long, i.e.
      spans five bytes or less):

      ; Insert a right-justified bit string from register into
      ; memory bit string.
      ;
      ; Assumptions:
      ; 1) The base of the string array is dword aligned, and
      ; 2) the length of the bit string is an immediate value
      ;    but the bit offset is held in a register.
      ;
      ; Register ESI holds the right-justified bit string
      ; to be inserted.
      ; Register EDI holds the bit offset of the start of the
      ; substring.
      ; Registers EAX, EBX, ECX, and EDI are also used by
      ; this "insert" operation.
      ;
      MOV   ECX,EDI     ; temp storage for offset
      SHR   EDI,5       ; signed divide offset by 32 (dword address)
      SHL   EDI,2       ; multiply by 4 (in byte address format)
      AND   CL,1FH      ; isolate low five bits of offset in CL
      MOV   EAX,[EDI]strg_base      ; move low string dword into EAX
      MOV   EDX,[EDI]strg_base+4    ; other string dword into EDX
      MOV   EBX,EAX     ; temp storage for part of string     + rotate
      SHRD  EAX,EDX,CL  ; double shift by offset within dword | EDX:EAX
      SHRD  EAX,EBX,CL  ; double shift by offset within dword + right
      SHRD  EAX,ESI,length          ; bring in new bits
      ROL   EAX,length  ; right justify new bit field
      MOV   EBX,EAX     ; temp storage for part of string         + rotate
      SHLD  EAX,EDX,CL  ; double shift back by offset within word | EDX:EAX
      SHLD  EDX,EBX,CL  ; double shift back by offset within word + left
      MOV   [EDI]strg_base,EAX      ; replace dword in memory
      MOV   [EDI]strg_base+4,EDX    ; replace dword in memory


    我也画了图:

    


[课程]Android-CTF解题方法汇总!

收藏
免费 0
支持
分享
最新回复 (9)
雪    币: 1176
活跃值: (1234)
能力值: ( LV12,RANK:380 )
在线值:
发帖
回帖
粉丝
2
是不是不懂shld 和shrd什么意思吗? 是这样的话我觉得你描述的好复杂。。。
2018-12-14 18:39
0
雪    币: 3091
活跃值: (10716)
能力值: ( LV9,RANK:240 )
在线值:
发帖
回帖
粉丝
3
shld,shrd我明白,不理解这段代码
2018-12-14 18:42
0
雪    币: 1176
活跃值: (1234)
能力值: ( LV12,RANK:380 )
在线值:
发帖
回帖
粉丝
4
尝试还原c代码 或者直接拿几组数据调试看结果 
2018-12-14 18:49
0
雪    币: 2067
活跃值: (82)
能力值: ( LV9,RANK:180 )
在线值:
发帖
回帖
粉丝
5
晕倒, 我也看不懂
2018-12-16 00:48
0
雪    币: 5734
活跃值: (1737)
能力值: ( LV4,RANK:50 )
在线值:
发帖
回帖
粉丝
6
shrd shld 是把两个寄存器拼起来移动 数据保留一个寄存器的长度 
2018-12-16 01:14
0
雪    币: 1270
活跃值: (109)
能力值: ( LV5,RANK:60 )
在线值:
发帖
回帖
粉丝
7
sessiondiy 晕倒, 我也看不懂
枸杞茶多喝点儿.

DOS里面单步走走.
2018-12-17 17:45
0
雪    币: 3091
活跃值: (10716)
能力值: ( LV9,RANK:240 )
在线值:
发帖
回帖
粉丝
8
各位大佬,我调了一下,没有得到期望的结果(由于我对linux环境相对熟悉一点,所以把代码转换成AT&T格式,然后用gdb调试的)。

汇编:as --gstabs -o test.o test.s
链接:ld -o a.out test.o
代码:
.data
        strg_base : .string "ABCDEFGHIJKLMN\0"
        strg_insr : .string "\x01\x03\x07\x0f"
        length = 29
.text
.global _start
_start:
        movl $strg_insr, %esi
        movl $45, %edi
        movl %edi, %ecx
        shrl $5, %edi
        shll $2, %edi
        andb $0x1f, %cl
        movl $4, %ebx
        movl strg_base(%edi), %eax
        movl strg_base(%edi,%ebx,), %edx
        movl %eax, %ebx
        shrdl %cl, %edx, %eax
        shrdl %cl, %ebx, %eax
        shrdl $length, %esi, %eax
        roll $length, %eax
        movl %eax, %ebx
        shldl %cl, %edx, %eax
        shldl %cl, %ebx, %edx
        movl $4, %ebx
        movl %eax, strg_base(%edi)
        movl %edx, strg_base(%edi,%ebx,)

        movl $0, %ebx
        movl $1, %eax
        int  $0x80

2018-12-18 12:49
0
雪    币: 2067
活跃值: (82)
能力值: ( LV9,RANK:180 )
在线值:
发帖
回帖
粉丝
9
xinpoo 各位大佬,我调了一下,没有得到期望的结果(由于我对linux环境相对熟悉一点,所以把代码转换成AT&T格式,然后用gdb调试的)。汇编:as --gstabs -o test.o te ...
代码可能错的
你将  SHRD  EAX,EBX,CL
改成  SHRD  EDX,EBX,CL
试试
2018-12-18 22:12
0
雪    币: 3091
活跃值: (10716)
能力值: ( LV9,RANK:240 )
在线值:
发帖
回帖
粉丝
10
sessiondiy 代码可能错的 你将 SHRD EAX,EBX,CL 改成 SHRD EDX,EBX,CL 试试
我开始也是这样想的,把EDX,EAX看作一个整体,就和例子1类似了,但想来想去,没想到一个简洁的改动方法,我对汇编不熟悉,加上这是官方手册里的,不敢断定错误,所以就来问大佬们了
2018-12-19 08:38
0
游客
登录 | 注册 方可回帖
返回
//